-
Notifications
You must be signed in to change notification settings - Fork 1.4k
188 lines (171 loc) · 6.83 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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]
permissions:
contents: read
pull-requests: write
actions: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Get PR/workflow run info
id: get-info
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
console.dir(context, { depth: null });
let pr;
let workflowRun;
let isLabel = false;
switch (context.eventName) {
case "workflow_dispatch":
console.log("Workflow dispatch event");
pr = (await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: +context.payload.inputs.pr,
})).data;
break;
case "pull_request_target":
console.log("Pull request target event");
pr = context.payload.pull_request;
break;
case "workflow_run":
console.log("Workflow run event");
workflowRun = context.payload.workflow_run;
pr = workflowRun.pull_requests[0];
if (pr) {
console.log("PR found in workflow run");
// Reload the PR to get the labels.
pr = (await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
})).data;
} else {
const q = `is:pr is:open repo:${context.repo.owner}/${context.repo.repo} sha:${workflowRun.head_sha}`;
console.log(`PR not found in workflow run, searching for it with ${q}`);
// PRs sent from forks do not get the pull_requests field set.
// https://github.com/orgs/community/discussions/25220
const response = await github.rest.search.issuesAndPullRequests({ q });
if (response.data.items.length > 0) {
pr = response.data.items[0];
}
}
}
if (!pr) {
console.log("Could not find PR");
return null;
}
console.log(`Found PR ${pr.html_url}`);
console.dir(pr, { depth: null });
if (pr.state !== "open") {
console.log(`PR ${pr.number} is not open`);
return null;
}
if (!pr.labels.some((label) => label.name === "deploy-preview")) {
console.log(`PR ${pr.number} does not have the deploy-preview label`);
return null;
}
if (!workflowRun) {
console.log(`No workflow run found in event, searching for it with ${pr.head.sha}`);
try {
workflowRun = (await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "CI.yml",
head_sha: pr.head.sha,
per_page: 1,
})).data.workflow_runs[0];
} catch (e) {
console.log(e);
}
}
if (!workflowRun) {
console.log(`Could not find workflow run for PR ${pr.number}`);
return null;
}
console.log(`Found workflow run ${workflowRun.html_url}`);
console.dir(workflowRun, { depth: null });
try {
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: "microsoft",
repo: "TypeScript-Website",
run_id: workflowRun.id,
});
console.dir(artifacts, { depth: null });
if (!artifacts.data.artifacts.some(x => x.name === "site")) {
console.log("No artifact found in workflow run");
return null;
}
} catch (e) {
console.log(e);
return null;
}
const result = { pr: pr.number, runId: workflowRun.id };
console.log(result);
return result;
- name: Download site build from PR
if: ${{ steps.get-info.outputs.result != 'null' }}
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: site
path: site
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ fromJson(steps.get-info.outputs.result).runId }}
- name: Deploy
id: deploy
if: ${{ steps.get-info.outputs.result != 'null' }}
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_PREVIEW }}
# Unsetting the repo token so we can control commenting ourselves.
# repo_token: ${{ secrets.GITHUB_TOKEN }}
action: "upload"
app_location: "site"
skip_app_build: true
production_branch: v2
deployment_environment: ${{ fromJson(steps.get-info.outputs.result).pr }}
- name: Comment on PR
if: ${{ steps.get-info.outputs.result != 'null' }}
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
PR_NUMBER: ${{ fromJson(steps.get-info.outputs.result).pr }}
SITE_URL: ${{ steps.deploy.outputs.static_web_app_url }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prefix = "Azure Static Web Apps: Your stage site is ready!";
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: +process.env.PR_NUMBER,
per_page: 100,
});
for (const comment of comments) {
if (comment.user?.login === "github-actions[bot]" && comment.body?.startsWith(prefix)) {
console.log(`Deleting comment ${comment.id}`);
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id,
});
}
}
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: +process.env.PR_NUMBER,
body: `${prefix} Visit it here: ${process.env.SITE_URL}`
});