From d9452619f45ae2fba974f242f4842b6e129bc1c2 Mon Sep 17 00:00:00 2001 From: Marc Farra Date: Fri, 7 Jun 2024 15:15:24 -0600 Subject: [PATCH] modify preview.yml --- .github/workflows/preview.yml | 84 ++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 5 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 61cb76f..ef6b218 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -1,3 +1,4 @@ +# credit @geohacker for the original script name: Preview Deployment on: @@ -7,6 +8,9 @@ on: env: NODE: 18 WORKING_DIRECTORY: packages/web + COMMENT_MARKER: "Preview deployed to S3!" + BUCKET_NAME: osm-gradient-pr-${{ github.event.number }} + AWS_REGION: us-east-1 jobs: build: @@ -14,6 +18,8 @@ jobs: permissions: id-token: write contents: read + issues: write + pull-requests: write steps: - name: Cancel Previous Runs @@ -49,19 +55,87 @@ jobs: run: npx vite build working-directory: ${{ env.WORKING_DIRECTORY }} + - name: Check if bucket exists + id: check_bucket + run: | + if aws s3 ls "s3://${{ env.BUCKET_NAME }}" 2>&1 | grep -q 'NoSuchBucket'; then + echo "Bucket does not exist." + echo "::set-output name=exists::false" + else + echo "Bucket exists." + echo "::set-output name=exists::true" + fi + + - name: Create S3 bucket + if: steps.check_bucket.outputs.exists == 'false' + run: | + aws s3 mb s3://${{ env.BUCKET_NAME }} + - name: Deploy to S3 (Preview) if: github.event.action != 'closed' run: | - PR_NUMBER=${{ github.event.number }} - BUCKET_NAME="osm-gradient-pr-${PR_NUMBER}" - aws s3 mb s3://$BUCKET_NAME aws s3 sync ./dist s3://$BUCKET_NAME --delete aws s3 website s3://$BUCKET_NAME --index-document index.html --error-document index.html working-directory: ${{ env.WORKING_DIRECTORY }} + - name: Make bucket public access + if: steps.check_bucket.outputs.exists == 'false' + run: | + aws s3api delete-public-access-block --bucket ${{ env.BUCKET_NAME }} + + - name: Add bucket policy for public access + if: steps.check_bucket.outputs.exists == 'false' + run: | + echo '{ + "Version": "2012-10-17", + "Statement": [{ + "Sid": "PublicReadGetObject", + "Effect": "Allow", + "Principal": "*", + "Action": "s3:GetObject", + "Resource": "arn:aws:s3:::${{ env.BUCKET_NAME }}/*" + }] + }' > bucket-policy.json + aws s3api put-bucket-policy --bucket ${{ env.BUCKET_NAME }} --policy file://bucket-policy.json + + - name: Check for existing preview comment + id: check_comment + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const comments = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.pull_request.number, + }); + const existingComment = comments.data.find(comment => comment.body.includes('${{ env.COMMENT_MARKER }}')); + if (existingComment) { + console.log('Deployment comment already exists:', existingComment.html_url); + core.setOutput('should_post_comment', 'false'); + return existingComment.html_url; + } else { + core.setOutput('should_post_comment', 'true'); + return ''; + } + + - name: Post comment with preview URL + if: steps.check_comment.outputs.should_post_comment == 'true' + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const websiteUrl = `http://${{ env.BUCKET_NAME }}.s3-website-${{ env.AWS_REGION }}.amazonaws.com/`; + const pullRequestNumber = context.payload.pull_request.number; + const message = `✨ Preview deployed to S3! Visit ${websiteUrl}`; + github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: pullRequestNumber, + body: message + }); + - name: Cleanup S3 Bucket if: github.event.action == 'closed' run: | - PR_NUMBER=${{ github.event.number }} - BUCKET_NAME="osm-gradient-pr-${PR_NUMBER}" aws s3 rb s3://$BUCKET_NAME --force