Share via

Azure Static web app deployment deploys wrong files

Marco Papst 31 Reputation points
2023-04-27T10:43:29.2366667+00:00

Hi,
I am trying to deploy a static webapp (Blazor WASM) using Azure Pipelines.

I have the packaged application as a pipeline artifact from a previous stage in my deployment pipeline. The basic steps I am doing is:

  • Download the artifact
  • Extract the zip into an empty directory
  • Get the deployment Token using AzureCLI Task
  • Deploy the application

This works as expected. Nevertheless, the current requirements brought me to the point that I need to modify the appsettings.json before deploying with some details for the environment in which the application is deployed.

To achieve that I added a AzureCLI Task after the zip extraction task in my pipeline, which modifies the appsettings.json in the local directory on the agent. This seems to work find (For debugging purposes I added a step after the retrieval of the deployment token, to display the contents of the appsettings.json file - it is modified here).

Unfortunately in the deployed web app, I always find the original appsettings.json and I am unable to figure out why and where the pipeline gets that one from.


steps:
  - task: DownloadBuildArtifacts@0
    displayName: Download Build Artifact
    inputs:
      buildType: 'current'
      downloadType: 'single'
      artifactName: '${{ parameters.artifactName }}'
      downloadPath: '${{ parameters.workingDirectory }}'
  - task: ExtractFiles@1
    displayName: Extract Artifact
    inputs:
      archiveFilePatterns: '${{ parameters.workingDirectory }}/**/*.zip'
      destinationFolder: '${{ parameters.workingDirectory }}/.deploy/${{ parameters.artifactName }}'
      cleanDestinationFolder: true
      overwriteExistingFiles: true

 - task: AzureCLI@2
   displayName: Configure appsettings.json
   inputs:
     azureSubscription: ${{ parameters.subscriptionName }}
     scriptType: 'pscore'
     scriptLocation: 'inlineScript'
     inlineScript: |
       az config set extension.use_dynamic_install=yes_without_prompt
       az extension add --name containerapp
 
       $path = Join-Path '${{ parameters.workingDirectory }}' '.deploy' '${{ parameters.artifactName }}' 'wwwroot' 'appsettings.json'
       $file = Get-Item $path
       if (!$file.exists)
       {
           Write-Host 'appsettings file does not exist!'
       }
       else
       {
           $appenv = az containerapp env show -n ${{ parameters.environment }}-${{ parameters.region }} -g rg-${{ parameters.environment }}-${{ parameters.region }}-backend | ConvertFrom-Json
           $appsettings = Get-Content $file | ConvertFrom-Json
 
           # set values
            Write-Host "Setting Environment.Name to ${{ parameters.environment }}"
           $appsettings.Environment.Name = '${{ parameters.environment }}'
           Write-Host "Environment.Name = $($appsettings.Environment.Name)"
 
           Write-Host "Setting Environment.Region to ${{ parameters.region }}"
           $appsettings.Environment.Region = '${{ parameters.region }}'
           Write-Host "Environment.Region = $($appsettings.Environment.Region)"
 
           Write-Host "Setting Environment.Backend to $($appenv.properties.defaultDomain)"
           $appsettings.Environment.Backend = $appenv.properties.defaultDomain
           Write-Host "Environment.Backend = $($appsettings.Environment.Backend)"
 
           $json = $appsettings | ConvertTo-Json
           Write-Host $json
           Set-Content -Path $file -Value $json
 
           Get-Content $file | Write-Host
       }

- task: AzureCLI@2
  displayName: Retrieve Deployment Token
  inputs:
    azureSubscription: ${{ parameters.subscriptionName }}
    scriptType: 'pscore'
    scriptLocation: 'inlineScript'
    inlineScript: |
      $key = (az staticwebapp secrets list -n ${{ parameters.webAppName }} --query "properties.apiKey").Replace('"', "")
      Write-Host "##vso[task.setvariable variable=deploymentToken]$key"
 
- task: AzureStaticWebApp@0
  displayName: 'Deploy WebApp ${{ parameters.artifactName }} to ${{ parameters.webAppName }}'
  inputs:
    app_location: '${{ parameters.workingDirectory }}/.deploy/${{ parameters.artifactName }}/wwwroot'
    skip_api_build: true
    azure_static_web_apps_api_token: $(deploymentToken)
Azure Static Web Apps
Azure Static Web Apps

An Azure service that provides streamlined full-stack web app development.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Marco Papst 31 Reputation points
    2023-04-28T07:14:07.1933333+00:00

    Figured out that this is not a Static Web App issue.

    When publishing a Blazor WASM App, the static files are compressed as GZip and Brotli in addition to the plain json file. SWA then delivers directly the brotli compressed file.

    My solution then was: do the changes, compress it as br and gz and store it also.

    Additonal noted: it can also be done during publish or use the built in environment functionality from AspNetCore.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.