From 5c755078633983e7e7473bc82b9ac5217d6d7486 Mon Sep 17 00:00:00 2001 From: Tim Hess Date: Fri, 9 Feb 2024 16:25:23 -0600 Subject: [PATCH] add PR cleanup, swap to prod workflows (#121) --- .editorconfig | 3 ++ .../{build-deploy.yml => build-and-stage.yml} | 11 ++++--- .github/workflows/pr-cleanup.yml | 32 +++++++++++++++++++ .github/workflows/stage-prod-swap.yml | 22 +++++++++++++ .mvn/wrapper/maven-wrapper.properties | 2 +- Dockerfile | 6 ++-- deploy/docker/docker-entrypoint.sh | 2 -- 7 files changed, 68 insertions(+), 10 deletions(-) rename .github/workflows/{build-deploy.yml => build-and-stage.yml} (86%) create mode 100644 .github/workflows/pr-cleanup.yml create mode 100644 .github/workflows/stage-prod-swap.yml diff --git a/.editorconfig b/.editorconfig index c7616d4f..98ca2e3b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,3 +5,6 @@ indent_style = space [*.{js,json}] indent_size = 2 + +[*.sh] +end_of_line = lf \ No newline at end of file diff --git a/.github/workflows/build-deploy.yml b/.github/workflows/build-and-stage.yml similarity index 86% rename from .github/workflows/build-deploy.yml rename to .github/workflows/build-and-stage.yml index 9e158b5c..8f38bb2d 100644 --- a/.github/workflows/build-deploy.yml +++ b/.github/workflows/build-and-stage.yml @@ -1,4 +1,4 @@ -name: Build and deploy +name: Build and stage on: pull_request: @@ -17,6 +17,7 @@ permissions: pull-requests: 'write' env: + IMAGE_NAME: initializr-web IMAGE_TAG: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || github.run_id }} jobs: @@ -44,21 +45,21 @@ jobs: password: "${{ secrets.DOCKER_PASSWORD }}" - name: Build image - run: docker build . --file "Dockerfile" -t ${{ vars.DOCKER_REGISTRY }}/initializr-web:${{ env.IMAGE_TAG }} + run: docker build . --file "Dockerfile" -t ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} - name: Push image - run: docker push ${{ vars.DOCKER_REGISTRY }}/initializr-web:${{ env.IMAGE_TAG }} + run: docker push ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} - 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 }} --configuration-source staging + run: az webapp deployment slot create --resource-group ${{ vars.AZURE_RESOURCE_GROUP }} --name ${{ vars.AZURE_WEBAPP_NAME}} --slot ${{ env.SLOT_NAME }} --configuration-source ${{ vars.STAGING_SLOT_NAME }} - name: Deploy to staging slot uses: azure/webapps-deploy@v3 id: deploy-to-webapp with: app-name: ${{ vars.AZURE_WEBAPP_NAME }} - images: ${{ vars.DOCKER_REGISTRY }}/initializr-web:${{ env.IMAGE_TAG }} + images: ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} slot-name: ${{ env.SLOT_NAME }} - name: If PR, comment with the preview link diff --git a/.github/workflows/pr-cleanup.yml b/.github/workflows/pr-cleanup.yml new file mode 100644 index 00000000..b55924b3 --- /dev/null +++ b/.github/workflows/pr-cleanup.yml @@ -0,0 +1,32 @@ +name: Delete a preview environment + +on: + pull_request: + types: [closed] + +env: + SLOT_NAME: pr-${{ github.event.number }} + +jobs: + delete-slot: + runs-on: ubuntu-latest + + steps: + - name: Log into Azure CLI with service principal + uses: azure/login@v1 + with: + creds: ${{ secrets.AZURE_CREDENTIALS }} + + - name: Delete slot on staging site + run: az webapp deployment slot delete --resource-group ${{ vars.AZURE_RESOURCE_GROUP }} --name ${{ vars.AZURE_WEBAPP_NAME}} --slot ${{ env.SLOT_NAME }} + + delete-deployment: + runs-on: ubuntu-latest + + steps: + - name: Delete Deployment Environment + uses: strumwolf/delete-deployment-environment@v2 + with: + environment: "pr-${{ github.event.number }}" + token: ${{ secrets.GITHUB_TOKEN }} + onlyRemoveDeployments: true \ No newline at end of file diff --git a/.github/workflows/stage-prod-swap.yml b/.github/workflows/stage-prod-swap.yml new file mode 100644 index 00000000..d917fe43 --- /dev/null +++ b/.github/workflows/stage-prod-swap.yml @@ -0,0 +1,22 @@ +name: Swap staging slot contents into production + +on: + workflow_dispatch: + +jobs: + promote-to-production: + name: Promote to production + runs-on: ubuntu-latest + environment: + name: 'Production' + url: 'https://${{ vars.AZURE_WEBAPP_NAME }}.azurewebsites.net/' + + steps: + - name: Log into Azure CLI with service principal + uses: azure/login@v1 + with: + creds: ${{ secrets.AZURE_CREDENTIALS }} + + - name: Swap slots + run: az webapp deployment slot swap -s ${{ vars.STAGING_SLOT_NAME }} -n ${{ vars.AZURE_WEBAPP_NAME }} -g ${{ vars.AZURE_RESOURCE_GROUP }} + \ No newline at end of file diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties index 5a40fb45..5f0536eb 100644 --- a/.mvn/wrapper/maven-wrapper.properties +++ b/.mvn/wrapper/maven-wrapper.properties @@ -1,2 +1,2 @@ -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.2/apache-maven-3.9.5-bin.zip +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar diff --git a/Dockerfile b/Dockerfile index 777f9639..064878e6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,8 +25,10 @@ RUN echo "env INITIALIZR_SERVICE_HOST;" >> /etc/nginx/main.d/default.conf RUN echo "env INITIALIZR_SERVICE_URI;" >> /etc/nginx/main.d/default.conf COPY deploy/docker/initializr-web.conf.template /etc/nginx/templates/ COPY deploy/docker/docker-entrypoint.sh ./ -RUN chmod +x docker-entrypoint.sh +RUN chmod +x ./docker-entrypoint.sh ENTRYPOINT [ "./docker-entrypoint.sh" ] CMD ["nginx", "-g", "daemon off;"] -# cat /etc/nginx/sites-enabled/initializr.conf \ No newline at end of file +# cat /etc/nginx/sites-enabled/initializr.conf +# tail -f /var/log/nginx/access.log +# tail -f /var/log/nginx/error.log \ No newline at end of file diff --git a/deploy/docker/docker-entrypoint.sh b/deploy/docker/docker-entrypoint.sh index 6f65c77a..74ddd99a 100644 --- a/deploy/docker/docker-entrypoint.sh +++ b/deploy/docker/docker-entrypoint.sh @@ -5,6 +5,4 @@ echo "Starting up with these env variables... host: $INITIALIZR_SERVICE_HOST, ur envsubst '$INITIALIZR_SERVICE_HOST $INITIALIZR_SERVICE_URI' < /etc/nginx/templates/initializr-web.conf.template > /etc/nginx/sites-enabled/initializr.conf -# cat /etc/nginx/sites-enabled/initializr.conf - exec "$@" \ No newline at end of file