diff --git a/.github/workflows/staging-build-and-deploy-azure.yml b/.github/workflows/azure-preview-env-deploy.yml similarity index 90% rename from .github/workflows/staging-build-and-deploy-azure.yml rename to .github/workflows/azure-preview-env-deploy.yml index 58de824acd..f8233025ee 100644 --- a/.github/workflows/staging-build-and-deploy-azure.yml +++ b/.github/workflows/azure-preview-env-deploy.yml @@ -1,7 +1,7 @@ -name: Staging - Azure Deploy App Service +name: Deploy Azure Preview Environment -# **What it does**: Build and deploy staging PRs to Azure -# **Why we have it**: It's our new staging deployment mechanism, only applicable to docs-internal +# **What it does**: Build and deploy to an Azure preview environment +# **Why we have it**: It's our preview environment deploy mechanism, only applicable to docs-internal # **Who does it impact**: All contributors. # This whole workflow is only guaranteed to be secure in the *private @@ -28,18 +28,19 @@ concurrency: cancel-in-progress: true jobs: - build-and-deploy-staging-azure: + build-and-deploy-azure-preview: if: ${{ github.repository == 'github/docs-internal' }} - name: Build and deploy image to staging App Service + name: Build and deploy image to Azure runs-on: ubuntu-latest timeout-minutes: 15 environment: - name: staging-pr-${{ github.event.number }} + name: preview-env-${{ github.event.number }} url: ${{ steps.deploy.outputs.defaultHostName }} env: GITHUB_EVENT_NUMBER: ${{ github.event.number }} - STAGING_RESOURCE_GROUPS: 4 + PREVIEW_ENV_RESOURCE_GROUPS: 4 NONPROD_REGISTRY_USERNAME: ghdocs + APP_LOCATION: eastus ENABLE_EARLY_ACCESS: ${{ github.repository == 'github/docs-internal' }} # 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 }} @@ -51,8 +52,8 @@ jobs: REPO_NAME=${GITHUB_REPOSITORY#*\/} echo "REPO_NAME=${REPO_NAME}" >> $GITHUB_ENV echo "DEPLOYMENT_NAME=${REPO_NAME}-pr-${GITHUB_EVENT_NUMBER}" >> $GITHUB_ENV - echo "RESOURCE_GROUP=preview-env-${REPO_NAME}-$((${GITHUB_EVENT_NUMBER} % ${STAGING_RESOURCE_GROUPS}))" >> $GITHUB_ENV - echo "APP_NAME=gh${REPO_NAME}-staging-${GITHUB_EVENT_NUMBER}" >> $GITHUB_ENV + echo "RESOURCE_GROUP=preview-env-${REPO_NAME}-$((${GITHUB_EVENT_NUMBER} % ${PREVIEW_ENV_RESOURCE_GROUPS}))" >> $GITHUB_ENV + echo "APP_NAME=${REPO_NAME}-preview-${GITHUB_EVENT_NUMBER}" >> $GITHUB_ENV - name: 'Az CLI login' uses: azure/login@1f63701bf3e6892515f1b7ce2d2bf1708b46beaf @@ -152,10 +153,10 @@ jobs: with: resourceGroupName: ${{ env.RESOURCE_GROUP }} subscriptionId: ${{ secrets.NONPROD_SUBSCRIPTION_ID }} - template: ./staging-azure-deploy-template.json + template: ./azure-preview-env-template.json deploymentName: ${{ env.DEPLOYMENT_NAME }} parameters: appName="${{ env.APP_NAME }}" - location="East US" + location="${{ env.APP_LOCATION }}" linuxFxVersion="DOCKER|${{ env.DOCKER_IMAGE }}" dockerRegistryUrl="https://${{ secrets.NONPROD_REGISTRY_SERVER }}" dockerRegistryUsername="${{ env.NONPROD_REGISTRY_USERNAME }}" diff --git a/.github/workflows/staging-undeploy-azure.yml b/.github/workflows/azure-preview-env-destroy.yml similarity index 67% rename from .github/workflows/staging-undeploy-azure.yml rename to .github/workflows/azure-preview-env-destroy.yml index ad35c9ca2f..8f41651998 100644 --- a/.github/workflows/staging-undeploy-azure.yml +++ b/.github/workflows/azure-preview-env-destroy.yml @@ -1,7 +1,7 @@ -name: Staging - Undeploy Azure PR +name: Destroy Azure Preview Env -# **What it does**: To undeploy PRs from Azure staging environment, i.e. destroy the App and associated resources. -# **Why we have it**: To save money spent on deployments for closed PRs. +# **What it does**: Destroys resources associated with a PRs Azure preview environment +# **Why we have it**: Closed PRs don't need apps # **Who does it impact**: All contributors. on: @@ -11,13 +11,13 @@ on: - locked jobs: - undeploy: - name: Undeploy + destory-azure-preview-env: + name: Destroy runs-on: ubuntu-latest timeout-minutes: 5 env: GITHUB_EVENT_NUMBER: ${{ github.event.number }} - STAGING_RESOURCE_GROUPS: 4 + PREVIEW_ENV_RESOURCE_GROUPS: 4 NONPROD_REGISTRY_NAME: ghdocs IMAGE_REPO: ${{ github.repository }}/pr-${{ github.event.number }} @@ -26,9 +26,9 @@ jobs: id: vars run: | REPO_NAME=${GITHUB_REPOSITORY#*\/} - echo "RESOURCE_GROUP=preview-env-${REPO_NAME}-$((${GITHUB_EVENT_NUMBER} % ${STAGING_RESOURCE_GROUPS}))" >> $GITHUB_ENV + echo "RESOURCE_GROUP=preview-env-${REPO_NAME}-$((${GITHUB_EVENT_NUMBER} % ${PREVIEW_ENV_RESOURCE_GROUPS}))" >> $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 + echo "APP_NAME=${REPO_NAME}-preview-${GITHUB_EVENT_NUMBER}" >> $GITHUB_ENV - name: 'Az CLI login' uses: azure/login@1f63701bf3e6892515f1b7ce2d2bf1708b46beaf @@ -40,11 +40,18 @@ jobs: run: | az deployment group cancel --name ${{ env.DEPLOYMENT_NAME }} -g ${{ env.RESOURCE_GROUP }} || true + # The full app name is obfuscated by an identifier, so we need to query to find the one for this PR + - name: 'Get full app name' + id: full-app-name + run: | + FULL_APP_NAME=$(az webapp list -g ${{ env.RESOURCE_GROUP }} --query "[?tags.DocsAppName == '${{ env.APP_NAME }}'})].name | [0]") + echo "::set-output name=result::${FULL_APP_NAME}" + # 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 ${{ env.APP_NAME }} -g ${{ env.RESOURCE_GROUP }} + az webapp delete -n ${{ steps.full-app-name.result }} -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 @@ -56,4 +63,4 @@ jobs: - uses: strumwolf/delete-deployment-environment@45c821e46baa405e25410700fe2e9643929706a0 with: token: ${{ secrets.DOCUBOT_REPO_PAT }} - environment: staging-pr-${{ github.event.number }} + environment: preview-env-${{ github.event.number }} diff --git a/.github/workflows/workflow-lint.yml b/.github/workflows/workflow-lint.yml index 7746da544b..698aa0f733 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", "!.github/workflows/staging-undeploy-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/azure-preview-env-deploy.yml", "!.github/workflows/azure-preview-env-destroy.yml"]' diff --git a/staging-azure-deploy-template.json b/azure-preview-env-template.json similarity index 87% rename from staging-azure-deploy-template.json rename to azure-preview-env-template.json index b99cc79cd2..32dbd849fa 100644 --- a/staging-azure-deploy-template.json +++ b/azure-preview-env-template.json @@ -22,7 +22,8 @@ } }, "variables": { - "appServicePlanName": "[concat('ASP-', parameters('appName'))]" + "appServicePlanName": "[concat('ASP-', parameters('appName'))]", + "dnsName": "[concat(parameters('appName'), '-', take(uniqueString(subscription().subscriptionId, resourceGroup().id, deployment().name), 6))]" }, "resources": [ { @@ -42,14 +43,16 @@ { "type": "Microsoft.Web/sites", "apiVersion": "2018-11-01", - "name": "[parameters('appName')]", + "name": "[variables('dnsName')]", "location": "[parameters('location')]", "dependsOn": [ "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]" ], - "tags": {}, + "tags": { + "DocsAppName": "[parameters('appName')]" + }, "properties": { - "name": "[parameters('appName')]", + "name": "[variables('dnsName')]", "siteConfig": { "appSettings": [ { @@ -105,7 +108,7 @@ "outputs": { "defaultHostName": { "type": "string", - "value": "[concat('https://', parameters('appName'), '.azurewebsites.net')]" + "value": "[concat('https://', variables('dnsName'), '.azurewebsites.net')]" } } }