Repository navigation
188 lines (155 loc) · 7.41 KB
/
Copy pathpr-test.yml
File metadata and controls
188 lines (155 loc) · 7.41 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
187
188
# This is more or less a copy of
# https://github.com/mdn/content/blob/main/.github/workflows/pr-test.yml
# but done in a way that it first checks out mdn/translated-content (or
# fork of) and _then_ checks out mdn/content which has the relevant
# CI related tooling.
name: PR Test
on:
pull_request:
jobs:
tests:
if: github.repository == 'mdn/translated-content'
runs-on: ubuntu-latest
# Set the permissions to `read-all`, preventing the workflow from
# any accidental write access to the repository.
permissions: read-all
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
# If we don't do this the built files may end up in
# `node_modules/@mdn/yari/client/build/` and we don't want that
# to get pushed into the cache.
BUILD_OUT_ROOT: /tmp/build
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.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: Checkout (content)
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
if: steps.check.outputs.HAS_FILES == 'true'
with:
repository: mdn/content
path: mdn/content
persist-credentials: false
- name: Setup Node.js environment
if: steps.check.outputs.HAS_FILES == 'true'
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version-file: mdn/content/.nvmrc
cache: npm
cache-dependency-path: mdn/content/package-lock.json
- name: Install
if: steps.check.outputs.HAS_FILES == 'true'
working-directory: ${{ github.workspace }}/mdn/content
run: npm ci
env:
# https://github.com/microsoft/vscode-ripgrep#github-api-limit-note
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 }}/mdn/content/files
CONTENT_TRANSLATED_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
working-directory: ${{ github.workspace }}/mdn/content
run: |
mkdir -p $BUILD_OUT_ROOT
# 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"
npx @mdn/rari build --no-basic --json-issues --data-issues "${files_to_build[@]/#files/$CONTENT_TRANSLATED_ROOT}"
# Workaround, as `fred ssr` doesn't copy assets.
cp -vR node_modules/@mdn/fred/out/. "$BUILD_OUT_ROOT"
npx @mdn/fred ssr
echo "Disk usage size of the build"
du -sh $BUILD_OUT_ROOT
# 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@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
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:
CONTENT_ROOT: ${{ github.workspace }}/mdn/content/files
CONTENT_TRANSLATED_ROOT: ${{ github.workspace }}/files
GIT_DIFF_FILES: ${{ steps.check.outputs.GIT_DIFF_FILES }}
working-directory: ${{ github.workspace }}/mdn/content
run: |
readarray -t files_to_check <<< "$GIT_DIFF_FILES"
echo "${files_to_check[@]}"
ARGS=()
for file in "${files_to_check[@]}"; do
file_without_prefix="${file#files/}"
ARGS+=("$CONTENT_TRANSLATED_ROOT/$file_without_prefix")
done
npm run filecheck "${ARGS[@]}"