forked from cloudflare/cloudflare-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
186 lines (178 loc) · 6.7 KB
/
preview.yml
File metadata and controls
186 lines (178 loc) · 6.7 KB
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
name: Preview
on: pull_request
jobs:
preview:
name: Check preview label
outputs:
result: ${{ steps.step1.outputs.result || 'false' }}
runs-on: ubuntu-latest
steps:
- name: Set env variable to true when label is present
id: step1
if: ${{ contains( github.event.pull_request.labels.*.name, 'preview' ) }}
run: echo "::set-output name=result::true"
changes:
name: "Check for docs changes"
needs: preview
if: ${{ needs.preview.outputs.result == 'true' }}
runs-on: ubuntu-latest
defaults:
run:
shell: bash
outputs:
products: ${{ steps.filter.outputs.products }}
steps:
- uses: actions/checkout@v2
- uses: dorny/paths-filter@v2.4.2
id: filter
with:
list-files: shell
filters: |
products:
- 'products/**'
- run: echo ${{ steps.filter.outputs.products_files }}
- name: Setup NodeJS
uses: actions/setup-node@v1
with:
node-version: 14.2.0
- name: Get PR number
env:
PRNUMBER: ${{ steps.filter.outputs.products == 'true' && format(github.event.number) || '0' }}
run: echo "pr_number=${{ env.PRNUMBER }}" >> $GITHUB_ENV
- name: Checkout changes
if: ${{ env.pr_number != '0' }}
uses: actions/checkout@v2
with:
ref: ${{ github.event.workflow_run.head_sha }}
- name: Checkout docs engine
if: ${{ env.pr_number != '0' }}
uses: actions/checkout@v2
with:
repository: 'cloudflare/cloudflare-docs-engine'
path: 'engine'
- name: Install docs engine
if: ${{ env.pr_number != '0' }}
run: |
cd $GITHUB_WORKSPACE/engine
npm link
- name: Build updated docs
if: ${{ env.pr_number != '0' }}
run: |
while read tile; do
echo "Building $tile..."
if [ $tile == "developers.cloudflare.com" ]; then
pushd developers.cloudflare.com
npm install
npm run build
popd
else
if [ ! -d "products/$tile" ]; then
echo "Warning: Skipping build because the 'products/$tile' directory no longer exists"
continue
fi
pushd products/$tile
npm link cloudflare-docs-engine
mkdir .docs
npm run ghactionsbootstrap
npm run build
popd
fi
done < <(echo ${{ steps.filter.outputs.products_files }} | node ./get-products-from-file-paths.js)
env:
WORKERS_ENV: development
- name: Publish preview
if: ${{ env.pr_number != '0' }}
run: |
export WRANGLER_HOME="$HOME"
mkdir -p "$HOME/.wrangler"
chmod -R 770 "$HOME/.wrangler"
export CF_API_TOKEN=${{ secrets.CF_API_TOKEN }}
npm install "@cloudflare/wrangler" -g
COPIED="false"
while read tile; do
if [ $tile == "developers.cloudflare.com" ]; then
pushd developers.cloudflare.com
if [ $COPIED == "false" ]; then
cp wrangler.toml ${GITHUB_WORKSPACE}/
COPIED=true
fi
echo "
[env.preview]
# Opt into backwards-incompatible changes through September 17, 2021.
compatibility_date = "2021-09-17"
name = \"pr-$prnumber\"
type = \"webpack\"
route = \"https://pr-$prnumber.cloudflare-docs.workers.dev/*\"
account_id = \"b54f07a6c269ecca2fa60f1ae4920c99\"
workers_dev = \"true\" " >> wrangler.toml
wrangler publish -e $ENVIRONMENT
popd
else
if [ ! -d "products/$tile" ]; then
echo "Warning: Skipping publish because the 'products/$tile' directory no longer exists"
continue
fi
pushd products/$tile
if [ $COPIED == "false" ]; then
cp wrangler.toml ${GITHUB_WORKSPACE}/
COPIED=true
fi
echo "
[env.preview]
# Opt into backwards-incompatible changes through September 17, 2021.
compatibility_date = "2021-09-17"
name = \"pr-$prnumber-$tile\"
type = \"webpack\"
account_id = \"b54f07a6c269ecca2fa60f1ae4920c99\"
workers_dev = \"true\" " >> wrangler.toml
wrangler publish -e $ENVIRONMENT
popd
fi
done < <(echo ${{ steps.filter.outputs.products_files }} | node ./get-products-from-file-paths.js)
cd ${GITHUB_WORKSPACE}
# iconv removes emoji in output; xargs does a trim
echo "SUBDOMAIN=$(wrangler subdomain | iconv -c -f utf-8 -t ascii | xargs)" >> $GITHUB_ENV
env:
prnumber: ${{env.pr_number}}
ENVIRONMENT: preview
CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
- name: Build PR comment with URLs
if: ${{ env.pr_number != '0' }}
id: build-comment
run: |
BASE_URL=$(echo "https://pr-${PR}.${SUBDOMAIN}")
# %0A is escaped newline:
# https://github.com/peter-evans/create-or-update-comment/issues/51
MESSAGE="[Previewer] Preview up to commit \`${SHA::7}\` available at the following URIs:%0A"
while read tile; do
if [ $tile == "developers.cloudflare.com" ]; then
CURR_URL=${BASE_URL}
MESSAGE="${MESSAGE}- ${CURR_URL}%0A"
else
if [ ! -d "products/$tile" ]; then
continue
fi
CURR_URL="https://pr-${PR}-${tile}.${SUBDOMAIN}"
MESSAGE="${MESSAGE}- ${CURR_URL}/${tile}%0A"
fi
done < <(echo ${{ steps.filter.outputs.products_files }} | node ./get-products-from-file-paths.js)
echo ::set-output name=body::$MESSAGE
env:
SUBDOMAIN: ${{ env.SUBDOMAIN }}
PR: ${{ env.pr_number }}
SHA: ${{ github.event.pull_request.head.sha }}
- name: Find comment about preview
if: ${{ env.pr_number != '0' }}
uses: peter-evans/find-comment@v1
id: find-comment
with:
issue-number: ${{ env.pr_number }}
body-includes: "[Previewer]"
- name: Create or update comment
if: ${{ env.pr_number != '0' }}
uses: peter-evans/create-or-update-comment@v1
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ env.pr_number }}
body: ${{ steps.build-comment.outputs.body }}
edit-mode: replace