-
Notifications
You must be signed in to change notification settings - Fork 4
173 lines (160 loc) · 6.87 KB
/
deploy-pr.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
name: Deploy PR
on:
workflow_run:
workflows: [build-pr]
types: [completed]
jobs:
deploy-pr:
runs-on: ubuntu-latest
if: |
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
permissions:
deployments: write
steps:
- name: Download build artifact
uses: actions/github-script@d556feaca394842dc55e4734bf3bb9f685482fa0
with:
script: |
let artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.event.workflow_run.id }},
});
artifacts = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "build"
});
if (artifacts.length <= 0) {
throw new Error("No artifact found.");
}
const matchArtifact = artifacts[0];
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
const fs = require('fs');
fs.writeFileSync('${{github.workspace}}/artifact.zip', Buffer.from(download.data));
- name: Unzip artifact
run: unzip artifact.zip
- name: Get PR number
id: get-pr-number
uses: actions/github-script@d556feaca394842dc55e4734bf3bb9f685482fa0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require("fs");
const prId = parseInt(fs.readFileSync("./pr.txt", "utf8"), 10);
if (isNaN(prId) || prId <= 0) {
throw new Error("Invalid PR id.");
}
return prId;
# Normally github automatically displays a deployment status for workflows
# that are related to a specific commit. But since this workflow is triggered
# by the 'workflow_run' event, the commit sha for this run is the sha from
# the 'main' branch rather than the commit from the PR.
# So we'll manually create a deployment status, this also allows us to
# set the environment_url so that github displays a button that leads you
# directly to the deployed page.
- name: Create deployment for status
id: create-deployment
uses: actions/github-script@d556feaca394842dc55e4734bf3bb9f685482fa0
env:
BUILD_COMMIT_SHA: ${{ github.event.workflow_run.head_sha }}
PR_ID: ${{ steps.get-pr-number.outputs.result }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const {BUILD_COMMIT_SHA, PR_ID} = process.env;
const {data: deploymentData} = await github.request("POST /repos/{owner}/{repo}/deployments", {
owner: context.repo.owner,
repo: context.repo.repo,
ref: BUILD_COMMIT_SHA,
environment: "PR preview",
auto_merge: false,
transient_environment: true,
task: "deploy_pr:" + PR_ID,
required_contexts: ["build / build"],
});
return deploymentData.id;
- name: Update deployment status with in progress
if: ${{ failure() }}
uses: actions/github-script@d556feaca394842dc55e4734bf3bb9f685482fa0
env:
DEPLOYMENT_ID: ${{ steps.create-deployment.outputs.result }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const {DEPLOYMENT_ID} = process.env;
await github.request("POST /repos/{owner}/{repo}/deployments/{deploymentId}/statuses", {
owner: context.repo.owner,
repo: context.repo.repo,
deploymentId: DEPLOYMENT_ID,
state: "in_progress",
});
- name: Deploy files
env:
PR_DEPLOY_TOKEN: ${{ secrets.PR_DEPLOY_TOKEN }}
PR_ID: ${{ steps.get-pr-number.outputs.result }}
run: |
curl -X POST --fail -H "Authorization: DeployToken $PR_DEPLOY_TOKEN" -H "Content-Type: application/x-tar" --data-binary @build_files.tar "https://deploy.renda.studio/pr?id=$PR_ID"
- name: Update deployment status success
uses: actions/github-script@d556feaca394842dc55e4734bf3bb9f685482fa0
env:
DEPLOYMENT_ID: ${{ steps.create-deployment.outputs.result }}
PR_ID: ${{ steps.get-pr-number.outputs.result }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const {DEPLOYMENT_ID, PR_ID} = process.env;
await github.request("POST /repos/{owner}/{repo}/deployments/{deploymentId}/statuses", {
owner: context.repo.owner,
repo: context.repo.repo,
deploymentId: DEPLOYMENT_ID,
state: "success",
environment_url: `https://pr-${ PR_ID }.renda.studio`
});
// Delete previous deployments except for the one we just created.
const iterator = github.paginate.iterator("GET /repos/{owner}/{repo}/deployments", {
owner: context.repo.owner,
repo: context.repo.repo,
task: "deploy_pr:" + PR_ID,
});
const allDeployments = [];
for await (const {data: deployments} of iterator) {
for (const deployment of deployments) {
allDeployments.push(deployment);
}
}
for (const deployment of allDeployments) {
const deploymentId = deployment.id;
if (deploymentId == DEPLOYMENT_ID) continue;
console.log(`Deleting ${deploymentId}`);
await github.request("POST /repos/{owner}/{repo}/deployments/{deploymentId}/statuses", {
owner: context.repo.owner,
repo: context.repo.repo,
deploymentId,
state: "inactive",
});
await github.request("DELETE /repos/{owner}/{repo}/deployments/{deploymentId}", {
owner: context.repo.owner,
repo: context.repo.repo,
deploymentId,
});
}
- name: Update deployment status failure
if: ${{ failure() }}
uses: actions/github-script@d556feaca394842dc55e4734bf3bb9f685482fa0
env:
DEPLOYMENT_ID: ${{ steps.create-deployment.outputs.result }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const {DEPLOYMENT_ID} = process.env;
await github.request("POST /repos/{owner}/{repo}/deployments/{deploymentId}/statuses", {
owner: context.repo.owner,
repo: context.repo.repo,
deploymentId: DEPLOYMENT_ID,
state: "failure",
});