Skip to content

Commit 56d1422

Browse files
authored
Autodeployment Improvements (#6)
* Switch to multiple environments & deployments * Add deployment permissions
1 parent 66b2dcb commit 56d1422

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

.github/workflows/deploy.yml

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Deploy to SST
1+
name: Deploy using SST
22

33
on:
44
push:
@@ -8,15 +8,16 @@ on:
88

99
permissions:
1010
contents: read
11+
deployments: write
1112
pull-requests: write
1213
issues: write
1314

1415
jobs:
1516
deploy-production:
1617
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
17-
environment: 'action-env'
18+
environment: production
1819
runs-on: ubuntu-latest
19-
20+
2021
steps:
2122
- name: Checkout code
2223
uses: actions/checkout@v4
@@ -26,8 +27,8 @@ jobs:
2627
- name: Setup Node.js
2728
uses: actions/setup-node@v4
2829
with:
29-
node-version: '20'
30-
cache: 'npm'
30+
node-version: "20"
31+
cache: "npm"
3132

3233
- name: Install dependencies
3334
run: npm ci
@@ -45,9 +46,11 @@ jobs:
4546
4647
deploy-preview:
4748
if: github.event_name == 'pull_request' && github.event.action != 'closed'
48-
environment: 'action-env'
49+
environment:
50+
name: preview
51+
url: ${{ steps.get-url.outputs.url }}
4952
runs-on: ubuntu-latest
50-
53+
5154
steps:
5255
- name: Checkout code
5356
uses: actions/checkout@v4
@@ -57,8 +60,8 @@ jobs:
5760
- name: Setup Node.js
5861
uses: actions/setup-node@v4
5962
with:
60-
node-version: '20'
61-
cache: 'npm'
63+
node-version: "20"
64+
cache: "npm"
6265

6366
- name: Install dependencies
6467
run: npm ci
@@ -82,6 +85,21 @@ jobs:
8285
URL=$(npx sst state export --stage pr-${{ github.event.number }} | jq -r '.latest.resources[] | select(.type == "sst:aws:Nextjs") | .outputs._metadata.url')
8386
echo "url=$URL" >> $GITHUB_OUTPUT
8487
88+
- name: Create/Update GitHub Deployment
89+
uses: actions/github-script@v7
90+
with:
91+
script: |
92+
const ref = context.payload.pull_request.head.sha;
93+
const { data: dep } = await github.rest.repos.createDeployment({
94+
owner: context.repo.owner, repo: context.repo.repo, ref,
95+
environment: 'preview', auto_merge: false, required_contexts: []
96+
});
97+
await github.rest.repos.createDeploymentStatus({
98+
owner: context.repo.owner, repo: context.repo.repo,
99+
deployment_id: dep.id, state: 'success',
100+
environment_url: '${{ steps.get-url.outputs.url }}'
101+
});
102+
85103
- name: Comment on PR
86104
uses: actions/github-script@v7
87105
with:
@@ -92,22 +110,22 @@ jobs:
92110
repo: context.repo.repo,
93111
issue_number: context.issue.number,
94112
});
95-
113+
96114
const botComment = comments.find(comment =>
97115
comment.user.type === 'Bot' &&
98116
comment.body.includes('## Preview Deployment')
99117
);
100-
118+
101119
const commentBody = `## Preview Deployment
102-
120+
103121
| Property | Value |
104122
|----------|-------|
105123
| Status | Deployed successfully |
106124
| Preview URL | [${{ steps.get-url.outputs.url }}](${{ steps.get-url.outputs.url }}) |
107125
| Stage | \`pr-${{ github.event.number }}\` |
108-
126+
109127
This preview deployment will be automatically removed when the PR is closed.`;
110-
128+
111129
if (botComment) {
112130
await github.rest.issues.updateComment({
113131
owner: context.repo.owner,
@@ -126,9 +144,9 @@ jobs:
126144
127145
remove-preview:
128146
if: github.event_name == 'pull_request' && github.event.action == 'closed'
129-
environment: 'action-env'
147+
environment: preview
130148
runs-on: ubuntu-latest
131-
149+
132150
steps:
133151
- name: Checkout code
134152
uses: actions/checkout@v4
@@ -138,8 +156,8 @@ jobs:
138156
- name: Setup Node.js
139157
uses: actions/setup-node@v4
140158
with:
141-
node-version: '20'
142-
cache: 'npm'
159+
node-version: "20"
160+
cache: "npm"
143161

144162
- name: Install dependencies
145163
run: npm ci
@@ -155,6 +173,23 @@ jobs:
155173
run: |
156174
npx sst remove --stage pr-${{ github.event.number }}
157175
176+
- name: Mark Deployment Inactive (best-effort)
177+
uses: actions/github-script@v7
178+
with:
179+
script: |
180+
try {
181+
const { data: deployments } = await github.rest.repos.listDeployments({
182+
owner: context.repo.owner, repo: context.repo.repo, environment: 'preview',
183+
sha: context.payload.pull_request?.head?.sha
184+
});
185+
for (const d of deployments) {
186+
await github.rest.repos.createDeploymentStatus({
187+
owner: context.repo.owner, repo: context.repo.repo,
188+
deployment_id: d.id, state: 'inactive'
189+
});
190+
}
191+
} catch (e) { core.warning(String(e)); }
192+
158193
- name: Comment on PR
159194
uses: actions/github-script@v7
160195
with:
@@ -165,12 +200,12 @@ jobs:
165200
repo: context.repo.repo,
166201
issue_number: context.issue.number,
167202
});
168-
203+
169204
const botComment = comments.find(comment =>
170205
comment.user.type === 'Bot' &&
171206
comment.body.includes('## Preview Deployment')
172207
);
173-
208+
174209
if (botComment) {
175210
await github.rest.issues.updateComment({
176211
owner: context.repo.owner,

0 commit comments

Comments
 (0)