-
Notifications
You must be signed in to change notification settings - Fork 0
193 lines (177 loc) · 7.01 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
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
189
190
191
192
193
name: Preview
on:
issue_comment:
types: [created]
pull_request:
branches: [main]
jobs:
check:
runs-on: ubuntu-latest
outputs:
triggered: ${{ steps.check.outputs.triggered }}
triggered_by_ci: ${{ steps.checkCI.outputs.triggered }}
steps:
- name: Check if the comment was a command, and react with 👀 if it was
if: github.event.issue.pull_request
uses: famly/pull-request-comment-trigger@master
id: check
with:
trigger: "/preview"
reaction: eyes
prefix_only: true
env:
GITHUB_TOKEN: "${{ secrets.CI_GITHUB_TOKEN }}"
- name: Check if it was CI comment
if: github.event.issue.pull_request
uses: famly/pull-request-comment-trigger@master
id: checkCI
with:
trigger: "/preview CI"
env:
GITHUB_TOKEN: "${{ secrets.CI_GITHUB_TOKEN }}"
- name: Check if it was pull request open to main branch
if: github.event_name == 'pull_request'
uses: marocchino/sticky-pull-request-comment@v2
with:
recreate: true
number: ${{ github.event.issue.number || github.event.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
message: |
/preview CI
run-preview:
runs-on: ubuntu-latest
needs: [check]
if: ${{ needs.check.outputs.triggered == 'true' }}
concurrency:
group: ${{ github.event.issue.number || github.event.pull_request.number }}
cancel-in-progress: true
steps:
- name: Add info about starting new preview
uses: marocchino/sticky-pull-request-comment@v2
with:
recreate: true
number: ${{ github.event.issue.number || github.event.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
message: |
**🖼️ Preview Environment:** _Starting..._
- name: Extract current commit
id: sha
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const { owner, repo, number: pull_number } = context.issue;
const pr = await github.rest.pulls.get({
owner,
repo,
pull_number,
});
return pr.data.head.sha
- uses: actions/checkout@v3
with:
ref: ${{ steps.sha.outputs.result }}
- uses: actions/setup-node@v3
with:
node-version: 20
- name: Start frontend tunnel
id: expose-tunnel
uses: codetalkio/[email protected]
with:
service: localhost.run
port: 3000
- name: Fix tunnel url
id: tunnel-url
run: |
url=${{ steps.expose-tunnel.outputs.tunnel-url }}
fixedUrl=${url::-6}
echo "tunnel-url=${fixedUrl}" >> $GITHUB_OUTPUT
- name: Hide preview comment
uses: actions/github-script@v6
if: ${{ needs.check.outputs.triggered_by_ci != 'true' }}
with:
result-encoding: string
github-token: ${{ secrets.CI_GITHUB_TOKEN }}
script: |
const { node_id } = context.payload.comment;
const query = `
mutation minimizeComment($id: ID!) {
minimizeComment(input: { classifier: OUTDATED, subjectId: $id }) {
clientMutationId
}
}
`;
await github.graphql(query, { id: node_id });
- name: Add info about dependency install
uses: marocchino/sticky-pull-request-comment@v2
with:
recreate: true
number: ${{ github.event.issue.number || github.event.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
message: |
**🖼️ Preview Environment:** _Installing dependencies..._
- name: Install
run: npm ci
- name: Cache build
uses: actions/cache@v3
with:
path: ${{ github.workspace }}/frontend/.next/cache
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
- name: Add info about build
uses: marocchino/sticky-pull-request-comment@v2
with:
number: ${{ github.event.issue.number || github.event.pull_request.number }}
recreate: true
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
message: |
**🖼️ Preview Environment:** _Building..._
- name: Build frontend
run: |
touch .env
echo DIRECTUS_URL="https://directus.universe.nexus" >> .env
npm run build
- name: Starting app
run: npm start &
- name: Comment PR
uses: marocchino/sticky-pull-request-comment@v2
with:
number: ${{ github.event.issue.number || github.event.pull_request.number }}
recreate: true
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
message: |
**🖼️ Preview Environment:** ${{ steps.tunnel-url.outputs.tunnel-url }}
You can stop the preview by clicking "Cancel" on the [GitHub Action](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}).
- name: Wait for 60 minutes before timing out (unless overwritten)
run: |
echo "Tunnel URL is ${{ steps.tunnel-url.outputs.tunnel-url }}"
timeout 61m sleep 3600
# Gracefully exit if the timeout hits.
exit 0
- name: Close comment
uses: marocchino/sticky-pull-request-comment@v2
if: ${{ success() }}
with:
recreate: true
number: ${{ github.event.issue.number || github.event.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
message: |
🖼️ Preview Environment exceeded the maximum time of 60 minutes. To start it again, comment `/preview` on this PR.
- name: Close comment when cancelled
uses: marocchino/sticky-pull-request-comment@v2
if: ${{ cancelled() }}
with:
recreate: true
number: ${{ github.event.issue.number || github.event.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
message: |
🖼️ Preview Environment was cancelled (because of newer preview or manual cancellation). To start it again, comment `/preview` on this PR.
- name: Close comment when failed
uses: marocchino/sticky-pull-request-comment@v2
if: ${{ failure() }}
with:
recreate: true
number: ${{ github.event.issue.number || github.event.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
message: |
🖼️ Preview Environment failed to start. You can see more info here [GitHub Action](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}). To start it again, comment `/preview` on this PR.