Home > Blockchain >  .NET User Secrets in GitHub Actions variables
.NET User Secrets in GitHub Actions variables

Time:01-20

I have this issue with .NET build pipeline on GitHub actions that fail in the test phase. The problem is that locally I use User Secrets configured using the structured JSON. Therefore I need to provide the replacement in GitHub Actions using the secrets that live in the scope of the Action lifetime.

In Bitbucket, I could simulate the structure of the JSON nesting using the __ resolving into correct namespaces; however, this option does not seem to work on GitHub actions.

Example JSON:


{
  "IntegrationTest": {
    "B2CPolicy": "https://example.com",
    "ClientId": "some_client_id"
  }
}

Where I would assume the variable INTEGRATIONTEST__B2CPOLICY should be placed into the GitHub actions secrets.

The dotnet.yaml:

# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET

on:
  push:
    branches: [ "develop" ]
  pull_request:
    branches: [ "develop" ]

jobs:
  build:
  
    env:
      SLN_FILE: "./src/be/proj.sln" 
      REPORTS_PATH: "./test-reports/build_$GITHUB_RUN_ATTEMPT"
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore $SLN_FILE
    - name: Generate client for test project
      run: pwsh -File "./src/be/scripts/nswag/generate_data_for_test_project.ps1"
    - name: Build
      run: dotnet build --no-restore --configuration Release $SLN_FILE
    - name: Test
      run: dotnet test --no-build --verbosity normal --configuration Release --test-adapter-path:. --logger:"junit;LogFilePath=$REPORTS_PATH/junit.xml;MethodFormat=Class;FailureBodyFormat=Verbose" $SLN_FILE

Any clue where is the possible problem?

Could it be caused by using encrypted secrets?

Thanks for any ideas.

CodePudding user response:

Secrets are not automatically available as environment variables in all your actions, you need to add them. Something like this should do it.

    - name: Test
      env:
        INTEGRATIONTEST__B2CPOLICY: ${{ secrets.INTEGRATIONTEST__B2CPOLICY }}
      run: dotnet test --no-build --verbosity normal --configuration Release --test-adapter-path:. --logger:"junit;LogFilePath=$REPORTS_PATH/junit.xml;MethodFormat=Class;FailureBodyFormat=Verbose" $SLN_FILE

For more info: https://docs.github.com/en/actions/security-guides/encrypted-secrets#using-encrypted-secrets-in-a-workflow

  • Related