1
- name : Surge PR Preview
1
+ # credit @geohacker for the original script
2
+ name : Preview Deployment
2
3
3
- on : [pull_request]
4
+ on :
5
+ pull_request :
6
+ types : [opened, synchronize, reopened, closed]
4
7
5
8
env :
6
9
NODE : 18
7
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
8
14
9
15
jobs :
10
- preview :
16
+ build :
11
17
runs-on : ubuntu-latest
12
18
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
+
14
24
steps :
25
+ - name : Cancel Previous Runs
26
+
27
+ with :
28
+ access_token : ${{ secrets.GITHUB_TOKEN }}
29
+
15
30
- name : Checkout
16
31
uses : actions/checkout@v3
17
32
@@ -30,12 +45,97 @@ jobs:
30
45
run : yarn lint
31
46
working-directory : ${{ env.WORKING_DIRECTORY }}
32
47
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
35
125
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