Skip to content

Commit e4eb45b

Browse files
authored
fix(workflows): pass changed files via bash array (mdn#40648)
* Use a bash array instead of a string to convey the file list to linters * refactor xargs usage * removed debug option from step * removed commented code * fix file existence text
1 parent d84aecf commit e4eb45b

File tree

2 files changed

+88
-34
lines changed

2 files changed

+88
-34
lines changed

.github/workflows/pr-check-lint_content.yml

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,34 @@ jobs:
3434
run: |
3535
# Use the GitHub API to get the list of changed files
3636
# documentation: https://docs.github.com/rest/commits/commits#compare-two-commits
37-
DIFF_DOCUMENTS=$(gh api repos/{owner}/{repo}/compare/${BASE_SHA}...${HEAD_SHA} \
38-
--jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename')
39-
# filter out files that are not markdown
40-
DIFF_DOCUMENTS=$(echo "${DIFF_DOCUMENTS}" | egrep -i "^files/.*\.md$" | xargs)
41-
echo "DIFF_DOCUMENTS=${DIFF_DOCUMENTS}" >> "$GITHUB_OUTPUT"
37+
38+
# Get files as newline-separated list
39+
FILTERED_FILES=$(gh api repos/{owner}/{repo}/compare/${BASE_SHA}...${HEAD_SHA} \
40+
--jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename' | \
41+
egrep -i "^files/.*\.md$")
42+
43+
# Store as multiline output
44+
EOF="$(openssl rand -hex 8)"
45+
echo "DIFF_DOCUMENTS<<${EOF}" >> "$GITHUB_OUTPUT"
46+
echo "${FILTERED_FILES}" >> "$GITHUB_OUTPUT"
47+
echo "${EOF}" >> "$GITHUB_OUTPUT"
48+
49+
# Also set a simple flag for whether we have files
50+
if [ -n "${FILTERED_MD_FILES// /}" ]; then # Remove all spaces and check if anything remains
51+
echo "HAS_FILES=true" >> "$GITHUB_OUTPUT"
52+
else
53+
echo "HAS_FILES=false" >> "$GITHUB_OUTPUT"
54+
fi
4255
4356
- name: Checkout HEAD
44-
if: steps.check.outputs.DIFF_DOCUMENTS
57+
if: steps.check.outputs.HAS_FILES == 'true'
4558
uses: actions/checkout@v4
4659
with:
4760
ref: ${{ github.event.pull_request.head.sha }}
4861
path: pr_head
4962

5063
- name: Get changed content from HEAD
51-
if: steps.check.outputs.DIFF_DOCUMENTS
64+
if: steps.check.outputs.HAS_FILES == 'true'
5265
run: |
5366
git config --global user.email "108879845+mdn-bot@users.noreply.github.com"
5467
git config --global user.name "mdn-bot"
@@ -64,63 +77,69 @@ jobs:
6477
git commit -m "Code from PR head"
6578
6679
- name: Setup Node.js environment
67-
if: steps.check.outputs.DIFF_DOCUMENTS
80+
if: steps.check.outputs.HAS_FILES == 'true'
6881
uses: actions/setup-node@v4
6982
with:
7083
node-version-file: ".nvmrc"
7184
cache: yarn
7285

7386
- name: Install all yarn packages
74-
if: steps.check.outputs.DIFF_DOCUMENTS
87+
if: steps.check.outputs.HAS_FILES == 'true'
7588
run: yarn --frozen-lockfile
7689
env:
7790
# https://github.com/microsoft/vscode-ripgrep#github-api-limit-note
7891
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7992

8093
- name: Lint and format markdown files
8194
id: lint
82-
if: steps.check.outputs.DIFF_DOCUMENTS
95+
if: steps.check.outputs.HAS_FILES == 'true'
8396
env:
8497
DIFF_DOCUMENTS: ${{ steps.check.outputs.DIFF_DOCUMENTS }}
8598
run: |
8699
# Generate random delimiter
87100
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
88101
EOF="$(openssl rand -hex 8)"
89102
90-
files_to_lint="$DIFF_DOCUMENTS"
103+
# The DIFF_DOCUMENTS env var contains the clean newline-separated list
104+
# Read into array, one filename per line
105+
readarray -t files_to_lint <<< "$DIFF_DOCUMENTS"
106+
107+
# Debug: show what we got
108+
printf "Files to process (%d files):\n" "${#files_to_lint[@]}"
109+
printf "'%s'\n" "${files_to_lint[@]}"
91110
92111
echo "crlf line ending check"
93112
CRLF_FAILED=true
94-
CRLF_LOG=$(git ls-files --eol ${files_to_lint} | grep -E 'w/(mixed|crlf)') || CRLF_FAILED=false
113+
CRLF_LOG=$(git ls-files --eol "${files_to_lint[@]}" | grep -E 'w/(mixed|crlf)') || CRLF_FAILED=false
95114
echo "CRLF_LOG<<${EOF}" >> "$GITHUB_OUTPUT"
96115
echo "${CRLF_LOG}" >> "$GITHUB_OUTPUT"
97116
echo "${EOF}" >> "$GITHUB_OUTPUT"
98117
echo "CRLF_FAILED=${CRLF_FAILED}" >> "$GITHUB_OUTPUT"
99118
100119
echo "Running markdownlint --fix"
101120
MD_LINT_FAILED=false
102-
MD_LINT_LOG=$(yarn markdownlint-cli2 --fix ${files_to_lint} 2>&1) || MD_LINT_FAILED=true
121+
MD_LINT_LOG=$(yarn markdownlint-cli2 --fix "${files_to_lint[@]}" 2>&1) || MD_LINT_FAILED=true
103122
echo "MD_LINT_LOG<<${EOF}" >> "$GITHUB_OUTPUT"
104123
echo "${MD_LINT_LOG}" >> "$GITHUB_OUTPUT"
105124
echo "${EOF}" >> "$GITHUB_OUTPUT"
106125
echo "MD_LINT_FAILED=${MD_LINT_FAILED}" >> "$GITHUB_OUTPUT"
107126
108127
echo "Linting front-matter"
109128
FM_LINT_FAILED=false
110-
FM_LINT_LOG=$(node scripts/front-matter_linter.js --fix true ${files_to_lint} 2>&1) || FM_LINT_FAILED=true
129+
FM_LINT_LOG=$(node scripts/front-matter_linter.js --fix true "${files_to_lint[@]}" 2>&1) || FM_LINT_FAILED=true
111130
echo "FM_LINT_LOG<<${EOF}" >> "$GITHUB_OUTPUT"
112131
echo "${FM_LINT_LOG}" >> "$GITHUB_OUTPUT"
113132
echo "${EOF}" >> "$GITHUB_OUTPUT"
114133
echo "FM_LINT_FAILED=${FM_LINT_FAILED}" >> "$GITHUB_OUTPUT"
115134
116135
echo "Running Prettier"
117136
PRETTIER_FAILED=false
118-
PRETTIER_LOG=$(yarn prettier --check ${files_to_lint} 2>&1) || PRETTIER_FAILED=true
137+
PRETTIER_LOG=$(yarn prettier --check "${files_to_lint[@]}" 2>&1) || PRETTIER_FAILED=true
119138
echo "PRETTIER_LOG<<${EOF}" >> "$GITHUB_OUTPUT"
120139
echo "${PRETTIER_LOG}" >> "$GITHUB_OUTPUT"
121140
echo "${EOF}" >> "$GITHUB_OUTPUT"
122141
echo "PRETTIER_FAILED=${PRETTIER_FAILED}" >> "$GITHUB_OUTPUT"
123-
yarn prettier -w ${files_to_lint}
142+
yarn prettier -w "${files_to_lint[@]}"
124143
125144
if [[ -n $(git diff) ]]; then
126145
echo "FILES_MODIFIED=true" >> "$GITHUB_OUTPUT"

.github/workflows/pr-test.yml

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,64 @@ jobs:
3535
id: check
3636
run: |
3737
# Use the GitHub API to get the list of changed files
38-
# documentation: https://docs.github.com/rest/commits/commits#compare-two-commits
3938
DIFF_DOCUMENTS=$(gh api repos/{owner}/{repo}/compare/${BASE_SHA}...${HEAD_SHA} \
4039
--jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename')
4140
42-
# filter out files that are not markdown files
43-
GIT_DIFF_CONTENT=$(echo "${DIFF_DOCUMENTS}" | egrep -i "^files/.*\.(md)$" | xargs)
44-
echo "GIT_DIFF_CONTENT=${GIT_DIFF_CONTENT}" >> "$GITHUB_OUTPUT"
45-
46-
# filter out files that are not attachments
47-
GIT_DIFF_FILES=$(echo "${DIFF_DOCUMENTS}" | egrep -i "^files/.*\.(png|jpeg|jpg|gif|svg|webp)$" | xargs)
48-
echo "GIT_DIFF_FILES=${GIT_DIFF_FILES}" >> "$GITHUB_OUTPUT"
41+
FILTERED_MD_FILES=$(echo "$DIFF_DOCUMENTS" | egrep -i "^files/.*\.md$" || true)
42+
FILTERED_IMAGE_FILES=$(echo "$DIFF_DOCUMENTS" | egrep -i "^files/.*\.(png|jpeg|jpg|gif|svg|webp)$" || true)
43+
44+
HAS_MD_FILES=false
45+
HAS_IMAGE_FILES=false
46+
47+
# Check if we actually have content (not just empty strings)
48+
if [ -n "${FILTERED_MD_FILES// /}" ]; then # Remove all spaces and check if anything remains
49+
HAS_MD_FILES=true
50+
EOF_MD="$(openssl rand -hex 8)"
51+
echo "GIT_DIFF_CONTENT<<${EOF_MD}" >> "$GITHUB_OUTPUT"
52+
echo "${FILTERED_MD_FILES}" >> "$GITHUB_OUTPUT"
53+
echo "${EOF_MD}" >> "$GITHUB_OUTPUT"
54+
else
55+
echo "GIT_DIFF_CONTENT=" >> "$GITHUB_OUTPUT"
56+
fi
57+
58+
if [ -n "${FILTERED_IMAGE_FILES// /}" ]; then # Remove all spaces and check if anything remains
59+
HAS_IMAGE_FILES=true
60+
EOF_IMG="$(openssl rand -hex 8)"
61+
echo "GIT_DIFF_FILES<<${EOF_IMG}" >> "$GITHUB_OUTPUT"
62+
echo "${FILTERED_IMAGE_FILES}" >> "$GITHUB_OUTPUT"
63+
echo "${EOF_IMG}" >> "$GITHUB_OUTPUT"
64+
else
65+
echo "GIT_DIFF_FILES=" >> "$GITHUB_OUTPUT"
66+
fi
67+
68+
# Set the boolean outputs
69+
echo "HAS_MD_FILES=${HAS_MD_FILES}" >> "$GITHUB_OUTPUT"
70+
echo "HAS_IMAGE_FILES=${HAS_IMAGE_FILES}" >> "$GITHUB_OUTPUT"
71+
if [ "${HAS_MD_FILES}" = "true" ] || [ "${HAS_IMAGE_FILES}" = "true" ]; then
72+
echo "HAS_FILES=true" >> "$GITHUB_OUTPUT"
73+
else
74+
echo "HAS_FILES=false" >> "$GITHUB_OUTPUT"
75+
fi
4976
env:
5077
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5178

5279
- name: Setup Node.js environment
53-
if: steps.check.outputs.GIT_DIFF_CONTENT || steps.check.outputs.GIT_DIFF_FILES
80+
if: steps.check.outputs.HAS_FILES == 'true'
5481
uses: actions/setup-node@v4
5582
with:
5683
node-version-file: ".nvmrc"
5784
cache: yarn
5885

5986
- name: Install all yarn packages
60-
if: steps.check.outputs.GIT_DIFF_CONTENT || steps.check.outputs.GIT_DIFF_FILES
87+
if: steps.check.outputs.HAS_FILES == 'true'
6188
run: yarn --frozen-lockfile
6289
env:
6390
# https://github.com/microsoft/vscode-ripgrep#github-api-limit-note
6491
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6592

6693
- name: Build changed content
6794
id: build-content
68-
if: steps.check.outputs.GIT_DIFF_CONTENT
95+
if: steps.check.outputs.HAS_MD_FILES == 'true'
6996
env:
7097
GIT_DIFF_CONTENT: ${{ steps.check.outputs.GIT_DIFF_CONTENT }}
7198

@@ -109,8 +136,16 @@ jobs:
109136
# you don't need that script as a writer. It's only used in CI
110137
# and it can't use the default CONTENT_ROOT that gets set in
111138
# package.json.
112-
ARGS=$(echo $GIT_DIFF_CONTENT | sed -E -e "s#(^| )files#\1-f $PWD/files#g")
113-
yarn rari build --no-basic --json-issues --data-issues $ARGS
139+
140+
# Read into array, one filename per line
141+
readarray -t files_to_build <<< "$GIT_DIFF_CONTENT"
142+
143+
ARGS=()
144+
for file in "${files_to_build[@]}"; do
145+
ARGS+=("-f" "$PWD/$file")
146+
done
147+
148+
yarn rari build --no-basic --json-issues --data-issues "${ARGS[@]}"
114149
yarn yari-render-html
115150
116151
echo "Disk usage size of the build"
@@ -126,25 +161,25 @@ jobs:
126161
wget https://github.com/${{ github.repository }}/compare/${BASE_SHA}...${HEAD_SHA}.diff -O ${BUILD_OUT_ROOT}/DIFF
127162
128163
- name: Merge static assets with built documents
129-
if: steps.check.outputs.GIT_DIFF_CONTENT
164+
if: steps.check.outputs.HAS_MD_FILES == 'true'
130165
run: |
131166
# Exclude the .map files, as they're used for debugging JS and CSS.
132167
rsync -a --exclude "*.map" node_modules/@mdn/yari/client/build/ $BUILD_OUT_ROOT
133168
# Show the final disk usage size of the build.
134169
du -sh $BUILD_OUT_ROOT
135170
136171
- uses: actions/upload-artifact@v4
137-
if: steps.check.outputs.GIT_DIFF_CONTENT
172+
if: steps.check.outputs.HAS_MD_FILES == 'true'
138173
with:
139174
name: build
140175
path: ${{ env.BUILD_OUT_ROOT }}
141176

142177
- name: Check changed files
143-
if: steps.check.outputs.GIT_DIFF_FILES
178+
if: steps.check.outputs.HAS_IMAGE_FILES == 'true'
144179
env:
145180
GIT_DIFF_FILES: ${{ steps.check.outputs.GIT_DIFF_FILES }}
146181
run: |
147-
echo $GIT_DIFF_FILES
182+
readarray -t files_to_check <<< "$GIT_DIFF_FILES"
148183
149184
export CONTENT_ROOT=$(pwd)/files
150-
yarn filecheck $GIT_DIFF_FILES
185+
yarn filecheck "${files_to_check[@]}"

0 commit comments

Comments
 (0)