§ 3.2 — The Standard Team Practices
Secrets in GitHub Actions
To setup your environment variables for use within GitHub we will need to setup repository secrets and add a section at the beginning of your workflow file.
-
Go to the GitHub repository where you want to use these settings and click on “Settings” in the top navigation bar.
-
In the left sidebar, click on “Secrets” and then click on “New repository secret” button.
-
Create two new secrets named “MYAPP_ACCEPTANCE_MYSETTINGS__APIKEY” and “MYAPP_ACCEPTANCE_MYSETTINGS__ORGANISATIONID”, with the corresponding values for “ApiKey” and “OrganisationId” from the given settings. (“ApiUrl” stays in appsettings.json.)
NOTE
- We have prefixed our environment variables with
MYAPP_ACCEPTANCE_to match the earlier configuration of.AddEnvironmentVariables("MYAPP_ACCEPTANCE_");. - The other difference is that a colon (:) is not allowed on GitHub Secrets to represent a hierarchy and you have to use a double underscore (__) instead.
- We have prefixed our environment variables with
-
To expose these secrets as environment variables in the GitHub Action Workflow, add the following code snippet at the beginning of your workflow file:
env:
ApiKey: ${{ secrets.MYAPP_ACCEPTANCE_MYSETTINGS__APIKEY }}
OrgId: ${{ secrets.MYAPP_ACCEPTANCE_MYSETTINGS__ORGANISATIONID }}
A full example of the dotnet.yml file will look like this:
name: MyApplication Build
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: windows-latest
env:
ApiKey: ${{ secrets.MYAPP_ACCEPTANCE_MYSETTINGS__APIKEY }}
OrgId: ${{ secrets.MYAPP_ACCEPTANCE_MYSETTINGS__ORGANISATIONID }}
steps:
- name: Pulling Code
uses: actions/checkout@v2
- name: Installing .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.0.201
include-prerelease: false
- name: Restoring Packages
run: dotnet restore
- name: Building Solution
run: dotnet build --no-restore
- name: Running Tests
run: dotnet test --no-build --verbosity normal This chapter lives on GitHub, where it is written in the open. Read the source or suggest a change.