|
| 1 | +# credit @geohacker for the original script |
1 | 2 | name: Preview Deployment
|
2 | 3 |
|
3 | 4 | on:
|
|
7 | 8 | env:
|
8 | 9 | NODE: 18
|
9 | 10 | WORKING_DIRECTORY: packages/web
|
| 11 | + COMMENT_MARKER: "Preview deployed to S3!" |
| 12 | + BUCKET_NAME: osm-gradient-pr-${{ github.event.number }} |
| 13 | + AWS_REGION: us-east-1 |
10 | 14 |
|
11 | 15 | jobs:
|
12 | 16 | build:
|
13 | 17 | runs-on: ubuntu-latest
|
14 | 18 | permissions:
|
15 | 19 | id-token: write
|
16 | 20 | contents: read
|
| 21 | + issues: write |
| 22 | + pull-requests: write |
17 | 23 |
|
18 | 24 | steps:
|
19 | 25 | - name: Cancel Previous Runs
|
@@ -49,19 +55,87 @@ jobs:
|
49 | 55 | run: npx vite build
|
50 | 56 | working-directory: ${{ env.WORKING_DIRECTORY }}
|
51 | 57 |
|
| 58 | + - name: Check if bucket exists |
| 59 | + id: check_bucket |
| 60 | + run: | |
| 61 | + if aws s3 ls "s3://${{ env.BUCKET_NAME }}" 2>&1 | grep -q 'NoSuchBucket'; then |
| 62 | + echo "Bucket does not exist." |
| 63 | + echo "::set-output name=exists::false" |
| 64 | + else |
| 65 | + echo "Bucket exists." |
| 66 | + echo "::set-output name=exists::true" |
| 67 | + fi |
| 68 | +
|
| 69 | + - name: Create S3 bucket |
| 70 | + if: steps.check_bucket.outputs.exists == 'false' |
| 71 | + run: | |
| 72 | + aws s3 mb s3://${{ env.BUCKET_NAME }} |
| 73 | +
|
52 | 74 | - name: Deploy to S3 (Preview)
|
53 | 75 | if: github.event.action != 'closed'
|
54 | 76 | run: |
|
55 |
| - PR_NUMBER=${{ github.event.number }} |
56 |
| - BUCKET_NAME="osm-gradient-pr-${PR_NUMBER}" |
57 |
| - aws s3 mb s3://$BUCKET_NAME |
58 | 77 | aws s3 sync ./dist s3://$BUCKET_NAME --delete
|
59 | 78 | aws s3 website s3://$BUCKET_NAME --index-document index.html --error-document index.html
|
60 | 79 | working-directory: ${{ env.WORKING_DIRECTORY }}
|
61 | 80 |
|
| 81 | + - name: Make bucket public access |
| 82 | + if: steps.check_bucket.outputs.exists == 'false' |
| 83 | + run: | |
| 84 | + aws s3api delete-public-access-block --bucket ${{ env.BUCKET_NAME }} |
| 85 | +
|
| 86 | + - name: Add bucket policy for public access |
| 87 | + if: steps.check_bucket.outputs.exists == 'false' |
| 88 | + run: | |
| 89 | + echo '{ |
| 90 | + "Version": "2012-10-17", |
| 91 | + "Statement": [{ |
| 92 | + "Sid": "PublicReadGetObject", |
| 93 | + "Effect": "Allow", |
| 94 | + "Principal": "*", |
| 95 | + "Action": "s3:GetObject", |
| 96 | + "Resource": "arn:aws:s3:::${{ env.BUCKET_NAME }}/*" |
| 97 | + }] |
| 98 | + }' > bucket-policy.json |
| 99 | + aws s3api put-bucket-policy --bucket ${{ env.BUCKET_NAME }} --policy file://bucket-policy.json |
| 100 | +
|
| 101 | + - name: Check for existing preview comment |
| 102 | + id: check_comment |
| 103 | + uses: actions/github-script@v6 |
| 104 | + with: |
| 105 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 106 | + script: | |
| 107 | + const comments = await github.rest.issues.listComments({ |
| 108 | + owner: context.repo.owner, |
| 109 | + repo: context.repo.repo, |
| 110 | + issue_number: context.payload.pull_request.number, |
| 111 | + }); |
| 112 | + const existingComment = comments.data.find(comment => comment.body.includes('${{ env.COMMENT_MARKER }}')); |
| 113 | + if (existingComment) { |
| 114 | + console.log('Deployment comment already exists:', existingComment.html_url); |
| 115 | + core.setOutput('should_post_comment', 'false'); |
| 116 | + return existingComment.html_url; |
| 117 | + } else { |
| 118 | + core.setOutput('should_post_comment', 'true'); |
| 119 | + return ''; |
| 120 | + } |
| 121 | +
|
| 122 | + - name: Post comment with preview URL |
| 123 | + if: steps.check_comment.outputs.should_post_comment == 'true' |
| 124 | + uses: actions/github-script@v6 |
| 125 | + with: |
| 126 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 127 | + script: | |
| 128 | + const websiteUrl = `http://${{ env.BUCKET_NAME }}.s3-website-${{ env.AWS_REGION }}.amazonaws.com/`; |
| 129 | + const pullRequestNumber = context.payload.pull_request.number; |
| 130 | + const message = `✨ Preview deployed to S3! Visit ${websiteUrl}`; |
| 131 | + github.rest.issues.createComment({ |
| 132 | + owner: context.repo.owner, |
| 133 | + repo: context.repo.repo, |
| 134 | + issue_number: pullRequestNumber, |
| 135 | + body: message |
| 136 | + }); |
| 137 | +
|
62 | 138 | - name: Cleanup S3 Bucket
|
63 | 139 | if: github.event.action == 'closed'
|
64 | 140 | run: |
|
65 |
| - PR_NUMBER=${{ github.event.number }} |
66 |
| - BUCKET_NAME="osm-gradient-pr-${PR_NUMBER}" |
67 | 141 | aws s3 rb s3://$BUCKET_NAME --force
|
0 commit comments