Skip to content

Commit d945261

Browse files
committed
modify preview.yml
1 parent be86934 commit d945261

File tree

1 file changed

+79
-5
lines changed

1 file changed

+79
-5
lines changed

.github/workflows/preview.yml

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# credit @geohacker for the original script
12
name: Preview Deployment
23

34
on:
@@ -7,13 +8,18 @@ on:
78
env:
89
NODE: 18
910
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
1014

1115
jobs:
1216
build:
1317
runs-on: ubuntu-latest
1418
permissions:
1519
id-token: write
1620
contents: read
21+
issues: write
22+
pull-requests: write
1723

1824
steps:
1925
- name: Cancel Previous Runs
@@ -49,19 +55,87 @@ jobs:
4955
run: npx vite build
5056
working-directory: ${{ env.WORKING_DIRECTORY }}
5157

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+
5274
- name: Deploy to S3 (Preview)
5375
if: github.event.action != 'closed'
5476
run: |
55-
PR_NUMBER=${{ github.event.number }}
56-
BUCKET_NAME="osm-gradient-pr-${PR_NUMBER}"
57-
aws s3 mb s3://$BUCKET_NAME
5877
aws s3 sync ./dist s3://$BUCKET_NAME --delete
5978
aws s3 website s3://$BUCKET_NAME --index-document index.html --error-document index.html
6079
working-directory: ${{ env.WORKING_DIRECTORY }}
6180

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+
62138
- name: Cleanup S3 Bucket
63139
if: github.event.action == 'closed'
64140
run: |
65-
PR_NUMBER=${{ github.event.number }}
66-
BUCKET_NAME="osm-gradient-pr-${PR_NUMBER}"
67141
aws s3 rb s3://$BUCKET_NAME --force

0 commit comments

Comments
 (0)