From 3741261744dc8eec8ae21c031439b59f3fda646c Mon Sep 17 00:00:00 2001 From: Tim Hess Date: Tue, 6 Feb 2024 14:15:35 -0600 Subject: [PATCH] use a single deployment workflow that is aware of PR vs merge --- .github/workflows/build-and-stage.yml | 46 +++++++++++-- .github/workflows/pr-cleanup.yml | 1 - .github/workflows/pr-deploy.yml | 98 --------------------------- 3 files changed, 42 insertions(+), 103 deletions(-) delete mode 100644 .github/workflows/pr-deploy.yml diff --git a/.github/workflows/build-and-stage.yml b/.github/workflows/build-and-stage.yml index a694484..4b64266 100644 --- a/.github/workflows/build-and-stage.yml +++ b/.github/workflows/build-and-stage.yml @@ -3,20 +3,34 @@ name: Build and stage env: AZURE_WEBAPP_PACKAGE_PATH: '.' DOTNET_VERSION: '8.0' - SLOT_NAME: staging on: push: branches: [ "main" ] + pull_request: + branches: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true permissions: contents: read jobs: + set-environment: + name: Determine workflow environment + runs-on: ubuntu-latest + outputs: + Slot_Name: ${{ steps.set-name.outputs.name }} + steps: + - id: set-name + run: echo "Slot_Name=${{ github.event_name == 'pull_request' && format('pr-', github.event.number) || vars.STAGING_SLOT_NAME }}" >> "$GITHUB_OUTPUT" + build: name: Build and publish app runs-on: ubuntu-latest - steps: - uses: actions/checkout@v4 @@ -24,6 +38,7 @@ jobs: uses: actions/setup-dotnet@v4 with: dotnet-version: ${{ env.DOTNET_VERSION }} + - name: Set up dependency caching for faster builds uses: actions/cache@v4 with: @@ -31,10 +46,13 @@ jobs: key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }} restore-keys: | ${{ runner.os }}-nuget- - - name: Build with dotnet + + - name: dotnet build run: dotnet build src/Client --configuration Release + - name: dotnet publish run: dotnet publish src/Client -c Release -o ${{ env.DOTNET_ROOT }}/mainsite + - name: Upload artifact for deployment job uses: actions/upload-artifact@v4 with: @@ -48,8 +66,10 @@ jobs: runs-on: ubuntu-latest needs: build environment: - name: 'Staging' + name: ${{ needs.set-environment.outputs.Slot_Name }} url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} + env: + SLOT_NAME: ${{ needs.set-environment.outputs.Slot_Name }} steps: - name: Download artifact from build job @@ -62,6 +82,10 @@ jobs: with: creds: ${{ secrets.AZURE_CREDENTIALS }} + - name: If PR, create a new staging slot + if: ${{ github.event_name == 'pull_request' }} + run: az webapp deployment slot create --resource-group ${{ vars.AZURE_RESOURCE_GROUP }} --name ${{ vars.AZURE_WEBAPP_NAME}} --slot ${{ env.SLOT_NAME }} + - name: Deploy to Azure Web App id: deploy-to-webapp uses: azure/webapps-deploy@v3 @@ -69,3 +93,17 @@ jobs: app-name: ${{ vars.AZURE_WEBAPP_NAME }} package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }} slot-name: ${{ env.SLOT_NAME }} + + - name: If PR, comment with the preview link + if: ${{ github.event_name == 'pull_request' }} + uses: mshick/add-pr-comment@v2 + with: + message: | + ## Preview link: https://${{ vars.AZURE_WEBAPP_NAME }}-${{ env.SLOT_NAME }}.azurewebsites.net + + - Your changes have been deployed to the preview site. The preview site will update as you add more commits to this branch. + - The preview link is shareable, but will be deleted when the pull request is merged or closed. + + > *This is an automated message.* + repo-token: ${{ secrets.GITHUB_TOKEN }} + \ No newline at end of file diff --git a/.github/workflows/pr-cleanup.yml b/.github/workflows/pr-cleanup.yml index 1fc73b2..7a85805 100644 --- a/.github/workflows/pr-cleanup.yml +++ b/.github/workflows/pr-cleanup.yml @@ -1,4 +1,3 @@ - name: Delete a preview environment on: diff --git a/.github/workflows/pr-deploy.yml b/.github/workflows/pr-deploy.yml deleted file mode 100644 index 63f3326..0000000 --- a/.github/workflows/pr-deploy.yml +++ /dev/null @@ -1,98 +0,0 @@ -name: Deploy a preview environment - -on: - pull_request: - branches: - - main - - v4 - -concurrency: ci-${{ github.event.number }} - -env: - AZURE_WEBAPP_PACKAGE_PATH: '.' - DOTNET_VERSION: '8.0' - SLOT_NAME: preview-${{ github.event.number }} - -jobs: - build: - name: Build and publish app - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - - name: Set up .NET Core - uses: actions/setup-dotnet@v4 - with: - dotnet-version: ${{ env.DOTNET_VERSION }} - - name: Set up dependency caching for faster builds - uses: actions/cache@v4 - with: - path: ~/.nuget/packages - key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }} - restore-keys: | - ${{ runner.os }}-nuget- - - name: Build with dotnet - run: dotnet build src/Client --configuration Release - - name: dotnet publish - run: dotnet publish src/Client -c Release -o ${{ env.DOTNET_ROOT }}/mainsite - - name: Upload artifact for deployment job - uses: actions/upload-artifact@v4 - with: - name: Steeltoe-MainSite - path: ${{ env.DOTNET_ROOT }}/mainsite - - set-up-test-env: - name: Create a staging slot for this PR - runs-on: ubuntu-latest - - steps: - - name: Log into Azure CLI with service principal - uses: azure/login@v1 - with: - creds: ${{ secrets.AZURE_CREDENTIALS }} - - - name: Create slot on staging site - run: az webapp deployment slot create --resource-group ${{ vars.AZURE_RESOURCE_GROUP }} --name ${{ vars.AZURE_WEBAPP_NAME}} --slot $SLOT_NAME - - deploy: - name: Deploy to new staging slot - runs-on: ubuntu-latest - needs: [build, set-up-test-env] - environment: - name: "PR #${{ github.event.number }}" - url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} - permissions: - pull-requests: write - - steps: - - name: Download artifact from build job - uses: actions/download-artifact@v4 - with: - name: Steeltoe-MainSite - - - name: Log into Azure CLI with service principal - uses: azure/login@v1 - with: - creds: ${{ secrets.AZURE_CREDENTIALS }} - - - name: Deploy to slot on staging site - uses: azure/webapps-deploy@v3 - id: deploy-to-webapp - with: - app-name: ${{ vars.AZURE_WEBAPP_NAME }} - package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }} - slot-name: ${{ env.SLOT_NAME }} - - - name: Comment on PR with the preview link - uses: mshick/add-pr-comment@v2 - with: - message: | - ## Preview link: https://${{ vars.AZURE_WEBAPP_NAME }}-${{ env.SLOT_NAME }}.azurewebsites.net - - - Your changes have been deployed to the preview site. The preview site will update as you add more commits to this branch. - - The preview link is shareable, but will be deleted when the pull request is merged or closed. - - > *This is an automated message.* - repo-token: ${{ secrets.GITHUB_TOKEN }} - \ No newline at end of file