Azure / static-web-apps

Azure Static Web Apps. For bugs and feature requests, please create an issue in this repo. For community discussions, latest updates, kindly refer to the Discussions Tab. To know what's new in Static Web Apps, visit https://aka.ms/swa/ThisMonth
https://aka.ms/swa
MIT License
333 stars 57 forks source link

Blazor Web Assembly - Deploy Azure Static Web App (Preview) appsettings.json issue #886

Open ZavenLepedjian opened 2 years ago

ZavenLepedjian commented 2 years ago

I'm using Deploy Azure Static Web App (Preview) task in my Release pipeline and I'm facing issue by not being able to replace appsettings.json values.

Here are the steps I'm doing.

  1. In the build pipeline

    • NUGET restore
    • dotnet publish -c DEBUG
  2. Release pipeline

    • replace appsettings.json values with a custom values (Because of multitenant configuration).
    • Use Deploy Azure Static Web App (Preview) to deploy my Blazor WebAssembly app.
    • No Oryx build is being used since it has been already built.

Result:

Debug Results:

vivekjilla commented 2 years ago

@ZavenLepedjian Is it possible to share the release pipeline yml and if possible, also share a repro, so that we can understand the issue better?

ZavenLepedjian commented 2 years ago

I do not have the whole yaml file of the release as I'm using the classic release pipeline. What I have is the yaml of each task. Please find below the tasks in yaml in order

1- Setup the config file pf a custom tool that will generate my appsettings.json - task: jakkaj.vsts-yaml-writer.custom-build-release-task.YamlWriter@0 displayName: 'Update static web wettings' inputs: file: '$(System.DefaultWorkingDirectory)/_deployments/tools/StaticWebMultitenantConfig/appsettings.json' set: 'Tenants="$(Host)",ApiUrl="$(HostApi)",AuthorityUrl="$(AuthUrl)",InputSettings="$(System.DefaultWorkingDirectory)/_deployments/web/Client/wwwroot/appsettings.json",OutputFile="$(System.DefaultWorkingDirectory)/_deployments/web/Client/wwwroot/tenantsettings.json"' json: true

2- Run the custom tool to generate the settings and replace it

- script: 'dotnet StaticWebMultitenantConfig.dll'
  workingDirectory: '$(System.DefaultWorkingDirectory)/_deployments/tools/StaticWebMultitenantConfig'
  displayName: 'Set static web settings'

3- Deploy the built app using the Azure Static Web App (Preview) Task - task: AzureStaticWebApp@0 displayName: 'Deploy static web app' inputs: app_location: '_deployments/web/Client/wwwroot' config_file_location: '_deployments/web/Client/wwwroot' skip_app_build: true skip_api_build: true is_static_export: false verbose: true azure_static_web_apps_api_token: '$(StaticWebApi)'

After step 2, if I echo the "_deployments/web/Client/wwwroot/appsettings.json" file, I get the right configuration but after the third step the appsettings.json is empty

vivekjilla commented 2 years ago

@ZavenLepedjian Thanks for the tasks yaml. Not completely sure on what's going wrong here, but is it possible that the file generated in task 2 is not accessible by task 3? To verify that, in task 3, can you try removing the static web app related steps and add a simple command to access the generated appsettings.json and see if you can see the contents?

And if the issue is happening only with static web app task, is it possible to provide a repro which I can access or provide me with access so that I can understand the issue better?

ZavenLepedjian commented 2 years ago

@vivekjilla Thanks for the help. After trying to get the file content after task 2, I got permission denied. /home/vsts/work/_temp/0b45eaf9-a2f1-4266-8e5b-55544d98e394.sh: line 3: /home/vsts/work/r1/a/_deployments/web/Client/wwwroot/appsettings.json: Permission denied

This result was predicted by you.

Knowing that this is an hosted "ubuntu-latest" agent, How do you think we can tackle this?

vivekjilla commented 2 years ago

@ZavenLepedjian thanks for trying it out. Not sure what's happening here but seems like something to do with azure devops pipelines. One thing we can do is to try a minimal repro where one task writes contents to a file and the next task reads it. And maybe follow up with the azure devops team on this - https://github.com/MicrosoftDocs/azure-devops-docs/issues

Can you please try that and see.

ZavenLepedjian commented 2 years ago

@vivekjilla , After running chmod -R 777 . to give access on the file, I was able to read the content of the file. But after the static web task, the appsettings is still not filled with the data I was waiting for.

vivekjilla commented 2 years ago

@ZavenLepedjian Oh okay. Then, is it possible to give me access to your pipeline or provide an accessible repro, where I can look into further to get to the bottom of this? Thanks!

ZavenLepedjian commented 2 years ago

@vivekjilla it is very difficult for me to give you access to the pipeline as it is the company's release pipeline. maybe we can have a call and share screen so that you can look into it? Also, even though I tried using incognito but, do you think this might be a problem of caching? Maybe the appsettings has been updated but it has been cached?

vivekjilla commented 2 years ago

@ZavenLepedjian sure. Please send a mail to me at vijilla@microsoft.com , We can discuss further there. I can't think of a reason why this could be happening, at this point.

ZavenLepedjian commented 2 years ago

Thanks @vivekjilla , I will contact you soon via email

ZavenLepedjian commented 2 years ago

@vivekjilla I contacted you a week ago via email and didn't get any response.

vivekjilla commented 2 years ago

@ZavenLepedjian I'm really sorry about that but I didn't see any mail or can't seem to find. Can you please check again and also send me the subject I can search for.

ZavenLepedjian commented 2 years ago

@vivekjilla here's the subject of the email sent. Azure static web app appsettings.json file modification Hope to hear from you soon.

ZavenLepedjian commented 2 years ago

@vivekjilla do you have any updates?

vivekjilla commented 2 years ago

@ZavenLepedjian sorry, didn't get chance to look into this further yet.

But to summarize our last discussion, ado tasks updating an already existing file in the repo is not working for some reason and the updated changes are not reflected.

Workaround is to have a file with different name in the repo and let the task create a new file with name appsettings.json.

Eonasdan commented 1 year ago

Hello. I'm having the same issue. I have a full demo.

I unzip a build artifact for a Blazor WASM project and then copy an environment specific appsetings file to overwrite the wwwroot/appsettings.json. However, when everything completes, I can load the appsettings on the deployed app and still get the appsettings that was a part of the build steps.

Someone else also had this issue on SO

yaml bits ```yaml - task: ExtractFiles@1 displayName: 'Extract Web' inputs: archiveFilePatterns: '${{ parameters.buildDrop }}/BlazorDemo.Web.zip' destinationFolder: '${{ parameters.buildDrop }}/BlazorDemo.Web' - task: PowerShell@2 displayName: 'Copy Specific Config' inputs: pwsh: true targetType: 'inline' script: | $source = "${{ parameters.buildDrop }}/web-settings/appsettings.${{ parameters.environment }}.json" $destination = "${{ parameters.buildDrop }}/BlazorDemo.Web/wwwroot/appsettings.json" Copy-Item -Path $source -Destination $destination # I have verified that the destination files contents have changes correctly. - task: AzureCLI@2 displayName: 'Get Static Web token' inputs: azureSubscription: ${{ parameters.azureResourceManagerConnection }} scriptType: 'pscore' scriptLocation: 'inlineScript' inlineScript: | $token = (az staticwebapp secrets list -n $(staticSiteName) --query "properties.apiKey").replace('"','') Write-Host "##vso[task.setvariable variable=apiToken]$token" - task: AzureStaticWebApp@0 displayName: 'Static Web App' inputs: workingDirectory: '${{ parameters.buildDrop }}/BlazorDemo.Web' app_location: wwwroot skip_app_build: true skip_api_build: true is_static_export: false verbose: false azure_static_web_apps_api_token: $(apiToken) ```
Eonasdan commented 1 year ago

Hi @vivekjilla Was my demo helpful? This is a blocking issue for me on a real project (and the demo)

vivekjilla commented 1 year ago

@Eonasdan apologies for the delay. But can you try this work-around

Workaround is to have a file with different name in the repo and let the task create a new file with name appsettings.json.

Basically either don't keep the wwwroot/appsettings.json file in the repo or rename it to somethine else like wwwroot/_appsettings.json.

This way, your custom appsettings.json won't get replaced by the initial appsettings.json file during the deploy step.

And for us to investigate this further, is it also possible for you to share the deployment run logs. Thanks!

Eonasdan commented 1 year ago

Based on your workaround, what I ended up doing was deleting the wwwroot/appsettings.json file before doing a publish in my build pipeline. In the deploy pipeline, I copy the environment specific file into the wwwroot folder. This seemed to work for the demo, I'll have to try it in the real project when I can get to it.

  - task: PowerShell@2
    displayName: 'Delete Config'
    inputs:
      pwsh: true
      targetType: 'inline'
      script: |
        $destination = "BlazorDemo/BlazorDemo.Web/wwwroot/appsettings.json"
        Remove-Item -Path $destination -Force