Search code examples
xmlconnection-stringgithub-actions

Github action variable substitution for xml connectionstring replacement


I'm trying to replace the connectionString in an App.Config XML file for a WPF app based on prod/qa/dev environments. I'm trying to use the github actions variable substitution (https://github.com/marketplace/actions/variable-substitution) to do it.

The goal is to replace the following section with alternative sources:

        <add name="DefaultConnection"
             connectionString="Data Source=(LocalDb)\\MSDB;DbFilename=aspcore-local.mdf;" />

I was able to successfully change a simple json file, but how can this section be replaced in an XML file?

    - name: variable substitution in json
      uses: microsoft/variable-substitution@v1 
      with:
        files: 'test.xml'
      env:
        configuration.connectionStrings.DefaultConnection.connectionString: "test" ????
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <connectionStrings>
            <add name="DefaultConnection" connectionString="replaceme!"/>
        </connectionStrings>
    </configuration>

I initially thought to transform the file using https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/file-transform?view=azure-devops but that's only available in Azure DevOps vs. GitHub actions.

I know there is a better way, I just don't know what it is... thanks!


Solution

  • I wound up using a combination of regex (awesome generator), search and replace strings, and XML path search/replace for connection strings, appx settings, version, etc. I used a channel variable (prod, beta, etc.) in combination with if: statement to apply the necessary changes.

       - name: change update location for dev
          run: |
           $autoupdate = "<AppInstallerUri>\\\\([a-zA-Z]+(\\[a-zA-Z]+)+)</AppInstallerUri>"
           $devautoupdate = "<AppInstallerUri>c:\temp</AppInstallerUri>"
           $Path = "D:\a\repo..wapproj" #specify location of checked out WAP project file
           (Get-Content -Path $Path -Raw) -replace $autoupdate, $devautoupdate | Out-File $Path
          if: ${{ inputs.channel == 'dev'}}    
    
        - name: App.config change statements for prod
          run: |
           (Get-Content -Path D:\a\.....repo-project location of App.config -Raw) -replace '<connectionStrings configSource="ConnectionStrings.dev.config">','<connectionStrings configSource="ConnectionStrings.prod.config">' | Set-Content -Path D:\a\.....repo-project location of App.config -Raw) -replace '<appSettings configSource="AppSettings.dev.config" />','<appSettings configSource="AppSettings.prod.config" />' 
          if: ${{ inputs.channel == 'prod'}}
    
    
        - name: assign prod variables
          run: |
            echo "MsixPackageId=MyApp.Prod" >> $env:GITHUB_ENV
            echo "MsixPublisherId=CN=Acme Inc, O=Acme, L=Mars, S=Milky Way, C=Universe" >> $env:GITHUB_ENV
            echo "MsixPackageDisplayName=MyApp (Prod)" >> $env:GITHUB_ENV
          if: ${{ inputs.channel == 'prod'}}
          
    
        - name: Update manifest version
          run: |
            [xml]$manifest = get-content ".\$env:Wap_Project_Directory\Package.appxmanifest"
            $manifest.Package.Identity.Version = "${{ env.Ver }}"
            $manifest.Package.Identity.Name = "${{ env.MsixPackageId }}"
            $manifest.Package.Identity.Publisher = "${{ env.MsixPublisherId }}"
            $manifest.Package.Properties.DisplayName = "${{ env.MsixPackageDisplayName }}"
            $manifest.Package.Applications.Application.VisualElements.DisplayName = "${{ env.MsixPackageDisplayName }}"
            $manifest.save(".\$env:Wap_Project_Directory\Package.appxmanifest")