forked from mdn/content
-
Notifications
You must be signed in to change notification settings - Fork 0
174 lines (140 loc) · 6.53 KB
/
pr-test.yml
File metadata and controls
174 lines (140 loc) · 6.53 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
# This file tests more or less everything related to a pull request. All
# in one big job. At the end, if all the testing passes, it proceeds
# to upload all the files that were built to our Dev environment.
# This way, if the tests passed, you'll be able to review the built
# pages on a public URL.
name: PR Test
on:
pull_request:
permissions:
# Compare two commits.
contents: read
jobs:
tests:
if: github.repository == 'mdn/content'
runs-on: ubuntu-latest
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
# This is the directory where the built files will be placed.
# It's also hardcoded in the `npm run build` command in package.json.
# If you change it here, you must also make the same change in
# package.json.
BUILD_OUT_ROOT: build
steps:
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
with:
persist-credentials: false
- name: Get changed files
id: check
run: |
# Use the GitHub API to get the list of changed files
DIFF_DOCUMENTS=$(gh api repos/{owner}/{repo}/compare/${BASE_SHA}...${HEAD_SHA} \
--jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename')
FILTERED_MD_FILES=$(echo "$DIFF_DOCUMENTS" | egrep -i "^files/.*\.md$" || true)
FILTERED_IMAGE_FILES=$(echo "$DIFF_DOCUMENTS" | egrep -i "^files/.*\.(png|jpeg|jpg|gif|svg|webp)$" || true)
HAS_MD_FILES=false
HAS_IMAGE_FILES=false
# Check if we actually have content (not just empty strings)
if [ -n "${FILTERED_MD_FILES// /}" ]; then # Remove all spaces and check if anything remains
HAS_MD_FILES=true
EOF_MD="$(openssl rand -hex 8)"
echo "GIT_DIFF_CONTENT<<${EOF_MD}" >> "$GITHUB_OUTPUT"
echo "${FILTERED_MD_FILES}" >> "$GITHUB_OUTPUT"
echo "${EOF_MD}" >> "$GITHUB_OUTPUT"
else
echo "GIT_DIFF_CONTENT=" >> "$GITHUB_OUTPUT"
fi
if [ -n "${FILTERED_IMAGE_FILES// /}" ]; then # Remove all spaces and check if anything remains
HAS_IMAGE_FILES=true
EOF_IMG="$(openssl rand -hex 8)"
echo "GIT_DIFF_FILES<<${EOF_IMG}" >> "$GITHUB_OUTPUT"
echo "${FILTERED_IMAGE_FILES}" >> "$GITHUB_OUTPUT"
echo "${EOF_IMG}" >> "$GITHUB_OUTPUT"
else
echo "GIT_DIFF_FILES=" >> "$GITHUB_OUTPUT"
fi
# Set the boolean outputs
echo "HAS_MD_FILES=${HAS_MD_FILES}" >> "$GITHUB_OUTPUT"
echo "HAS_IMAGE_FILES=${HAS_IMAGE_FILES}" >> "$GITHUB_OUTPUT"
if [ "${HAS_MD_FILES}" = "true" ] || [ "${HAS_IMAGE_FILES}" = "true" ]; then
echo "HAS_FILES=true" >> "$GITHUB_OUTPUT"
else
echo "HAS_FILES=false" >> "$GITHUB_OUTPUT"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Node.js environment
if: steps.check.outputs.HAS_FILES == 'true'
uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
with:
node-version-file: ".nvmrc"
cache: npm
- name: Install
if: steps.check.outputs.HAS_FILES == 'true'
run: npm ci
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Build changed content
id: build-content
if: steps.check.outputs.HAS_MD_FILES == 'true'
env:
GIT_DIFF_CONTENT: ${{ steps.check.outputs.GIT_DIFF_CONTENT }}
# Used by the `rari` cli to avoid rate limiting issues
# when fetching the latest releases info from the GitHub API.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CONTENT_ROOT: ${{ github.workspace }}/files
# This is so that if there's a single 'unsafe_html' flaw, it
# completely fails the build.
# But all other flaws should be 'warn', so that we can include
# information about the flaws when we analyze the built PR.
BUILD_FLAW_LEVELS: "unsafe_html: error, *:warn"
BUILD_LIVE_SAMPLES_BASE_URL: https://live.mdnyalp.dev
BUILD_LEGACY_LIVE_SAMPLES_BASE_URL: https://live-samples.mdn.allizom.net
# TODO: This should be implicit when `CI=true`
BUILD_NO_PROGRESSBAR: true
FRED_WRITER_MODE: true
FRED_PLAYGROUND_BASE_HOST: mdnyalp.dev
# rari
LIVE_SAMPLES_BASE_URL: https://live.mdnyalp.dev
INTERACTIVE_EXAMPLES_BASE_URL: https://interactive-examples.mdn.allizom.net
run: |
# The reason this script isn't in `package.json` is because
# you don't need that script as a writer. It's only used in CI
# and it can't use the default CONTENT_ROOT that gets set in
# package.json.
# Read into array, one filename per line
readarray -t files_to_build <<< "$GIT_DIFF_CONTENT"
ARGS=()
for file in "${files_to_build[@]}"; do
ARGS+=("-f" "$PWD/$file")
done
npx rari build --no-basic --json-issues --data-issues "${ARGS[@]}"
# Workaround, as fred-ssr doesn't copy assets.
cp -vR node_modules/@mdn/fred/out/. "$BUILD_OUT_ROOT"
npx fred-ssr
echo "Disk usage size of the build"
du -sh $BUILD_OUT_ROOT
# Save the PR number into the build
echo ${{ github.event.number }} > ${BUILD_OUT_ROOT}/NR
# Download the raw diff blob and store that inside the build
# directory.
# The purpose of this is for the PR Review Companion to later
# be able to use this raw diff file for the benefit of analyzing.
wget https://github.com/${{ github.repository }}/compare/${BASE_SHA}...${HEAD_SHA}.diff -O ${BUILD_OUT_ROOT}/DIFF
- name: Show final disk usage size of build
if: steps.check.outputs.HAS_MD_FILES == 'true'
run: du -sh $BUILD_OUT_ROOT
- uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
if: steps.check.outputs.HAS_MD_FILES == 'true'
with:
name: build
path: ${{ env.BUILD_OUT_ROOT }}
- name: Check changed files
if: steps.check.outputs.HAS_IMAGE_FILES == 'true'
env:
GIT_DIFF_FILES: ${{ steps.check.outputs.GIT_DIFF_FILES }}
run: |
readarray -t files_to_check <<< "$GIT_DIFF_FILES"
export CONTENT_ROOT=$(pwd)/files
npm run filecheck "${files_to_check[@]}"