Sometimes while doing CI/CD with GitHub Actions, you need to read environment variables stored in GitHub Secrets
for your tests. Here I demonstrate 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 asT1
and Value asHello world
. Then save it
- Once you save it, it should be visible on the screen
- Now you need to add the secret in your GitHub Actions Workflow file. You need to call the secret using
env:
T1: ${{secrets.T1}}
Here is my Github Actions workflow
name: Go
on:
push:
branches: ["master"]
pull_request:
branches: ["master"]
env:
T1: ${{secrets.T1}}
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
- name: Environment list
run: env
Once you do that, you should access this in your unit tests and code as
env1, err1 := os.LookupEnv("T1")
log.Println(env1, err1)
Top comments (0)