From 837e8364bcc3baff5e73d2f4f6bada9e35c7d05b Mon Sep 17 00:00:00 2001 From: Mike Surowiec Date: Tue, 25 Jan 2022 12:41:42 -0600 Subject: [PATCH] Improve Azure staging deployment concurrency (#24625) * Improve azure staging deployment concurrency * update workflow-lint to ignore undeploy --- .../workflows/staging-build-and-deploy-azure.yml | 10 +++++++++- .github/workflows/staging-undeploy-azure.yml | 14 +++++++++++--- .github/workflows/workflow-lint.yml | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/.github/workflows/staging-build-and-deploy-azure.yml b/.github/workflows/staging-build-and-deploy-azure.yml index f781a743c9..5c802112e8 100644 --- a/.github/workflows/staging-build-and-deploy-azure.yml +++ b/.github/workflows/staging-build-and-deploy-azure.yml @@ -38,6 +38,7 @@ jobs: url: ${{ steps.deploy.outputs.defaultHostName }} env: GITHUB_EVENT_NUMBER: ${{ github.event.number }} + RESOURCE_GROUP: docs-nonprod NONPROD_REGISTRY_USERNAME: ghdocs # Image tag is unique to each workflow run so that it always triggers a new deployment DOCKER_IMAGE: ${{ secrets.NONPROD_REGISTRY_SERVER }}/${{ github.repository }}/pr-${{ github.event.number }}:${{ github.event.pull_request.head.sha }}-${{ github.run_number }}-${{ github.run_attempt }} @@ -48,6 +49,7 @@ jobs: run: | REPO_NAME=${GITHUB_REPOSITORY#*\/} echo "REPO_NAME=${REPO_NAME}" >> $GITHUB_ENV + echo "DEPLOYMENT_NAME=${REPO_NAME}-pr-${GITHUB_EVENT_NUMBER}" >> $GITHUB_ENV echo "APP_NAME=gh${REPO_NAME}-staging-${GITHUB_EVENT_NUMBER}" >> $GITHUB_ENV - name: 'Az CLI login' @@ -99,15 +101,21 @@ jobs: cache-from: type=gha cache-to: type=gha,mode=max + # Succeed despite any non-zero exit code (e.g. if there is no deployment to cancel) + - name: 'Cancel any existing deployments for this PR' + run: | + az deployment group cancel --name ${{ env.DEPLOYMENT_NAME }} -g ${{ env.RESOURCE_GROUP }} || true + # Deploy ARM template is idempotent # Note: once the resources exist the image tag must change for a new deployment to occur (the image tag includes workflow run number, run attempt, as well as sha) - name: Run ARM deploy id: deploy uses: azure/arm-deploy@841b12551939c88af8f6df767c24c38a5620fd0d with: - resourceGroupName: docs-nonprod + resourceGroupName: ${{ env.RESOURCE_GROUP }} subscriptionId: ${{ secrets.NONPROD_SUBSCRIPTION_ID }} template: ./staging-azure-deploy-template.json + deploymentName: ${{ env.DEPLOYMENT_NAME }} parameters: appName="${{ env.APP_NAME }}" location="East US" linuxFxVersion="DOCKER|${{ env.DOCKER_IMAGE }}" diff --git a/.github/workflows/staging-undeploy-azure.yml b/.github/workflows/staging-undeploy-azure.yml index 3b4f9a8efb..cadfdbb449 100644 --- a/.github/workflows/staging-undeploy-azure.yml +++ b/.github/workflows/staging-undeploy-azure.yml @@ -17,6 +17,8 @@ jobs: timeout-minutes: 5 env: GITHUB_EVENT_NUMBER: ${{ github.event.number }} + RESOURCE_GROUP: docs-nonprod + NONPROD_REGISTRY_NAME: ghdocs IMAGE_REPO: ${{ github.repository }}/pr-${{ github.event.number }} steps: @@ -24,6 +26,7 @@ jobs: id: vars run: | REPO_NAME=${GITHUB_REPOSITORY#*\/} + echo "DEPLOYMENT_NAME=${REPO_NAME}-pr-${GITHUB_EVENT_NUMBER}" >> $GITHUB_ENV echo "APP_NAME=gh${REPO_NAME}-staging-${GITHUB_EVENT_NUMBER}" >> $GITHUB_ENV - name: 'Az CLI login' @@ -31,19 +34,24 @@ jobs: with: creds: ${{ secrets.NONPROD_AZURE_CREDENTIALS }} + # Succeed despite any non-zero exit code (e.g. if there is no deployment to cancel) + - name: 'Cancel any in progress deployments' + run: | + az deployment group cancel --name ${{ env.DEPLOYMENT_NAME }} -g ${{ env.RESOURCE_GROUP }} || true + # Delete web app (which will also delete the App Service plan) # This will succeed even if the app doesn't exist / has already been deleted - name: 'Delete App Service App (which will also delete the App Service plan)' run: | - az webapp delete -n $APP_NAME -g docs-nonprod + az webapp delete -n ${{ env.APP_NAME }} -g ${{ env.RESOURCE_GROUP }} # Untag all images under this PR's container registry repo - the container registry will automatically remove untagged images. # This will fail if the IMAGE_REPO doesn't exist, but we don't care - name: 'Untag all docker images for this PR' run: | - az acr repository delete -n ghdocs --repository ${{ env.IMAGE_REPO }} -y || true + az acr repository delete -n ${{ env.NONPROD_REGISTRY_NAME }} --repository ${{ env.IMAGE_REPO }} -y || true - # Remove all deployments from this environment and remove the environment + # Remove all GitHub deployments from this environment and remove the environment - uses: strumwolf/delete-deployment-environment@45c821e46baa405e25410700fe2e9643929706a0 with: token: ${{ secrets.DOCUBOT_REPO_PAT }} diff --git a/.github/workflows/workflow-lint.yml b/.github/workflows/workflow-lint.yml index a0eaf2de10..7746da544b 100644 --- a/.github/workflows/workflow-lint.yml +++ b/.github/workflows/workflow-lint.yml @@ -30,4 +30,4 @@ jobs: - name: Run linter uses: cschleiden/actions-linter@caffd707beda4fc6083926a3dff48444bc7c24aa with: - workflows: '[".github/workflows/*.yml", ".github/workflows/*.yaml", "!.github/workflows/remove-from-fr-board.yaml", "!.github/workflows/staging-deploy-pr.yml", "!.github/workflows/triage-issue-comments.yml", "!.github/workflows/staging-build-and-deploy-azure.yml"]' + workflows: '[".github/workflows/*.yml", ".github/workflows/*.yaml", "!.github/workflows/remove-from-fr-board.yaml", "!.github/workflows/staging-deploy-pr.yml", "!.github/workflows/triage-issue-comments.yml", "!.github/workflows/staging-build-and-deploy-azure.yml", "!.github/workflows/staging-undeploy-azure.yml"]'