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
320 stars 53 forks source link

AzureStaticWebApp@0 - Unable to runtime variable for azure_static_web_apps_api_token #635

Open dbassking opened 2 years ago

dbassking commented 2 years ago

Unable to pass the deployment token as a parameter when using a runtime variable

for the tasks below, "./artifacts/build/" is a built reactjs site, and "./artifacts/api/" is a function app. When "deploymentToken" is defined as a library or pipeline variable the site deploys fine, but when it is retrieved from the resource on-the-fly using the azure cli, deployment fails with an "invalid token" error. The value of $(deploymentToken) is the same as the value provided when you manage tokens in the Azure Portal. I verified this in both the Azure CLI and by writing the value to the log during deployment.

            - task: AzureCLI@2
              displayName: Get Deployment Key Variable
              inputs:
                azureSubscription: ******
                scriptLocation: inlineScript
                scriptType: pscore
                inlineScript: |
                  $KEY= & az staticwebapp secrets list `
                  --name $(resource_group_name)_client `
                  --resource-group $(resource_group_name) `
                  --query properties.apiKey;  `
                  Write-Host "##vso[task.setvariable variable=deploymentToken;issecret=true]$KEY"

            - task: AzureStaticWebApp@0
              displayName: Deploy Public Client
              inputs:
                app_location: "./artifacts/build/"
                api_location: "./artifacts/api/"
                azure_static_web_apps_api_token: $(deploymentToken)
                output_location: ""
                skip_app_build: true

Expected behavior The token should be accepted and the deployment succeed, just as when using a compile time variable

Screenshots image

DieselMeister commented 2 years ago

use:

- task
   input: ...
   env:
     azure_static_web_Apps_api_token: $(deploymenttoken)

saw this in this article and it worked for me... at leat until I try to build a azure function with .net 6 :(

https://docs.microsoft.com/de-de/azure/static-web-apps/publish-devops

hope at leat it helps you!

dbassking commented 2 years ago

That's what I'm currently doing, unfortunately. Hopefully there is a way to do it without manually retrieving the token

DieselMeister commented 2 years ago

I want to mention the "env" in the config of the task was important. In your example you have the 'azure_static_web_Apps_api_token' in the input section! Don't know if that will work for you.

dbassking commented 2 years ago

it currently works when the token is in a pipeline variable. I'll move it to 'env' and see what happens. Good catch

tarunbhatt9784 commented 1 year ago

I am facing the same problem. Any solution for this?

scottfli03 commented 5 months ago

Just wanted to point out that DieselMeister has a capital "A" in his environment variable which caused his suggested solution to not work. Figure someone else reading through this may save themselves some time if they copy and pasted and didn't understand why the solution wasn't working for them. He is correct about env needing to be outside of inputs.

iqxrmike commented 2 months ago

In your inline script, pass -o tsv after --query properties.apiKey because otherwise, the output of az staticwebapp secrets list will include quotes around the key, which results in an invalid deployment token.