-
Notifications
You must be signed in to change notification settings - Fork 2
129 lines (116 loc) · 4.31 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
name: Deploy preview
on:
workflow_dispatch:
inputs:
pr:
required: true
type: string
description: PR number
workflow_run:
workflows: [ci]
types: [completed]
pull_request_target:
types: [labeled]
concurrency:
group: preview
jobs:
deploy:
if: ${{ github.event_name == 'workflow_dispatch' || (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success') || (github.event_name == 'pull_request_target' && github.event.label.name == 'deploy-preview') }}
runs-on: ubuntu-latest
steps:
- name: Get PR/workflow run info
id: get-info
uses: actions/github-script@v7
with:
script: |
let pr;
let workflowRun;
let isLabel = false;
switch (context.eventName) {
case "workflow_dispatch":
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":
pr = context.payload.pull_request;
break;
case "workflow_run":
workflowRun = context.payload.workflow_run;
pr = workflowRun.pull_requests[0];
if (pr) {
// 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 {
// PRs sent from forks do not get the pull_requests field set.
// https://github.com/orgs/community/discussions/25220
prNumber = (await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: context.payload.workflow_run.head_sha,
})).data[0];
}
}
console.log("pr", pr);
if (!pr) {
throw new Error("Could not find PR");
}
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) {
try {
workflowRun = (await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "ci.yml",
head_sha: pr.head.sha,
})).data.workflow_runs[0];
} catch (e) {
console.log(e);
}
}
console.log("workflowRun", workflowRun);
if (!workflowRun) {
console.log(`Could not find workflow run for PR ${pr.number}`);
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@v4
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' }}
run: |
echo deploying site
ls -R site
echo azure_static_web_apps_api_token=$azure_static_web_apps_api_token
echo repo_token=$repo_token
echo action=$action
echo app_location=$app_location
echo skip_app_build=$skip_app_build
echo production_branch=$production_branch
echo deployment_environment=$deployment_environment
env:
azure_static_web_apps_api_token: AZURE_STATIC_WEB_APPS_API_TOKEN_PROD
repo_token: GITHUB_TOKEN
action: 'upload'
app_location: 'site'
skip_app_build: true
production_branch: v2
deployment_environment: ${{ fromJson(steps.get-info.outputs.result).pr }}