At times while CI/CD with Github Actions, you need to read environment variables stored in Github Secrets
. This article describes how you can access Github Secrets
stored in Github while doing CI/CD.
Steps
- Go to Github Repository > Settings. From the left menu, selection Secrets > Actions
- Select New repository secret and add a key-value pair. For our example, we have used the key name as
T1
and Value asHello world
and save it
- Once you save it, it should be visible on the screen
- You need to add the secret in your Github Actions Workflow file where it can be called as.
env:
T1: ${{secrets.T1}}
Here, the value secrets.T1
stored in Github Secrets is assigned to a Runner environment variable with key T1
. secrets
is a prefix that you need to use to access the variable from Github
- Lastly, here is the skeleton of the Github Actions that you need to call.
name: Read environment variable in Github Secrets from your program
on:
push:
branches: ["master"]
pull_request:
branches: ["master"]
env:
T1: ${{secrets.T1}}
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Environment list
run: env
Optional
- I have created a sample Golang program to demonstrate a complete Workflow Github Actions workflow. Once you do that, you should access this in your unit tests and code (for the sample Golang program) as
env1, err1 := os.LookupEnv("T1")
log.Println(env1, err1)