Skip to content

Commit 56837b3

Browse files
authored
Fix GH Action for link checking (#3448)
* Enhance GitHub Actions workflow by initializing FAILED variable for better error handling in link checks * Refine GitHub Actions workflow to ensure link checks report errors correctly by adding failure handling for absolute and relative link checks * try again * and another * Add comments to PR * bump versions * Refine GitHub Actions workflow to improve link check error handling by preventing immediate exit on failure and adding a reporting step after posting comments. * Update GitHub Actions workflow to standardise quotes and enhance summary comment generation for link checks * Fix date display on PR output
1 parent 5c53f21 commit 56837b3

File tree

1 file changed

+89
-17
lines changed

1 file changed

+89
-17
lines changed

.github/workflows/check-links.yml

Lines changed: 89 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ on:
44
workflow_dispatch:
55
push:
66
branches: [main]
7-
paths-ignore: [src/**, tests/**, "**.py"]
7+
paths-ignore: [src/**, tests/**, '**.py']
88
pull_request:
99
types: [opened, synchronize, reopened]
10-
paths-ignore: [src/**, tests/**, "**.py"]
10+
paths-ignore: [src/**, tests/**, '**.py']
1111
concurrency:
1212
# New commit on branch cancels running workflows of the same branch
1313
group: ${{ github.workflow }}-${{ github.ref }}
@@ -24,17 +24,24 @@ jobs:
2424
- name: Set up Python
2525
uses: actions/setup-python@v4
2626
with:
27-
python-version: "3.11"
27+
python-version: '3.11'
2828
- name: Install dependencies
2929
run: pip install requests
3030
- name: Check Absolute Links
3131
id: check-absolute
3232
run: |
3333
# Only check absolute links (http/https), not relative links
3434
python docs/link_checker.py --dir docs/book --substring "http" --validate-links --timeout 15 --ci-mode
35-
echo "exit_code=$?" >> $GITHUB_OUTPUT
36-
# Continue on error so both checks can run to completion
37-
continue-on-error: true
35+
# Capture the exit code even if the command fails
36+
EXIT_CODE=$?
37+
echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT
38+
# Don't exit with error yet, as we want to keep the exit code for the summary step
39+
exit 0
40+
- name: Mark job status based on check result
41+
if: steps.check-absolute.outputs.exit_code != '0'
42+
run: |
43+
echo "::error::Absolute links check failed! Found broken links."
44+
exit 1
3845
check-relative-links:
3946
if: github.event.pull_request.draft == false
4047
runs-on: ubuntu-latest
@@ -46,44 +53,109 @@ jobs:
4653
- name: Set up Python
4754
uses: actions/setup-python@v4
4855
with:
49-
python-version: "3.11"
56+
python-version: '3.11'
5057
- name: Check Relative Links
5158
id: check-relative
5259
run: |
5360
# Check if relative links resolve within the repository
5461
python scripts/check_relative_links.py --dir docs/book
55-
echo "exit_code=$?" >> $GITHUB_OUTPUT
56-
# Continue on error so both checks can run to completion
57-
continue-on-error: true
58-
summary:
62+
# Capture the exit code even if the command fails
63+
EXIT_CODE=$?
64+
echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT
65+
# Don't exit with error yet, as we want to keep the exit code for the summary step
66+
exit 0
67+
- name: Mark job status based on check result
68+
if: steps.check-relative.outputs.exit_code != '0'
69+
run: |
70+
echo "::error::Relative links check failed! Found broken links."
71+
exit 1
72+
post-summary:
5973
needs: [check-absolute-links, check-relative-links]
6074
if: always() && github.event.pull_request.draft == false
6175
runs-on: ubuntu-latest
6276
steps:
6377
- name: Create Summary
64-
run: |-
78+
id: create-summary
79+
run: |
6580
echo "# Documentation Link Check Results" >> $GITHUB_STEP_SUMMARY
6681
82+
# Initialize FAILED variable
83+
FAILED=false
84+
85+
# Create summary content for PR comment
86+
cat << 'EOL' > comment_beginning.txt
87+
## Documentation Link Check Results
88+
EOL
89+
6790
# Check for failures in absolute links job
6891
if [[ "${{ needs.check-absolute-links.outputs.exit_code }}" != "0" ]]; then
6992
echo "❌ **Absolute links check failed**" >> $GITHUB_STEP_SUMMARY
7093
echo "There are broken absolute links in the documentation. Please check the job logs for details." >> $GITHUB_STEP_SUMMARY
94+
cat << 'EOL' > comment_absolute.txt
95+
❌ **Absolute links check failed**
96+
There are broken absolute links in the documentation. [See workflow logs for details](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
97+
EOL
7198
FAILED=true
7299
else
73100
echo "✅ **Absolute links check passed**" >> $GITHUB_STEP_SUMMARY
101+
cat << 'EOL' > comment_absolute.txt
102+
✅ **Absolute links check passed**
103+
EOL
74104
fi
75105
76106
# Check for failures in relative links job
77107
if [[ "${{ needs.check-relative-links.outputs.exit_code }}" != "0" ]]; then
78108
echo "❌ **Relative links check failed**" >> $GITHUB_STEP_SUMMARY
79109
echo "There are broken relative links in the documentation. Please check the job logs for details." >> $GITHUB_STEP_SUMMARY
110+
cat << 'EOL' > comment_relative.txt
111+
❌ **Relative links check failed**
112+
There are broken relative links in the documentation. [See workflow logs for details](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
113+
EOL
80114
FAILED=true
81115
else
82116
echo "✅ **Relative links check passed**" >> $GITHUB_STEP_SUMMARY
117+
cat << 'EOL' > comment_relative.txt
118+
✅ **Relative links check passed**
119+
EOL
83120
fi
84121
85-
# Exit with failure if any checks failed
86-
if [[ "$FAILED" == "true" ]]; then
87-
echo "::error::One or more link checks failed. Please fix the broken links."
88-
exit 1
89-
fi
122+
# Add timestamp
123+
cat << EOL > comment_footer.txt
124+
<sub>Last checked: $(date -u '+%Y-%m-%d %H:%M:%S UTC')</sub>
125+
EOL
126+
127+
# Combine all parts
128+
cat comment_beginning.txt comment_absolute.txt comment_relative.txt comment_footer.txt > full_comment.txt
129+
130+
# Save comment body to output
131+
echo "comment_body<<EOF" >> $GITHUB_OUTPUT
132+
cat full_comment.txt >> $GITHUB_OUTPUT
133+
echo "EOF" >> $GITHUB_OUTPUT
134+
135+
# Save failed status to output
136+
echo "failed=$FAILED" >> $GITHUB_OUTPUT
137+
138+
# Do not exit with error - we want to post the comment first
139+
- name: Find Comment
140+
uses: peter-evans/find-comment@v3
141+
if: github.event_name == 'pull_request'
142+
id: find-comment
143+
with:
144+
issue-number: ${{ github.event.pull_request.number }}
145+
comment-author: github-actions[bot]
146+
body-includes: Documentation Link Check Results
147+
- name: Create or Update Comment
148+
uses: peter-evans/create-or-update-comment@v4
149+
if: github.event_name == 'pull_request'
150+
with:
151+
comment-id: ${{ steps.find-comment.outputs.comment-id }}
152+
issue-number: ${{ github.event.pull_request.number }}
153+
body: ${{ steps.create-summary.outputs.comment_body }}
154+
edit-mode: replace
155+
156+
# This step runs after comment is posted and fails the workflow if links are broken
157+
- name: Report Link Check Status
158+
if: steps.create-summary.outputs.failed == 'true'
159+
run: |-
160+
echo "::error::One or more link checks failed. Please fix the broken links."
161+
exit 1

0 commit comments

Comments
 (0)