-
Notifications
You must be signed in to change notification settings - Fork 1.4k
168 lines (151 loc) · 6.27 KB
/
deploy-preview.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
name: Deploy preview environment
on:
workflow_run:
workflows: [CI]
types: [completed]
workflow_dispatch:
inputs:
pr:
required: true
type: string
description: PR number
pull_request_target:
types: [labeled]
jobs:
deploy:
if: github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Download site build
if: github.event_name == 'workflow_run'
uses: actions/github-script@v6
id: download-from-workflow-run
with:
result-encoding: string
script: |
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
const artifact = allArtifacts.data.artifacts.find(artifact => {
return artifact.name.startsWith('site-');
});
if (!artifact) {
console.log('No site artifact found');
return;
}
const prNumber = artifact.name.split('-')[1];
console.log('Detected PR number from artifact: ', prNumber);
// Check if the PR has the "deploy-preview" label
const pr = (await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
})).data;
if (!pr.labels.some(label => label.name === 'deploy-preview')) {
console.log('PR does not have the "deploy-preview" label');
return;
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
const fs = require('fs');
fs.writeFileSync('site.zip', Buffer.from(download.data));
return prNumber;
- name: Download site build
if: github.event_name == 'pull_request_target' || github.event_name == 'workflow_dispatch'
uses: actions/github-script@v6
id: download-from-pr
with:
result-encoding: string
script: |
const prNumber = context.eventName === 'pull_request_target'
? context.payload.pull_request.number
: context.payload.inputs.pr;
if (context.eventName === 'pull_request_target' && context.payload.label.name !== 'deploy-preview') {
console.log('Label is not "deploy-preview"');
return;
}
let pr;
if (context.eventName === 'workflow_dispatch') {
pr = (await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
})).data;
} else {
pr = context.payload.pull_request;
}
// https://github.com/orgs/community/discussions/24709
const workflowRun = (await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: 'CI.yml',
branch: pr.head.ref,
event: 'pull_request',
status: 'success',
per_page: 1,
})).data.workflow_runs[0];
if (!workflowRun) {
console.log('No matching workflow run found from branch ', pr.head.ref);
return;
}
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: workflowRun.id,
});
const artifact = allArtifacts.data.artifacts.find(artifact => {
return artifact.name.startsWith('site-');
});
if (!artifact) {
console.log('No site artifact found');
return;
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
const fs = require('fs');
fs.writeFileSync('site.zip', Buffer.from(download.data));
return prNumber;
- name: Unzip site build
if: steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result
run: unzip site.zip -d site
- run: ls -R
if: steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result
working-directory: site
- name: Deploy
id: deploy
if: steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_PROD }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: "upload"
app_location: "site"
skip_app_build: true
production_branch: v2
deployment_environment: ${{ steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result }}
- name: Comment on PR
# azure/static-web-apps-deploy seems to comment itself when the event is pull_request_target
if: github.event_name != 'pull_request_target' && (steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result)
uses: actions/github-script@v6
env:
PR_NUMBER: ${{ steps.download-from-workflow-run.outputs.result || steps.download-from-pr.outputs.result }}
SITE_URL: ${{ steps.deploy.outputs.static_web_app_url }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: process.env.PR_NUMBER,
body: `Azure Static Web Apps: Your stage site is ready! Visit it here: ${process.env.SITE_URL}`
});