-
Notifications
You must be signed in to change notification settings - Fork 46
132 lines (123 loc) · 4.61 KB
/
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
name: 'Preview'
on:
pull_request:
types:
- synchronize
- opened
- closed
env:
BRANCH: ${{ github.event.pull_request.head.ref }}
CC_CLEVER_TOOLS_PREVIEWS_CELLAR_KEY_ID: ${{ secrets.CC_CLEVER_TOOLS_PREVIEWS_CELLAR_KEY_ID }}
CC_CLEVER_TOOLS_PREVIEWS_CELLAR_SECRET_KEY: ${{ secrets.CC_CLEVER_TOOLS_PREVIEWS_CELLAR_SECRET_KEY }}
jobs:
wait_for_build:
name: 'Wait for build to succeed'
if: |
(github.event.action == 'synchronize' || github.event.action == 'opened')
&& github.event.repository.fork == false
&& !startsWith(github.event.pull_request.head.ref, 'release-please--')
runs-on: ubuntu-latest
steps:
- name: 'Wait for build to succeed'
uses: lewagon/[email protected]
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
check-name: Build
ref: ${{ github.event.pull_request.head.sha }}
allowed-conclusions: success
publish:
name: 'Publish Preview'
needs: wait_for_build
runs-on: ubuntu-latest
steps:
- name: '[Prepare] Checkout'
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: '[Prepare] Setup Node.js'
uses: actions/setup-node@v3
with:
node-version-file: 'package.json'
cache: 'npm'
- name: '[Prepare] Install dependencies'
run: npm ci
- name: '[Prepare] Check if a preview already exists'
id: check_preview
run: |
if node scripts/job-preview.js get ${BRANCH} > /dev/null 2>&1; then
echo "exists=yes" >> $GITHUB_OUTPUT
else
echo "exists=no" >> $GITHUB_OUTPUT
fi
- name: '[Run] Build preview'
run: node scripts/job-preview.js build ${BRANCH}
- name: '[Run] Publish preview'
run: node scripts/job-preview.js publish ${BRANCH}
- name: '[Finalize] Get preview links'
id: get_preview_links
run: |
PREVIEW_LINKS=$(node scripts/job-preview.js links $BRANCH | sed -z 's/\n/\\n/g'; echo)
echo "PREVIEW_LINKS=${PREVIEW_LINKS::-2}" >> $GITHUB_ENV
- name: '[Finalize] Add comment'
if: steps.check_preview.outputs.exists == 'no'
uses: actions/github-script@v6
with:
script: |
const links = "${{ env.PREVIEW_LINKS }}";
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `🔎 A preview has been automatically published:\n\n${links}\n\n\n_This preview will be deleted once this PR is closed._`
});
- name: '[Finalize] Edit comment'
if: steps.check_preview.outputs.exists == 'yes'
uses: actions/github-script@v6
with:
script: |
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
const comment = comments.data.find((comment) => comment.body.startsWith(`🔎 A preview has been automatically published:`));
if (comment != null) {
const links = "${{ env.PREVIEW_LINKS }}";
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id,
body: `🔎 A preview has been automatically published:\n\n${links}\n\n\n_This preview will be deleted once this PR is closed._`
});
}
delete:
if: |
github.event.action == 'closed'
&& github.event.repository.fork == false
&& !startsWith(github.event.pull_request.head.ref, 'release-please--')
name: 'Delete Preview'
runs-on: ubuntu-latest
steps:
- name: '[Prepare] Checkout'
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: '[Prepare] Setup Node.js'
uses: actions/setup-node@v3
with:
node-version-file: 'package.json'
cache: 'npm'
- name: '[Prepare] Install dependencies'
run: npm ci
- name: '[Run] Delete preview'
run: node scripts/job-preview.js delete ${BRANCH}
- name: '[Finalize] Add comment'
uses: actions/github-script@v6
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `🔎 The preview has been automatically deleted.`
});