3

I want to use GitHub secrets inside the repository code. The repository is an asp.net core web app. I am deploying the app to the Azure app service using Github actions.

I have tried declaring env variable in the workflow like this

# Docs for the Azure Web Apps Deploy action: https://go.microsoft.com/fwlink/?linkid=2134798
# More GitHub Actions for Azure: https://go.microsoft.com/fwlink/?linkid=2135048

name: Azure App Service - TestJuzer(Production), Build and deploy DotnetCore app

on:
  push:
    branches:
      - master

env:
  TEST_STRING: ${{ secrets.TEST_STRING }}

jobs:
  build-and-deploy:
    runs-on: windows-latest
    
    steps:
    # checkout the repo
    - name: 'Checkout Github Action'
      uses: actions/checkout@master

    - name: Set up Node.js '12.x'
      uses: actions/setup-node@v1
      with:
        node-version: '12.x'

    - name: Set up .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: '5.0.x'

    - name: Build with dotnet
      run: dotnet build --configuration Release
      env:
        TEST_STRING: ${{ secrets.TEST_STRING }}

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

    - name: Run Azure webapp deploy action using publish profile credentials
      uses: azure/webapps-deploy@v2
      with:
        app-name: TestJuzer
        slot-name: Production
        publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_461CFB6B8D42419FA7F58944D621BA78 }}
        package: ${{env.DOTNET_ROOT}}/myapp
      env:
        TEST_STRING: ${{ secrets.TEST_STRING }}

and accessing the env variable in .net like this

Environment.GetEnvironmentVariable("TEST_STRING")

"TEST_STRING" is the name of the secret. But I am getting null.

I want to pass secret as an environment variable in the workflow and use it in the deployed app.

Any help appreciated Thanks

2 Answers 2

0

You set env variable on agent machine. And here you should rather set it on App Service. You could use Azure App Service Settings:

Here is sample workflow:

# .github/workflows/configureAppSettings.yml
on: [push]

jobs:
  build:
    runs-on: windows-latest
    steps:
    - uses: azure/login@v1
      with:
        creds: '${{ secrets.AZURE_CREDENTIALS }}'
    - uses: azure/appservice-settings@v1
      with:
        app-name: 'my-app'
        slot-name: 'staging'  # Optional and needed only if the settings have to be configured on the specific deployment slot
        app-settings-json: '${{ secrets.APP_SETTINGS }}' 
        connection-strings-json: '${{ secrets.CONNECTION_STRINGS }}'
        general-settings-json: '{"alwaysOn": "false", "webSocketsEnabled": "true"}' #'General configuration settings as Key Value pairs'
      id: settings
    - run: echo "The webapp-url is ${{ steps.settings.outputs.webapp-url }}"
    - run: |
        az logout

In you case it could be like:

    - name: Set Web App settings
      uses: Azure/appservice-settings@v1
      with:
       app-name: 'node-rnc'
       app-settings-json: |
         [
             {
                 "name": "TEST_STRING",
                 "value": "${{ secrets.TEST_STRING }}",
                 "slotSetting": false
             }
         ]
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I tried but still getting null. How to access this app-settings-json contents in .net core app?
I tried accessing it with IConfiguration. But that did not work as well. I did not know about Application Settings in the Azure App service. I am using that now as it serves the same purpose for me. For future reference on how to access app service settings in .net core learn.microsoft.com/en-us/azure/app-service/…
0

There is a github action step for setting config settings

https://github.com/Azure/appservice-settings

- uses: azure/login@v1
  with:
    creds: '${{ secrets.AZURE_CREDENTIALS }}'


- name: Azure App Service Settings
  uses: Azure/appservice-settings@v1
  with:
    app-name: ${{ env.AZURE_FUNCTIONAPP_NAME }}
    app-settings-json: |
    [
       {
          "name": "appSetting1",
          "value": "${{ vars.APP_SETTING_1 }}"",
          "slotSetting": false
        }
    ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.