From 0e32401d960ca7b59307ac1048f2b53d2f1d9fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Sun, 22 Sep 2024 18:52:04 +0200 Subject: [PATCH 1/7] Swap workflow moves staging/latest tag too --- .../workflows/swap-azure-web-app-slots.yml | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/.github/workflows/swap-azure-web-app-slots.yml b/.github/workflows/swap-azure-web-app-slots.yml index 50820f1e4..83792c909 100644 --- a/.github/workflows/swap-azure-web-app-slots.yml +++ b/.github/workflows/swap-azure-web-app-slots.yml @@ -154,11 +154,6 @@ jobs: token: ${{ secrets.CODE_REPOSITORY_WRITE_TOKEN }} submodules: false - - name: Remove Old Latest Tags - uses: Lombiq/GitHub-Actions/.github/actions/remove-old-latest-tags@dev - with: - tag-prefix: ${{ inputs.tag-prefix }} - - name: Move Latest Tag (Swap) and Add Timestamp Tag id: move-latest-tag if: inputs.app-code-repository != '' @@ -168,19 +163,36 @@ jobs: throw 'Tag prefix not set, exiting' } + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' + git config user.name 'github-actions[bot]' + $tagExists = (git tag --list '${{ inputs.swap-prefix }}/latest') if ([string]::IsNullOrEmpty($tagExists)) { throw 'No latest tag found for swap prefix' } - $latest = (git rev-list -n 1 '${{ inputs.swap-prefix }}/latest') - if ($latest) + $stagingLatest = (git rev-list --max-count=1 '${{ inputs.swap-prefix }}/latest') + if ($stagingLatest) { - git tag --annotate '${{ inputs.tag-prefix }}/latest' $latest --message 'Latest tag for ${{ inputs.tag-prefix }}' + git fetch --tags + $stagingLatestTag = (git tag --list '${{ inputs.tag-prefix }}/latest') + if ($stagingLatestTag) + { + # Move the staging/latest tag to the commit of production/latest. + $productionLatest = (git rev-list --max-count=1 '${{ inputs.tag-prefix }}/latest') + + git tag --delete '${{ inputs.tag-prefix }}/latest' + git push origin ':refs/tags/${{ inputs.tag-prefix }}/latest' + + git tag --annotate '${{ inputs.swap-prefix }}/latest' $productionLatest --message 'Latest tag for ${{ inputs.swap-prefix }}' + git push origin 'refs/tags/${{ inputs.swap-prefix }}/latest' + } + + git tag --annotate '${{ inputs.tag-prefix }}/latest' $stagingLatest --message 'Latest tag for ${{ inputs.tag-prefix }}' git push origin 'refs/tags/${{ inputs.tag-prefix }}/latest' $gitMessage = 'Swap tagged at ${{ steps.create-timestamp.outputs.timestamp-tag }}' - git tag --annotate '${{ inputs.tag-prefix }}/${{ steps.create-timestamp.outputs.timestamp-tag }}' $latest --message $gitMessage + git tag --annotate '${{ inputs.tag-prefix }}/${{ steps.create-timestamp.outputs.timestamp-tag }}' $stagingLatest --message $gitMessage git push origin 'refs/tags/${{ inputs.tag-prefix }}/${{ steps.create-timestamp.outputs.timestamp-tag }}' } else @@ -188,8 +200,8 @@ jobs: throw "No latest tag found for swap prefix" } - Write-Output "::notice::The commit from the app code repository with the SHA $latest was swapped out." - "commit-sha=$latest" >> $Env:GITHUB_OUTPUT + Write-Output "::notice::The commit from the app code repository with the SHA $stagingLatest was swapped out." + "commit-sha=$stagingLatest" >> $Env:GITHUB_OUTPUT - name: Add Azure Application Insights Release Annotation uses: Lombiq/GitHub-Actions/.github/actions/add-azure-application-insights-release-annotation@dev From 091e634b49dcf9760cefb26e368300173c71d2e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Sun, 22 Sep 2024 18:55:17 +0200 Subject: [PATCH 2/7] Temporarily removing Azure swap logic --- .../workflows/swap-azure-web-app-slots.yml | 35 ------------------- 1 file changed, 35 deletions(-) diff --git a/.github/workflows/swap-azure-web-app-slots.yml b/.github/workflows/swap-azure-web-app-slots.yml index 83792c909..e47591823 100644 --- a/.github/workflows/swap-azure-web-app-slots.yml +++ b/.github/workflows/swap-azure-web-app-slots.yml @@ -127,14 +127,6 @@ jobs: -WebAppName ${{ inputs.app-name }} ` -SlotName ${{ inputs.source-slot-name }} - - name: Swap Web App Slots - run: | - Switch-AzWebAppSlot ` - -ResourceGroupName ${{ inputs.resource-group-name }} ` - -Name ${{ inputs.app-name }} ` - -SourceSlotName ${{ inputs.source-slot-name }} ` - -DestinationSlotName ${{ inputs.destination-slot-name }} - - name: Create Timestamp id: create-timestamp uses: Lombiq/GitHub-Actions/.github/actions/create-timestamp@dev @@ -203,33 +195,6 @@ jobs: Write-Output "::notice::The commit from the app code repository with the SHA $stagingLatest was swapped out." "commit-sha=$stagingLatest" >> $Env:GITHUB_OUTPUT - - name: Add Azure Application Insights Release Annotation - uses: Lombiq/GitHub-Actions/.github/actions/add-azure-application-insights-release-annotation@dev - with: - release-name: 'Swap #${{ github.run_number }} from ${{ inputs.source-slot-name }} to ${{ inputs.destination-slot-name }}' - application-insights-resource-id: ${{ inputs.application-insights-resource-id }} - timestamp: ${{ steps.create-timestamp.outputs.timestamp }} - commit-sha: ${{ steps.move-latest-tag.outputs.commit-sha }} - - # Required because the release action takes the repository name and owner as separate inputs. - - name: Extract Repository Name - id: extract-repo-name - if: inputs.app-code-repository != '' - run: | - $repoName = '${{ inputs.app-code-repository }}'.Split('/')[1] - "repo-name=$repoName" >> $Env:GITHUB_OUTPUT - - - name: Create Release - uses: Lombiq/GitHub-Actions/.github/actions/release-action@dev - if: inputs.app-code-repository != '' - with: - repo: ${{ steps.extract-repo-name.outputs.repo-name }} - token: ${{ secrets.CODE_REPOSITORY_WRITE_TOKEN }} - tag: ${{ inputs.tag-prefix }}/${{ steps.create-timestamp.outputs.timestamp-tag }} - releaseName: ${{ steps.create-timestamp.outputs.timestamp }} - allowUpdates: true - generateReleaseNotes: true - - name: Cancel Workflow on Failure if: failure() && inputs.cancel-workflow-on-failure == 'true' uses: Lombiq/GitHub-Actions/.github/actions/cancel-workflow@dev From 57a2130195293c2f2cdc41e6125f4b1a002a7c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Sun, 22 Sep 2024 19:58:43 +0200 Subject: [PATCH 3/7] Temporarily removing more logic --- .../workflows/swap-azure-web-app-slots.yml | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/.github/workflows/swap-azure-web-app-slots.yml b/.github/workflows/swap-azure-web-app-slots.yml index e47591823..18af4ecdc 100644 --- a/.github/workflows/swap-azure-web-app-slots.yml +++ b/.github/workflows/swap-azure-web-app-slots.yml @@ -110,34 +110,10 @@ jobs: shell: pwsh timeout-minutes: ${{ inputs.timeout-minutes }} steps: - - name: Login to Azure - uses: Lombiq/GitHub-Actions/.github/actions/login-to-azure@dev - env: - AZURE_CLIENT_ID: ${{ secrets.AZURE_APP_SERVICE_SWAP_SERVICE_PRINCIPAL_ID }} - AZURE_TENANT_ID: ${{ secrets.AZURE_APP_SERVICE_SWAP_AZURE_TENANT_ID }} - AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_APP_SERVICE_SWAP_AZURE_SUBSCRIPTION_ID }} - - - name: Initialize PowerShell modules - uses: Lombiq/Infrastructure-Scripts/.github/actions/initialize@dev - - - name: Test Source Web App Slot - run: | - Test-AzureWebApp ` - -ResourceGroupName ${{ inputs.resource-group-name }} ` - -WebAppName ${{ inputs.app-name }} ` - -SlotName ${{ inputs.source-slot-name }} - - name: Create Timestamp id: create-timestamp uses: Lombiq/GitHub-Actions/.github/actions/create-timestamp@dev - - name: Test Destination Web App Slot - run: | - Test-AzureWebApp ` - -ResourceGroupName ${{ inputs.resource-group-name }} ` - -WebAppName ${{ inputs.app-name }} ` - -SlotName ${{ inputs.destination-slot-name }} - - name: Checkout Code Repository if: inputs.app-code-repository != '' uses: Lombiq/GitHub-Actions/.github/actions/checkout@dev From fce90813cfc6297e2abe920228cb54857cd4519d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Sun, 22 Sep 2024 19:58:53 +0200 Subject: [PATCH 4/7] Better error messages --- .github/workflows/swap-azure-web-app-slots.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/swap-azure-web-app-slots.yml b/.github/workflows/swap-azure-web-app-slots.yml index 18af4ecdc..d3fe63d19 100644 --- a/.github/workflows/swap-azure-web-app-slots.yml +++ b/.github/workflows/swap-azure-web-app-slots.yml @@ -137,7 +137,7 @@ jobs: $tagExists = (git tag --list '${{ inputs.swap-prefix }}/latest') if ([string]::IsNullOrEmpty($tagExists)) { - throw 'No latest tag found for swap prefix' + throw 'No latest tag found for the swap prefix in the code repo. Note that you can only swap out from the default branch.' } $stagingLatest = (git rev-list --max-count=1 '${{ inputs.swap-prefix }}/latest') @@ -165,7 +165,7 @@ jobs: } else { - throw "No latest tag found for swap prefix" + throw 'No latest tag found for the swap prefix in the code repo. Note that you can only swap out from the default branch.' } Write-Output "::notice::The commit from the app code repository with the SHA $stagingLatest was swapped out." From 57e4f5c63fd209626aa2813c85ed7802bc9891d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Sun, 22 Sep 2024 20:06:51 +0200 Subject: [PATCH 5/7] Fetching the whole history from the code repo --- .github/workflows/swap-azure-web-app-slots.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/swap-azure-web-app-slots.yml b/.github/workflows/swap-azure-web-app-slots.yml index d3fe63d19..bd7f7f93d 100644 --- a/.github/workflows/swap-azure-web-app-slots.yml +++ b/.github/workflows/swap-azure-web-app-slots.yml @@ -121,6 +121,8 @@ jobs: repository: ${{ inputs.app-code-repository }} token: ${{ secrets.CODE_REPOSITORY_WRITE_TOKEN }} submodules: false + # We need the full history for the tags, and it's not necessarily the default branch that's swapped out. + fetch-depth: 0 - name: Move Latest Tag (Swap) and Add Timestamp Tag id: move-latest-tag @@ -137,7 +139,7 @@ jobs: $tagExists = (git tag --list '${{ inputs.swap-prefix }}/latest') if ([string]::IsNullOrEmpty($tagExists)) { - throw 'No latest tag found for the swap prefix in the code repo. Note that you can only swap out from the default branch.' + throw 'No latest tag found for the swap prefix in the code repo.' } $stagingLatest = (git rev-list --max-count=1 '${{ inputs.swap-prefix }}/latest') @@ -165,7 +167,7 @@ jobs: } else { - throw 'No latest tag found for the swap prefix in the code repo. Note that you can only swap out from the default branch.' + throw 'No latest tag found for the swap prefix in the code repo.' } Write-Output "::notice::The commit from the app code repository with the SHA $stagingLatest was swapped out." From 5b19ab1a187e5e1b18bef81cc8c949dc0db07ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Sun, 22 Sep 2024 20:11:49 +0200 Subject: [PATCH 6/7] Adding missing staging/latest tag deletion --- .github/workflows/swap-azure-web-app-slots.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/swap-azure-web-app-slots.yml b/.github/workflows/swap-azure-web-app-slots.yml index bd7f7f93d..04bffc29c 100644 --- a/.github/workflows/swap-azure-web-app-slots.yml +++ b/.github/workflows/swap-azure-web-app-slots.yml @@ -155,6 +155,9 @@ jobs: git tag --delete '${{ inputs.tag-prefix }}/latest' git push origin ':refs/tags/${{ inputs.tag-prefix }}/latest' + git tag --delete '${{ inputs.swap-prefix }}/latest' + git push origin ':refs/tags/${{ inputs.swap-prefix }}/latest' + git tag --annotate '${{ inputs.swap-prefix }}/latest' $productionLatest --message 'Latest tag for ${{ inputs.swap-prefix }}' git push origin 'refs/tags/${{ inputs.swap-prefix }}/latest' } From dbd55203b622c3c3778f7cf3e8489a3129ac7482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Sun, 22 Sep 2024 20:19:13 +0200 Subject: [PATCH 7/7] Reverting temporary changes --- .../workflows/swap-azure-web-app-slots.yml | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/.github/workflows/swap-azure-web-app-slots.yml b/.github/workflows/swap-azure-web-app-slots.yml index 04bffc29c..778eb2da9 100644 --- a/.github/workflows/swap-azure-web-app-slots.yml +++ b/.github/workflows/swap-azure-web-app-slots.yml @@ -110,10 +110,42 @@ jobs: shell: pwsh timeout-minutes: ${{ inputs.timeout-minutes }} steps: + - name: Login to Azure + uses: Lombiq/GitHub-Actions/.github/actions/login-to-azure@dev + env: + AZURE_CLIENT_ID: ${{ secrets.AZURE_APP_SERVICE_SWAP_SERVICE_PRINCIPAL_ID }} + AZURE_TENANT_ID: ${{ secrets.AZURE_APP_SERVICE_SWAP_AZURE_TENANT_ID }} + AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_APP_SERVICE_SWAP_AZURE_SUBSCRIPTION_ID }} + + - name: Initialize PowerShell modules + uses: Lombiq/Infrastructure-Scripts/.github/actions/initialize@dev + + - name: Test Source Web App Slot + run: | + Test-AzureWebApp ` + -ResourceGroupName ${{ inputs.resource-group-name }} ` + -WebAppName ${{ inputs.app-name }} ` + -SlotName ${{ inputs.source-slot-name }} + + - name: Swap Web App Slots + run: | + Switch-AzWebAppSlot ` + -ResourceGroupName ${{ inputs.resource-group-name }} ` + -Name ${{ inputs.app-name }} ` + -SourceSlotName ${{ inputs.source-slot-name }} ` + -DestinationSlotName ${{ inputs.destination-slot-name }} + - name: Create Timestamp id: create-timestamp uses: Lombiq/GitHub-Actions/.github/actions/create-timestamp@dev + - name: Test Destination Web App Slot + run: | + Test-AzureWebApp ` + -ResourceGroupName ${{ inputs.resource-group-name }} ` + -WebAppName ${{ inputs.app-name }} ` + -SlotName ${{ inputs.destination-slot-name }} + - name: Checkout Code Repository if: inputs.app-code-repository != '' uses: Lombiq/GitHub-Actions/.github/actions/checkout@dev @@ -176,6 +208,33 @@ jobs: Write-Output "::notice::The commit from the app code repository with the SHA $stagingLatest was swapped out." "commit-sha=$stagingLatest" >> $Env:GITHUB_OUTPUT + - name: Add Azure Application Insights Release Annotation + uses: Lombiq/GitHub-Actions/.github/actions/add-azure-application-insights-release-annotation@dev + with: + release-name: 'Swap #${{ github.run_number }} from ${{ inputs.source-slot-name }} to ${{ inputs.destination-slot-name }}' + application-insights-resource-id: ${{ inputs.application-insights-resource-id }} + timestamp: ${{ steps.create-timestamp.outputs.timestamp }} + commit-sha: ${{ steps.move-latest-tag.outputs.commit-sha }} + + # Required because the release action takes the repository name and owner as separate inputs. + - name: Extract Repository Name + id: extract-repo-name + if: inputs.app-code-repository != '' + run: | + $repoName = '${{ inputs.app-code-repository }}'.Split('/')[1] + "repo-name=$repoName" >> $Env:GITHUB_OUTPUT + + - name: Create Release + uses: Lombiq/GitHub-Actions/.github/actions/release-action@dev + if: inputs.app-code-repository != '' + with: + repo: ${{ steps.extract-repo-name.outputs.repo-name }} + token: ${{ secrets.CODE_REPOSITORY_WRITE_TOKEN }} + tag: ${{ inputs.tag-prefix }}/${{ steps.create-timestamp.outputs.timestamp-tag }} + releaseName: ${{ steps.create-timestamp.outputs.timestamp }} + allowUpdates: true + generateReleaseNotes: true + - name: Cancel Workflow on Failure if: failure() && inputs.cancel-workflow-on-failure == 'true' uses: Lombiq/GitHub-Actions/.github/actions/cancel-workflow@dev