azureazure-devopsazure-static-web-appnx-monorepo

Azure DevOps: Deploy static web app from script (not task)


Context: Monorepository (Nx) with multiple applications

I have a Azure DevOps pipeline in which I want to deploy a list of applications under certain conditions. Because I need to run some commands from nx, the list of apps is available only at runtime and not when the pipeline is compiled, so I can't use a for loop something like ${{ each app in variables.apps }} that calls a AzureStaticWebApp@0 task.

For this reason I built the following script:

- task: AzureCLI@2
  displayName: 'Retrieve secret'
  inputs:
    azureSubscription: ${{ parameters.ArmConnection }}
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      for app in $apps; do
         # Check if the deployment-config.json exists for the app
         configPath="apps/$app/deployment-configs.json"
         if [ -f "$configPath" ]; then
            appName=$(jq -r '.appName' "$configPath") # Retrieve app name from config file
            deploymentToken=$(az staticwebapp secrets list --name "$appName" --query "properties.apiKey")
            # Deploy app with the deployment token (from dist/apps/$app)
         fi
      done

I notice there is a static web app CLI swa that's separate from the azure CLI, but I think it is supposed to be used locally. How am I supposed to deploy the app from the script


Solution

  • Update

    Just in case your current pipeline may incur the deployment_token provided was invalid exception, here is my sample Azure CLI script for your reference. Kindly remember to add the -o tsv argument for your az staticwebapp secrets list command, which can remove the double quotation marks from the output $deploymentToken.

    enter image description here

    - task: AzureCLI@2
      inputs:
        azureSubscription: 'ARMSvcCnnWIFAutoSub1'
        scriptType: 'bash'
        scriptLocation: 'inlineScript'
        inlineScript: |
          npm install -g @azure/static-web-apps-cli
          swa --version
    
          appName="xxxMyStaticWebAppxxx"
          # Add -o tsv to output a valid token without double quotation marks
          deploymentToken=$(az staticwebapp secrets list --name "$appName" --query "properties.apiKey" -o tsv)
    
          # Check if the token is valid -> Comment out to avoid token leakage -> Consider resetting token after deployment
          # echo "deploymentToken for $appName - $deploymentToken"
          
          swa deploy ./src --deployment-token $deploymentToken --env ${{ parameters.target_environment }}
    

    It seemed that your pipeline was running on Microsoft-hosted agents, where SWA CLI was not installed by default.

    You can use a script to install SWA CLI before running swa deploy command for deployment.

    - script: |
        npm install -g @azure/static-web-apps-cli
        swa --version
    

    You may also configure your local machine/VM/VMSS/MDP with the capabilities of Azure CLI and SWA CLI as a self-hosted agent for your pipeline to run on.