Azure / Azure/static-web-apps

Unable to deploy static web app using GitHub Actions

Open
#788 7 comments 1 reaction 0 assignees View on GitHub
Dominant language
No language data
Stars
346
Forks
67
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**

Unable to deploy a Static Web App using GitHub Actions right after provisioning by ARM template.
The error was:
```
deployment_token was not provided.
The deployment_token is required for deploying content. If you'd like to continue the run without deployment, add the configuration skip_deploy_on_missing_secrets set to true in your workflow file
An unknown exception has occurred
```

**To Reproduce**
Steps to reproduce the behavior:
1. Create an ARM template
``` json
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"resourceName": {
"type": "string",
"metadata": {
"description": "The base name to use for creating resources"
}
},
"environment": {
"type": "string",
"defaultValue": "dev",
"allowedValues": [
"dev",
"inte",
"prep",
"prod"
]
},
"location": {
"type": "string",
"metadata": {
"description": "The location of static forms."
}
},
"repositoryUrl": {
"type": "string",
"metadata": {
"description": "URL for the repository of the static forms."
}
},
"githubToken": {
"type": "string",
"metadata": {
"description": "A user's github repository token which is used to setup the Github Actions workflow file and API secrets."
}
},
"deploymentTokenName":{
"type": "string",
"metadata": {
"description": "Name of a deployment token which will be created as a secret in GitHub."
}
}
},
"variables": {
"baseName": "[concat(parameters('resourceName'), parameters('environment'))]",
"staticWebAppName": "[toLower(concat(variables('baseName'), '-', 'swa'))]",
"staticWebAppResourceId": "[resourceId('Microsoft.Web/staticSites', variables('staticWebAppName'))]"
},
"resources": [
{
"type": "Microsoft.Web/staticSites",
"apiVersion": "2021-01-01",
"name": "[variables('staticWebAppName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard",
"tier": "Standard"
},
"properties": {
"repositoryUrl": "[parameters('repositoryUrl')]",
"branch": "main",
"stagingEnvironmentPolicy": "Enabled",
"allowConfigFileUpdates": true,
"provider": "GitHub",
"repositoryToken": "[parameters('githubToken')]",
"buildProperties":{
"appLocation": "/",
"skipGithubActionWorkflowGeneration": true,
"githubActionSecretNameOverride": "[parameters('deploymentTokenName')]"
}
}
}
],
"outputs": {
"deploymentToken": {
"type": "string",
"value": "[listSecrets(variables('staticWebAppResourceId'), '2019-08-01').properties.apiKey]"
}
}
}
```
2. Create a secret TOKEN_TO_DEPLOY_STATIC_WEB_APP which has access to the current repo.
3. Create YAML file to deploy.
``` yml
name: Static Web App Deployment

on:
workflow_dispatch:
inputs:
resourceName:
type: string
description: The base name to use for creating resources
required: true

environment:
type: choice
description: The abbreviation of a environment.
required: false
default: dev
options:
- dev
- inte
- prep
- prod

location:
type: choice
description: The location of the logic app and related resources.
required: false
default: westeurope
options:
- westeurope
- eastus2
- westus2
- centralus
- eastasia

jobs:
PrepareSecrets:
name: Preparing secrets
runs-on: windows-latest

steps:
- name: Prepare secrets
id: prepare-secrets
shell: powershell
run: |
if ('${{ github.event.inputs.environment }}' -eq 'prod') {
Write-Output "::set-output name=azureCredentialName::AZURE_CREDENTIALS"
}
else {
Write-Output "::set-output name=azureCredentialName::AZURE_CREDENTIALS_DEV"
}

$deploymentTokenName = "STATIC_WEB_APP_DEPLOYMENT_TOKEN_" + "${{ github.event.inputs.resourceName }}".ToUpper() + "${{ github.event.inputs.environment }}".ToUpper()
$resourceGroupName = "${{ github.event.inputs.resourceName }}" + "${{ github.event.inputs.environment }}"

Write-Output "::set-output name=deploymentTokenName::$deploymentTokenName"
Write-Output "::set-output name=resourceGroupName::$resourceGroupName"

outputs:
azureCredentialName: ${{ steps.prepare-secrets.outputs.azureCredentialName }}
deploymentTokenName: ${{ steps.prepare-secrets.outputs.deploymentTokenName }}
resourceGroupName: ${{ steps.prepare-secrets.outputs.resourceGroupName }}

Deploy:
name: Deploy static web app
runs-on: ubuntu-latest
needs: [PrepareSecrets]
if: |
always() && needs.PrepareSecrets.result == 'success'
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
submodules: true

- name: Azure login
uses: Azure/login@v1.4.0
with:
creds: ${{ secrets[needs.PrepareSecrets.outputs.azureCredentialName] }}
enable-AzPSSession: true

- name: Create resource group
uses: Azure/CLI@v1
with:
azcliversion: 2.30.0
inlineScript: |
az group create --location ${{ github.event.inputs.location }} --name ${{ needs.PrepareSecrets.outputs.resourceGroupName }}

- name: Deploy ARM template
id: deployArmTemplate
uses: azure/arm-deploy@v1
with:
resourceGroupName: ${{ needs.PrepareSecrets.outputs.resourceGroupName }}
template: ./static-forms.json
parameters:
resourceName=${{ github.event.inputs.resourceName }}
environment=${{ github.event.inputs.environment }}
location=${{ github.event.inputs.location }}
repositoryUrl=
githubToken=${{ secrets.TOKEN_TO_DEPLOY_STATIC_WEB_APP }}
deploymentTokenName=${{ needs.PrepareSecrets.outputs.deploymentTokenName }}

- name: Print out deployment token
run: |
echo ${{ secrets[needs.PrepareSecrets.outputs.deploymentTokenName] }} | sed 's/./& /g'

- name: Deploy Azure static web app
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets[needs.PrepareSecrets.outputs.deploymentTokenName] }}
repo_token: ${{ secrets.TOKEN_TO_DEPLOY_STATIC_WEB_APP }}
action: "upload"
app_location: "/"
```
4. Start the GitHub Action manually.

The error always shows up on the first attempt. However, the issue does not happen if we run it again.

**Expected behavior**
The deployment should work without any issue.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.