HASSANHABIB

§ 3.2 — The Standard Team Practices

Secrets in GitHub Actions

1 min read · chapter 12 of 15

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.

  1. Go to the GitHub repository where you want to use these settings and click on “Settings” in the top navigation bar.

  2. In the left sidebar, click on “Secrets” and then click on “New repository secret” button.

  3. 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.
  4. 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.