30

I am trying to deploy an app with github actions. I linked my azure account to my github repository and the following actions has been created:

name: Build and deploy ASP.Net Core app to Azure Web App - my_app_name

on:
  push:
    branches:
      - master

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@master

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

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

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

    - name: Deploy to Azure Web App
      uses: azure/webapps-deploy@v1
      with:
        app-name: 'my_app_name'
        slot-name: 'production'
        publish-profile: ${{ secrets.AzureAppService_PublishProfile_xxxxxx }}
        package: ${{env.DOTNET_ROOT}}/myapp 

I have some variables in my appsettings.json file that I want to overwrite, but I don't find how to do it.

2
  • Why not change it before deploy? Commented Mar 5, 2020 at 10:58
  • 6
    Because it contains a secret key Commented Mar 9, 2020 at 18:38

1 Answer 1

74
Answer recommended by Microsoft Azure Collective

You may add the following action prior to deploying the artifacts to azure.

You can specify multiple files and it is supported with wildcard entries too.

The environment variable key must be specified with dot separated heirarchy.

#substitute production appsettings entries to appsettings json file
- name: App Settings Variable Substitution
  uses: microsoft/variable-substitution@v1
  with:
    files: '${{env.DOTNET_ROOT}}/myapp/appsettings.json'
  env:
    ConnectionStrings.Default: ${{ secrets.SOME_CONNECTION_STRING }}
    App.ServerRootAddress: ${{ env.SERVER_ROOT_ADDRESS }}

The above action can be used for xml and yaml file changes too.

Sign up to request clarification or add additional context in comments.

11 Comments

This answer should be ranked higher, and works very well. Particularly with AppSettings values (use AppSettings.MySecret, instead of AppSettings:MySecret). Thank you!
How do we change the attribute of an xml element? Ex: <rule name="ToWwwHttps" enabled="false" stopProcessing="true"> I need to set enabled="true". Thanks for your reply.
Secrets in Github er case sensitive and they seem to be saved in upper case. You have to have it like this: ${{ secrets.MYCONNSTR }}
For some reason I could only get it to find my file by specifying '**/appsettings.json', including any other part of the older structure caused a file not found error
I have my JSON property to replace with spaces and colon in the name, like { "MongoDB:connection string": "replace in Octopus" }. How can I use it in the env? There is a way to escape spaces and/or colon?
|

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.