diff --git a/.github/workflows/delete-dev-version.yaml b/.github/workflows/delete-dev-version.yaml new file mode 100644 index 0000000..1ab643d --- /dev/null +++ b/.github/workflows/delete-dev-version.yaml @@ -0,0 +1,42 @@ +name: Delete Dev Version + +on: + workflow_dispatch: + pull_request: + types: [closed] + +jobs: + delete: + permissions: + pull-requests: write + 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..0f418f8 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-${{ 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 }} @@ -39,10 +64,16 @@ jobs: aws s3 sync dist/horde-gui/browser s3://$BUCKET --delete - name: Create invalidation env: - STACK_NAME: HordeNG-prod + 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 }} 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 + 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