How do I get a GitHub secret into an Environment Variable into my C# application?

265 views Asked by At

If I set environment variables locally on my computer, this program shows them correctly. If I build and deploy using a GitHub workflow and push them to Azure, the GitHub secret with the name MYSECRET does not get picked up with the GetEnvironmentalVariable method call. My understanding is that it should. Am I missing something?

'

@page "/"

@using System;

<PageTitle>Index</PageTitle>

<h1>Value of Secret (User) is '@myUserSecret'</h1>
<h1>Value of Secret (Machine) is '@myMachineSecret'</h1>
<h1>Value of Secret (Process) is '@myProcessSecret'</h1>

@code{   
    // Was set locally on computer as aaa
    private string myUserSecret = Environment.GetEnvironmentVariable("MYSECRET", EnvironmentVariableTarget.User) ?? "";

    // Was set locally on computer as bbb
    private string myMachineSecret = Environment.GetEnvironmentVariable("MYSECRET", EnvironmentVariableTarget.Machine) ?? "";

    // Was set in method OnInitialized as ccc
    private string myProcessSecret = Environment.GetEnvironmentVariable("MYSECRET", EnvironmentVariableTarget.Process) ?? "";

    // On GitHub, a secret with the value of ddd

    protected override void OnInitialized()
    {
        Environment.SetEnvironmentVariable(variable: "MYSECRET", value: "ccc");
    }
}

`

name: Working with environmental variables

on:
  workflow_dispatch:

env:
  my_secret: ${{ secrets.MYSECRET }}

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Show secret
        run: echo '${{ secrets.MYSECRET }}'

      - name: Show secret as env variablee
        run: echo '${{ env.my_secret }}'

      - name: Checkout .NET
        uses: actions/checkout@v3
      
      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: '7.x'
          include-preleases: false

      - name: Build with .NET
        run: dotnet build --configuration Release

      - name: Publish
        run: dotnet publish -c Release -o ${{ env.DOTNET_ROOT }}/myapp

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v2
        with:
          name: .net-app
          path: ${{ env.DOTNET_ROOT }}/myapp

  publish:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: Production
      url: ${{ steps.deploy-to-web-app.output.webapp-url }}

    steps:
      - name: Show url
        run: echo '${{ env.url }}'

      - name: Download artifact from build job
        uses: actions/download-artifact@v2
        with:
          name: .net-app

      - name: Deploy to Azure Web App
        id: deploy-to-web-app
        uses: azure/webapps-deploy@v2
        with:
          app-name: 'PhillipsEngineerTelirite'
          slot-name: Production
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_03557B670BF14D93BD6244DC95264F2C }}
          package: .

I tried using the GitHub documentation, ChatGPT, and several YouTube videos.

0

There are 0 answers