How to configure .net core 3.1 appsettings to run tests on Github actions

Viewed 537

I'm developing a web API using .Net core 3.1 trying to integrate it to Github Actions to run the integration tests when a pull request is created.

I'm using the secrets manager to store my API tokens and other sensitive data in development mode

secrets.json

{
    "Firebase": {
        "Login": "foo@bar.com",
        "Password": "FooBar",
        "Url": "foobar.firebaseapp.com "
    },

}

And on GitHub I've tried to add Secrets(GitHub secrets = environment variables) with the same names I have on my secrets JSON but it hasn't worked. The GitHub secrets that I've created are like

(KEY - VALUE)
LOGIN - foo@bar.com 
PASSWORD - FooBar

And on my test class, I'm invoking the environment variables using the following configuration before the tests

public MyTestClass()
{           
    var builder = new ConfigurationBuilder() 
        .AddUserSecrets<MyTestClass>()
        .AddEnvironmentVariables();

    Configuration = builder.Build();
    _settings = Configuration.Get<Settings>();
}

And my .yml looks like

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.300
    - name: Nuget
      run: dotnet restore
    - name: Build
      run: dotnet build --configuration Release --no-restore
    - name: Test
      run: dotnet test --no-restore --verbosity normal
      env: 
        LOGIN: ${{ secrets.LOGIN }}
        PASSWORD: ${{ secrets.PASSWORD}}
        URL: ${{ secrets.URL}}            

How can I load the Settings when executing inside GitHub Actions?

1 Answers

Not sure if i'm not stating the obvious (you didn't mention how you start test app), but GitHub secrets != environment variables. They can be, if user wants that, but it's not done automagically.

- run: program.exe

In this case, program won't know login/password, as there's no way it can access this data.

- run: program.exe
  env:
   LOGIN:    ${{ secrets.LOGIN }}
   PASSWORD: ${{ secrets.PASSWORD }}

In this case, program will know login/password, as secrets are "converted" to environment variables, which application can see.

- run: program.exe --login=${{ secrets.LOGIN }} --password=${{ secrets.PASSWORD }}

In this case, program will know login/password, as secrets are passed via command line arguments, which application can see. Environment variables remain unchanged in this case.

Another option is to keep encrypted secrets.json in repository, and decrypt it when workflow is running; see docs for details.

Related