forked from rossjrw/pr-preview-action
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaction.yml
190 lines (158 loc) · 5.84 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
name: Deploy PR Preview
author: Ross Williams
description: >
Deploy a pull request preview to GitHub Pages, similar to Vercel and
Netlify.
branding:
icon: git-pull-request
color: yellow
inputs:
# Main parameters
preview-branch:
description: Branch on which the previews will be deployed.
required: false
default: gh-pages
umbrella-dir:
description: Name of the directory containing all previews.
required: false
default: pr-preview
source-dir:
description: >
Directory containing files to deploy.
Required when deploying a preview; will raise a warning if specified
when removing a preview.
required: false
default: ""
action:
description: >
Determines what this action will do when it is executed. Supported
values: `deploy`, `remove`.
If set to `deploy`, will attempt to deploy the preview and overwrite
any existing preview in that location.
If set to `remove`, will attempt to remove the preview in that
location.
required: true
# Parameters for simulating a pull request
pr-number:
description: >
The number of the PR to comment on when updating the preview
deployment. If the initiating event is `pull_request` or
`pull_request_target`, defaults to the number of the initiating PR.
Required otherwise.
required: false
default: ${{ github.event.number }}
outputs:
deployment-url:
description: The URL at which the preview has been deployed
value: ${{ steps.url.outputs.url }}
runs:
using: composite
steps:
- name: Store environment variables and validate inputs
env:
action: ${{ inputs.action }}
umbrella: ${{ inputs.umbrella-dir }}
source_dir: ${{ inputs.source-dir }}
pr: ${{ inputs.pr-number }}
actionref: ${{ github.action_ref }}
actionrepo: ${{ github.action_repository }}
run: |
if [ -z "$action" ]; then
echo "::error::Action input is required (deploy/remove)" >&2
exit 1
fi
if [ "$action" != "deploy" ] && [ "$action" != "remove" ]; then
echo "::error::Unknown action $action" >&2
exit 1
fi
if [ "$action" = "deploy" ] && [ -z "$source_dir" ]; then
echo "::error::Source dir must be defined when deploying" >&2
exit 1
fi
if [ "$action" = "remove" ] && [ -n "$source_dir" ]; then
echo "::warning::Source dir should not be defined when removing" >&2
fi
if [ -z "$pr" ]; then
echo "::error::PR number required for non-pull_request event" >&2
exit 1
fi
echo "action=$action" >> "$GITHUB_ENV"
echo "targetdir=$umbrella/pr-$pr" >> "$GITHUB_ENV"
echo "pr=$pr" >> "$GITHUB_ENV"
org=$(echo "$GITHUB_REPOSITORY" | cut -d "/" -f 1 | tr '[:upper:]' '[:lower:]' )
thirdleveldomain=$(echo "$GITHUB_REPOSITORY" | cut -d "/" -f 2 | cut -d "." -f 1 | tr '[:upper:]' '[:lower:]' )
echo "debug_org=$org" >> $GITHUB_ENV
echo "debug_thirdleveldomain=$thirdleveldomain" >> $GITHUB_ENV
if [ "$org" == "$thirdleveldomain" ]; then
pagesurl="${org}.github.io"
else
pagesurl=$(echo "$GITHUB_REPOSITORY" | sed 's/\//.github.io\//')
fi
echo "pagesurl=$pagesurl" >> $GITHUB_ENV
echo "emptydir=$(mktemp -d)" >> "$GITHUB_ENV"
echo "datetime=$(date '+%Y-%m-%d %H:%M %Z')" >> "$GITHUB_ENV"
echo "actionref=$actionref" >> "$GITHUB_ENV"
echo "actionrepo=$actionrepo" >> "$GITHUB_ENV"
shell: bash
- name: Determine action version
run: |
version=$(
"${{ github.action_path }}/lib/find-current-git-tag.sh" \
-p $actionrepo -f $actionref
)
echo "action_version=$version" >> "$GITHUB_ENV"
shell: bash
- name: Deploy preview directory
if: env.action == 'deploy'
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: ${{ inputs.preview-branch }}
folder: ${{ inputs.source-dir }}
target-folder: ${{ env.targetdir }}
commit-message: Deploy preview for PR ${{ env.pr }} 🛫
force: false
- name: Expose deployment URL
id: url
run: echo "::set-output name=url::https://${{ env.pagesurl }}/${{ env.targetdir }}/"
shell: bash
- name: Leave a comment after deployment
if: env.action == 'deploy' && env.deployment_status == 'success'
uses: marocchino/sticky-pull-request-comment@v2
with:
header: pr-preview
number: ${{ env.pr }}
message: "\
[PR Preview Action]\
(${{ github.server_url }}/${{ env.actionrepo }})
${{ env.action_version }}
:---:
🛫 Deployed preview to
https://${{ env.pagesurl }}/${{ env.targetdir }}/
on branch [`${{ inputs.preview-branch }}`](\
${{ github.server_url }}/${{ github.repository }}\
/tree/${{ inputs.preview-branch }})
at ${{ env.datetime }}
"
- name: Remove preview directory
if: env.action == 'remove'
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: ${{ inputs.preview-branch }}
folder: ${{ env.emptydir }}
target-folder: ${{ env.targetdir }}
commit-message: Remove preview for PR ${{ env.pr }} 🛬
force: false
- name: Leave a comment after removal
if: env.action == 'remove' && env.deployment_status == 'success'
uses: marocchino/sticky-pull-request-comment@v2
with:
header: pr-preview
number: ${{ env.pr }}
message: "\
[PR Preview Action]\
(${{ github.server_url }}/${{ env.actionrepo }})
${{ env.action_version }}
:---:
🛬 Preview removed because the pull request was closed.
${{ env.datetime }}
"