Using GitHub secrets in .NET application code?

316 Views Asked by At

I am using GitHub actions to deploy a .NET core application to a Lambda function in AWS and am trying to retrieve secrets so that I can use them in the application.

I have two environments set up; staging and production. Each has a secret called MISC_KEY.

The following snippet of code is from the GitHub Actions workflow which sets the secret as an environment variable in the staging environment first...

deploy_staging:
    name: 'Deploy to Staging'
    environment: staging
    runs-on: ubuntu-latest

    steps:
    - name: 'Checkout repository'
      uses: actions/checkout@v3

    - name: 'Set Environment Secrets'
      run: echo "GitHubSecret=${{ secrets.MISC_KEY }}" >> $GITHUB_ENV

In my .NET application, I am trying to access this variable using Environment.GetEnvironmentVariable and then just pass this into the endpoint of the API...

// Get GitHub secret
string gitHubSecret = Environment.GetEnvironmentVariable("GitHubSecret");


app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.MapGet("/", () => $"The GitHub secret for this environment is {gitHubSecret}");

app.Run();

I am getting nothing returning, and am wondering if I am missing anything out in this process? Can GitHub secrets actually be used within the application code itself, or can they only be used as part of the GitHub Actions workflow?

1

There are 1 best solutions below

0
Chris On

As the application is deployed on AWS, further configuration is required to pass the variables to the deployment stage which has been missed out.