-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
207 lines (193 loc) · 8.29 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
name: Preview Pages
description: Action to deploy a preview GitHub Pages for each branch, pull request, commit.
branding:
icon: copy
color: blue
inputs:
source-dir:
description: >
Path of the directory you want to deploy.
This is the only required input.
required: true
token:
description: >
The GitHub App token or personal access token to access GitHub Pages publishing repository and commenting to the pull requests and branches.
Defaults to the repository scoped GitHub token (Which means that the repository you want to deploy GitHub Pages is the same one as you are running your workflow).
default: ${{ github.token }}
target-repository:
description: >
The repository you want to deploy GitHub Pages to, in the format of 'rajyan/preview-pages'.
Defaults to the current repository you are running the workflow.
default: ${{ github.repository }}
target-branch:
description: The branch you want to deploy GitHub Pages to.
default: gh-pages
target-dir:
description: |
The directory you want to deploy in the GitHub Pages repository.
Defaults the root directory.
default: .
configured-dir:
description: The directory configured in GitHub Pages repository as a source. (ex. docs)
default: .
configured-domain:
description: The custom domain configured in GitHub Pages repository.
# github-pages-deploy-action options
git-config-name:
description: >
Username of the commit when deploying to the GitHub Pages publishing repository.
See https://github.com/JamesIves/github-pages-deploy-action#optional-choices
default: github-actions[bot]
git-config-email:
description: >
Email of the commit when deploying to the GitHub Pages publishing repository.
See https://github.com/JamesIves/github-pages-deploy-action#optional-choices.
default: 41898282+github-actions[bot]@users.noreply.github.com
clean:
description: >
Whether to delete files in the destination that doesn't exist in source.
See https://github.com/JamesIves/github-pages-deploy-action#optional-choices.
clean-exclude:
description: >
Files or paths to exclude from clean.
See https://github.com/JamesIves/github-pages-deploy-action#optional-choices.
force:
description: >
Force-push on deployment. Default is false which is different from the original action.
See https://github.com/JamesIves/github-pages-deploy-action#optional-choices.
default: 'false'
tag:
description: >
Add a tag on commit.
See https://github.com/JamesIves/github-pages-deploy-action#optional-choices.
# pull request event options
pr-per-commit:
description: |
Whether to keep different pages per each commit SHA or overwrite per each pull request.
If 'true' the deploy directory for pull request will be '{{ inputs.target-dir }}/pr-{{ github.event.number }}/{{ github.sha }}'.
If 'false' the directory will be '{{ inputs.target-dir }}/pr-{{ github.event.number }}' and overwrites on each commit.
default: 'true'
pr-comment:
description: >
Whether to comment the preview page url in a pull request and how.
Valid choices are 'each' which posts a comment on each commit,
'append', 'sticky', 'recreate', 'hide_and_recreate' see https://github.com/marocchino/sticky-pull-request-comment for the options,
or 'none' if you don't need the comment.
default: 'each'
# push event options
branch-per-commit:
description: |
Whether to keep different pages per each commit SHA or overwrite per each branch.
If 'true' the deploy directory for pull request will be '{{ inputs.target-dir }}/{{ github.ref_name }}/{{ github.sha }}'.
If 'false' the directory will be '{{ inputs.target-dir }}/{{ github.ref_name }}' and overwrites on each commit.
default: 'true'
branch-comment:
description: >
Whether to comment the preview page url on the branches commit.
Valid options are 'each' or 'none'.
default: 'each'
outputs:
pages-url:
description: The deployed GitHub Pages url
value: ${{ steps.params.outputs.pages-url }}
upload-dir:
description: The deployed directory under {{inputs.target-dir}}
value: ${{ steps.params.outputs.upload-dir }}
runs:
using: composite
steps:
- name: Setup parameters
id: params
env:
upload_dir: >
${{
startsWith(github.event_name, 'pull_request')
&& format('pr-{0}/{1}', github.event.number, inputs.pr-per-commit == 'true' && github.sha || '')
|| format('{0}/{1}', github.ref_name, inputs.branch-per-commit == 'true' && github.sha || '')
}}
config_domain: ${{ inputs.configured-domain }}
config_dir: ${{ inputs.configured-dir }}
target_dir: ${{ inputs.target-dir }}
repo: ${{ inputs.target-repository }}
run: |
echo "upload-dir=$upload_dir" >> $GITHUB_OUTPUT
page_path=$(realpath -m -s --relative-to "$config_dir" "${target_dir}/${upload_dir}")
org=$(echo "$repo" | cut -d "/" -f 1)
repo_name=$(echo "$repo" | cut -d "/" -f 2)
if [ -z "$config_domain" ]; then
echo "pages-url=${org}.github.io/${repo_name}/${page_path}" >> $GITHUB_OUTPUT
else
echo "pages-url=${config_domain}/${page_path}" >> $GITHUB_OUTPUT
fi
shell: bash
- name: Setup comment body
id: comment
env:
sha: ${{ github.sha }}
server_url: ${{ github.server_url }}
pages_url: ${{ steps.params.outputs.pages-url }}
target_repository: ${{ inputs.target-repository }}
target_branch: ${{ inputs.target-branch }}
run: |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "body<<$EOF" >> $GITHUB_OUTPUT
echo "Deployed ${sha} to <https://${pages_url}>" >> $GITHUB_OUTPUT
echo "on [branch ${target_branch} in ${target_repository}](${server_url}/${target_repository}/tree/${target_branch})" >> $GITHUB_OUTPUT
echo "$EOF" >> $GITHUB_OUTPUT
shell: bash
- name: Deploy
id: deploy
uses: JamesIves/github-pages-deploy-action@5c6e9e9f3672ce8fd37b9856193d2a537941e66c #4.6.1
with:
token: ${{ inputs.token }}
repository-name: ${{ inputs.target-repository }}
branch: ${{ inputs.target-branch }}
folder: ${{ inputs.source-dir }}
target-folder: ${{ inputs.target-dir }}/${{ steps.params.outputs.upload-dir }}
git-config-name: ${{ inputs.git-config-name }}
git-config-email: ${{ inputs.git-config-email }}
clean: ${{ inputs.clean }}
clean-exclude: ${{ inputs.clean-exclude }}
force: ${{ inputs.force }}
tag: ${{ inputs.tag }}
- name: Comment preview link to pull request
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 #2.9.0
if: >
steps.deploy.outputs.deployment-status == 'success'
&& startsWith(github.event_name, 'pull_request')
&& inputs.pr-comment != 'none'
&& inputs.pr-comment != 'each'
with:
GITHUB_TOKEN: ${{ inputs.token }}
message: ${{ steps.comment.outputs.body }}
append: ${{ inputs.pr-comment == 'append' }}
recreate: ${{ inputs.pr-comment == 'recreate' }}
hide_and_recreate: ${{ inputs.pr-comment == 'hide_and_recreate' }}
- name: Comment preview link to commit
uses: actions/github-script@v7
if: >
steps.deploy.outputs.deployment-status == 'success'
&& (
github.event_name == 'push' && inputs.branch-comment == 'each'
|| startsWith(github.event_name, 'pull_request') && inputs.pr-comment == 'each'
)
env:
body: ${{ steps.comment.outputs.body }}
with:
github-token: ${{ inputs.token }}
script: |
if (context.eventName == 'push') {
github.rest.repos.createCommitComment({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: context.sha,
body: process.env.body,
})
} else {
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: process.env.body,
})
}