-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
268 lines (244 loc) · 10.2 KB
/
action.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
name: Pages Preview
author: Federico Grandi <[email protected]>
description: Deploy GitHub Pages previews for your branches and PRs
branding:
color: gray-dark
icon: upload-cloud
inputs:
# Required
build_dir:
description: The directory in which the website has been built, in the a/b/c format
required: true
preview_base_url:
description: The GitHub Pages base URL of the preview repo (e.g. https://octocat.github.io/preview)
required: true
preview_repo:
description: The repository to push previews to, in the Owner/Name format
required: true
preview_token:
description: The token to access the preview repo
required: true
# Not required
deployment_env:
description: The name of the environment to use for the deployment
default: 'preview'
required: false
deployments:
description: Whether to use the deployments API
default: 'true'
required: false
git_author_name:
description: The name of the author of the resulting commit
default: ${{ github.actor }}
required: false
git_author_email:
description: The email of the author of the resulting commit
default: ${{ github.actor }}@users.noreply.github.com
required: false
git_committer_name:
description: The committer of the resulting commit
# default: copies git_author_email at runtime
required: false
git_committer_email:
description: The email of the committer of the resulting commit
# default: copies git_author_email at runtime
required: false
pr_comment:
description: Whether to comment on PRs
default: 'true'
required: false
preview_branch:
description: The name of the branch that hosts the previews
default: gh-pages
required: false
preview_workflow_file_name:
description: The name of the workflow file that contains the comment workflow in the preview repo. If you use my template, the name is already 'preview.yml'
default: preview.yml
required: false
runs:
using: composite
steps:
- name: Parse what the action has to perform
run: ${{ github.action_path }}/lib/parse_action.sh
shell: bash
id: parse_action
# outputs: action, path
env:
EVENT_NAME: ${{ github.event_name }}
EVENT_TYPE: ${{ github.event.action }}
PR_NUMBER: ${{ github.event.number }}
REF: ${{ github.event.ref.ref || github.event.ref }} # event.ref.ref for delete, event.ref for push
REF_TYPE: ${{ github.event.ref_type || github.ref_type }} # event.ref_type for delete, ref_type for push
REPO_NAME: ${{ github.repository }}
- name: Create a new deployment
if: ${{ steps.parse_action.outputs.action == 'deploy' && inputs.deployments == 'true' }}
id: deployment
uses: EndBug/deployments@140-task
with:
step: start
env: ${{ inputs.deployment_env }}
ref: ${{ github.event.pull_request.head.sha || github.head_ref || github.ref }}
task: deploy:${{ steps.parse_action.outputs.path }}
- name: Set up the preview repo
if: ${{ steps.parse_action.outputs.action != 'none' }}
shell: bash
run: |
cd ${{ github.workspace }}/..
git clone "https://git:${{ inputs.preview_token }}@github.com/${{ inputs.preview_repo }}.git" preview_${{ github.sha }}
cd preview_${{ github.sha }}
git config --global user.name ${{ inputs.git_author_name }}
git config --global user.email ${{ inputs.git_author_email }}
echo "GIT_COMMITTER_NAME=${{ inputs.git_committer_name || inputs.git_author_name }}" >> $GITHUB_ENV
echo "GIT_COMMITTER_EMAIL=${{ inputs.git_committer_email || inputs.git_author_email }}" >> $GITHUB_ENV
git checkout ${{ inputs.preview_branch }}
- name: Copy the build directory to the destination and commit the changes
if: ${{ steps.parse_action.outputs.action == 'deploy' }}
shell: bash
working-directory: ../preview_${{ github.sha }}
run: |
git rm "${{ steps.parse_action.outputs.path }}" -r --ignore-unmatch
mkdir -p "${{ steps.parse_action.outputs.path }}" && cp "${{ github.workspace }}/${{ inputs.build_dir }}/." "$_" -a
git add "${{ steps.parse_action.outputs.path }}"
git commit --message="ci: deploy preview for ${{ steps.parse_action.outputs.path }}
Commit created with [EndBug/pages-preview](https://github.com/EndBug/pages-preview)" --allow-empty
git push origin ${{ inputs.preview_branch }}
- name: Remove the destination directory and commit the changes
if: ${{ steps.parse_action.outputs.action == 'remove' }}
shell: bash
working-directory: ../preview_${{ github.sha }}
run: |
git rm "${{ steps.parse_action.outputs.path }}" -r --ignore-unmatch
git commit --message="ci: remove preview for ${{ steps.parse_action.outputs.path }}
Commit created with [EndBug/pages-preview](https://github.com/EndBug/pages-preview)" --allow-empty
git push origin ${{ inputs.preview_branch }}
- name: Check whether there has been any change in the preview repo
if: ${{ steps.parse_action.outputs.action != 'none' }}
id: check_changes
shell: bash
working-directory: ../preview_${{ github.sha }}
run: |
changed=$(git diff --exit-code --quiet HEAD^..HEAD && echo "false" || echo "true")
echo "changed=$changed" >> $GITHUB_OUTPUT
echo "changed: $changed"
- name: Get current ref & commit date
if: ${{ steps.parse_action.outputs.action != 'none' }}
id: current_ref
shell: bash
working-directory: ../preview_${{ github.sha }}
# outputs: sha, date
run: |
sha=$(git rev-parse HEAD)
echo "sha=$sha" >> $GITHUB_OUTPUT
echo "sha: $sha"
date=$(git show -s --format=%ci HEAD)
echo "date=$date" >> $GITHUB_OUTPUT
echo "date: $date"
- name: Parse preview repo owner and name
if: ${{ steps.parse_action.outputs.action != 'none' }}
id: parse_preview_repo
shell: bash
# outputs: owner, name
run: |
owner=$(cut -d "/" -f 1 <<<"${{ inputs.preview_repo }}")
echo "owner=$owner" >> $GITHUB_OUTPUT
echo "owner: $owner"
name=$(cut -d "/" -f 2 <<<"${{ inputs.preview_repo }}")
echo "name=$name" >> $GITHUB_OUTPUT
echo "name: $name"
- name: Trigger deployment in the preview repo
uses: convictional/[email protected]
if: ${{ steps.parse_action.outputs.action != 'none' && inputs.deployments == 'true' && steps.check_changes.outputs.changed == 'true'}}
id: dispatch
with:
owner: ${{ steps.parse_preview_repo.outputs.owner }}
repo: ${{ steps.parse_preview_repo.outputs.name }}
github_token: ${{ inputs.preview_token }}
workflow_file_name: ${{ inputs.preview_workflow_file_name }}
wait_interval: 10
# VERSION: update before changing the major version
client_payload: >-
{
"ref": "${{ steps.current_ref.outputs.sha }}",
"version": "v1"
}
propagate_failure: true
- name: Set deployment status
if: ${{ always() && steps.parse_action.outputs.action == 'deploy' && inputs.deployments == 'true' }}
uses: EndBug/deployments@140-task
with:
step: finish
status: ${{ job.status }}
env: ${{ steps.deployment.outputs.env }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: ${{ inputs.preview_base_url }}/${{ steps.parse_action.outputs.path }}
override: false
auto_inactive: false
- name: Deactivate previous deployments
if: ${{ success() && inputs.deployments == 'true' }}
uses: actions/github-script@v7
with:
result-encoding: string
script: |
let deployments = (await github.rest.repos.listDeployments({
owner: context.repo.owner,
repo: context.repo.repo,
task: 'deploy:${{ steps.parse_action.outputs.path }}'
}))
.data
.filter(d => d.id != '${{ steps.deployment.outputs.deployment_id }}' && d.task == 'deploy:${{ steps.parse_action.outputs.path }}')
.map(d => d.id)
console.log(`Found ${deployments.length} deployments to deactivate`, deployments)
for (id of deployments) {
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: id,
state: 'inactive'
})
}
- name: Determine whether to comment on PR
id: pr_comment
shell: bash
run: |
result=${{
inputs.pr_comment == 'true' &&
startsWith(github.event_name, 'pull_request') &&
steps.parse_action.outputs.action != 'none' &&
steps.dispatch.outputs.conclusion == 'success'
}}
echo "result=$result" >> $GITHUB_OUTPUT
echo "result: $result"
- name: Parse source repo owner and name
if: ${{ steps.pr_comment.outputs.result == 'true' }}
id: parse_source_repo
shell: bash
# outputs: owner, name
run: |
owner=$(cut -d "/" -f 1 <<<"${{ github.repository }}")
echo "owner=$owner" >> $GITHUB_OUTPUT
echo "owner: $owner"
name=$(cut -d "/" -f 2 <<<"${{ github.repository }}")
echo "name=$name" >> $GITHUB_OUTPUT
echo "name: $name"
- name: Comment on deployment
if: ${{ steps.pr_comment.outputs.result == 'true' && steps.parse_action.outputs.action == 'deploy' }}
uses: marocchino/sticky-pull-request-comment@v2
with:
repo: ${{ steps.parse_source_repo.outputs.name }}
message: |-
[Pages Preview](https://github.com/EndBug/pages-preview)
:---:
:rocket: Deployed preview to ${{ inputs.preview_base_url }}/${{ steps.parse_action.outputs.path }}
on branch [`${{ inputs.preview_branch }}`](${{ github.server_url }}/${{ inputs.preview_repo }}/tree/${{ inputs.preview_branch }}) at ${{ steps.current_ref.outputs.date }}
number: ${{ github.event.number }}
- name: Comment on removal
if: ${{ steps.pr_comment.outputs.result == 'true' && steps.parse_action.outputs.action == 'remove' }}
uses: marocchino/sticky-pull-request-comment@v2
with:
repo: ${{ steps.parse_source_repo.outputs.name }}
message: |-
[Pages Preview](https://github.com/EndBug/pages-preview)
:---:
Preview removed because the pull request was closed.
${{ steps.current_ref.outputs.date }}
number: ${{ github.event.number }}