Skip to content

Commit 8201147

Browse files
authored
Merge pull request #51 from developmentseed/feature/invalidate-cloudfront
invalidate cloudfront
2 parents e6a7fe8 + d945261 commit 8201147

File tree

2 files changed

+117
-12
lines changed

2 files changed

+117
-12
lines changed

.github/workflows/deploy-web.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ jobs:
5353
- name: Deploy to S3
5454
working-directory: ${{ env.WORKING_DIRECTORY }}
5555
run: aws s3 sync ./dist s3://gradient.osmcha.org --delete
56+
57+
- name: Invalidate CloudFront Cache
58+
working-directory: ${{ env.WORKING_DIRECTORY }}
59+
run: |
60+
aws cloudfront create-invalidation --distribution-id E3I6NYCQVXFMCK --paths "/*"

.github/workflows/preview.yml

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
1-
name: Surge PR Preview
1+
# credit @geohacker for the original script
2+
name: Preview Deployment
23

3-
on: [pull_request]
4+
on:
5+
pull_request:
6+
types: [opened, synchronize, reopened, closed]
47

58
env:
69
NODE: 18
710
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
814

915
jobs:
10-
preview:
16+
build:
1117
runs-on: ubuntu-latest
1218
permissions:
13-
pull-requests: write # allow surge-preview to create/update PR comments
19+
id-token: write
20+
contents: read
21+
issues: write
22+
pull-requests: write
23+
1424
steps:
25+
- name: Cancel Previous Runs
26+
uses: styfle/[email protected]
27+
with:
28+
access_token: ${{ secrets.GITHUB_TOKEN }}
29+
1530
- name: Checkout
1631
uses: actions/checkout@v3
1732

@@ -30,12 +45,97 @@ jobs:
3045
run: yarn lint
3146
working-directory: ${{ env.WORKING_DIRECTORY }}
3247

33-
- uses: afc163/surge-preview@v1
34-
id: preview_step
48+
- name: Configure AWS credentials using OIDC
49+
uses: aws-actions/configure-aws-credentials@v2
50+
with:
51+
role-to-assume: arn:aws:iam::003081160852:role/osm-gradient-deploy-s3-role
52+
aws-region: us-east-1
53+
54+
- name: Build
55+
run: npx vite build
56+
working-directory: ${{ env.WORKING_DIRECTORY }}
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+
74+
- name: Deploy to S3 (Preview)
75+
if: github.event.action != 'closed'
76+
run: |
77+
aws s3 sync ./dist s3://$BUCKET_NAME --delete
78+
aws s3 website s3://$BUCKET_NAME --index-document index.html --error-document index.html
79+
working-directory: ${{ env.WORKING_DIRECTORY }}
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
35125
with:
36-
surge_token: ${{ secrets.SURGE_TOKEN }}
37-
dist: dist
38-
build: |
39-
vite build
40-
- name: Get the preview_url
41-
run: echo "url => ${{ steps.preview_step.outputs.preview_url }}"
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+
138+
- name: Cleanup S3 Bucket
139+
if: github.event.action == 'closed'
140+
run: |
141+
aws s3 rb s3://$BUCKET_NAME --force

0 commit comments

Comments
 (0)