From 7ac309b1a4192d9e7ed34489d0d0e2137cf00507 Mon Sep 17 00:00:00 2001 From: Rikudou_Sage Date: Wed, 27 Mar 2024 20:11:49 +0100 Subject: [PATCH 1/2] Feat: Add branch publish script (#17) --- .github/workflows/delete-dev-version.yaml | 40 +++++++++++++++++++++++ .github/workflows/publish.yaml | 38 ++++++++++++++++++--- 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/delete-dev-version.yaml diff --git a/.github/workflows/delete-dev-version.yaml b/.github/workflows/delete-dev-version.yaml new file mode 100644 index 0000000..766f4fe --- /dev/null +++ b/.github/workflows/delete-dev-version.yaml @@ -0,0 +1,40 @@ +name: Delete Dev Version + +on: + workflow_dispatch: + pull_request: + types: [closed] + +jobs: + publish: + runs-on: ubuntu-latest + steps: + - name: GitHub Slug Action + uses: rlespinasse/github-slug-action@v4 + - name: Generate SLS Stage + id: generate_sls_stage + uses: actions/github-script@v6 + with: + result-encoding: string + script: | + if (context.payload.ref == 'refs/heads/master') { + return 'prod'; + } + return process.env.GITHUB_REF_NAME_SLUG_URL; + - name: Remove Dev Version + env: + STACK_NAME: HordeNG-${{ env.GITHUB_REF_NAME_SLUG_URL }} + AWS_REGION: eu-central-1 + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + run: | + export BUCKET_NAME=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --query "Stacks[0].Outputs[?OutputKey=='Bucket'].OutputValue" --output=text) + export DEPLOYMENT_BUCKET_NAME=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --query "Stacks[0].Outputs[?OutputKey=='ServerlessDeploymentBucketName'].OutputValue" --output=text) + aws s3 rm s3://$BUCKET_NAME --recursive + aws s3 rm s3://$DEPLOYMENT_BUCKET_NAME --recursive + aws cloudformation delete-stack --stack-name $STACK_NAME + - name: Post Link + uses: thollander/actions-comment-pull-request@v2 + with: + message: The preview URL for this PR was deleted when the PR got closed. + comment_tag: post_link_comment diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 2a3fa76..7be09ef 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -5,11 +5,36 @@ on: push: branches: - master + pull_request: jobs: publish: + permissions: + pull-requests: write runs-on: ubuntu-latest steps: + - name: GitHub Slug Action + uses: rlespinasse/github-slug-action@v4 + - name: Generate Domain Name + id: generate_domain_name + uses: actions/github-script@v6 + with: + result-encoding: string + script: | + if (context.payload.ref == 'refs/heads/master') { + return 'horde-ng.org'; + } + return process.env.GITHUB_REF_NAME_SLUG_URL + '.dev.horde-ng.org'; + - name: Generate SLS Stage + id: generate_sls_stage + uses: actions/github-script@v6 + with: + result-encoding: string + script: | + if (context.payload.ref == 'refs/heads/master') { + return 'prod'; + } + return process.env.GITHUB_REF_NAME_SLUG_URL; - name: Checkout code uses: actions/checkout@v4 - name: Setup prod dependencies @@ -17,20 +42,20 @@ jobs: - name: Deploy infrastructure uses: serverless/github-action@v3 with: - args: deploy --stage prod --verbose + args: deploy --stage ${{ steps.generate_sls_stage.outputs.result }} --verbose env: AWS_REGION: eu-central-1 AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} DOMAIN_ZONE: ${{ secrets.APP_DOMAIN_HOSTED_ZONE }} - DOMAIN_NAME: horde-ng.org + DOMAIN_NAME: ${{ steps.generate_domain_name.outputs.result }} - name: Create runtime version run: echo "window['APP_VERSION'] = '$GITHUB_SHA';" >> src/assets/runtime-variables.js - name: Build app run: ./node_modules/.bin/ng build - name: Copy files to S3 env: - STACK_NAME: HordeNG-prod + STACK_NAME: HordeNG-${{ env.GITHUB_REF_NAME_SLUG_URL }} AWS_REGION: eu-central-1 AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} @@ -39,10 +64,15 @@ jobs: aws s3 sync dist/horde-gui/browser s3://$BUCKET --delete - name: Create invalidation env: - STACK_NAME: HordeNG-prod + STACK_NAME: HordeNG-${{ env.GITHUB_REF_NAME_SLUG_URL }} AWS_REGION: eu-central-1 AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} run: | CDN_ID=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --query "Stacks[0].Outputs[?OutputKey=='Cdn'].OutputValue" --output=text) aws cloudfront create-invalidation --distribution-id $CDN_ID --paths "/*" 2>&1 > /dev/null + - name: Post Link + uses: thollander/actions-comment-pull-request@v2 + with: + message: This pull request has been deployed to https://${{ steps.generate_domain_name.outputs.result }} + comment_tag: post_link_comment From 59f4bc67ac245122cf8d4a676ebb45194e5297ee Mon Sep 17 00:00:00 2001 From: Rikudou_Sage Date: Wed, 27 Mar 2024 20:37:39 +0100 Subject: [PATCH 2/2] Fix: Deployment fixes (#18) --- .github/workflows/delete-dev-version.yaml | 4 +++- .github/workflows/publish.yaml | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/delete-dev-version.yaml b/.github/workflows/delete-dev-version.yaml index 766f4fe..1ab643d 100644 --- a/.github/workflows/delete-dev-version.yaml +++ b/.github/workflows/delete-dev-version.yaml @@ -6,7 +6,9 @@ on: types: [closed] jobs: - publish: + delete: + permissions: + pull-requests: write runs-on: ubuntu-latest steps: - name: GitHub Slug Action diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 7be09ef..0f418f8 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -55,7 +55,7 @@ jobs: run: ./node_modules/.bin/ng build - name: Copy files to S3 env: - STACK_NAME: HordeNG-${{ env.GITHUB_REF_NAME_SLUG_URL }} + STACK_NAME: HordeNG-${{ steps.generate_sls_stage.outputs.result }} AWS_REGION: eu-central-1 AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} @@ -64,7 +64,7 @@ jobs: aws s3 sync dist/horde-gui/browser s3://$BUCKET --delete - name: Create invalidation env: - STACK_NAME: HordeNG-${{ env.GITHUB_REF_NAME_SLUG_URL }} + STACK_NAME: HordeNG-${{ steps.generate_sls_stage.outputs.result }} AWS_REGION: eu-central-1 AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} @@ -73,6 +73,7 @@ jobs: aws cloudfront create-invalidation --distribution-id $CDN_ID --paths "/*" 2>&1 > /dev/null - name: Post Link uses: thollander/actions-comment-pull-request@v2 + if: github.event_name == 'pull_request' with: message: This pull request has been deployed to https://${{ steps.generate_domain_name.outputs.result }} comment_tag: post_link_comment