Check Broken Links #11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Check Broken Links | |
on: | |
workflow_dispatch: | |
push: | |
branches: [ main ] # Runs on push to main branch | |
schedule: | |
- cron: "0 12 * * 1" # Runs every Monday at 12:00 UTC | |
jobs: | |
check-links: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
sparse-checkout: .linkinatorrc.json | |
sparse-checkout-cone-mode: false | |
- name: Setup Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: "18" | |
- name: Wait for GitHub Pages deployment | |
if: github.event_name == 'push' | |
run: sleep 60s # Delay to allow deployment to complete | |
- name: Get short SHA | |
run: echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_ENV | |
- name: Check links on the live site to the action output | |
run: | | |
echo "Commit SHA: $SHORT_SHA" | |
npx linkinator https://hyperledger-identus.github.io/docs/ --config .linkinatorrc.json --format text || EXIT_CODE=$? | |
echo "LINKINATOR_EXIT_CODE=${EXIT_CODE:-0}" >> $GITHUB_ENV | |
- name: Check links on the live site and safe to a file | |
if: env.LINKINATOR_EXIT_CODE != 0 | |
run: | | |
npx linkinator https://hyperledger-identus.github.io/docs/ --config .linkinatorrc.json > link-check-results.json || true | |
- name: Generate Markdown Table from Linkinator Results | |
if: env.LINKINATOR_EXIT_CODE != 0 | |
run: | | |
echo "| URL | Status | Parent Page |" >> link-report.md | |
echo "|-----|--------|-------------|" >> link-report.md | |
jq -r '.links[] | select(.state == "BROKEN") | "| \(.url) | \(.status) | \(.parent) |"' link-check-results.json >> link-report.md | |
BROKEN_COUNT=$(jq '[.links[] | select(.state == "BROKEN")] | length' link-check-results.json) | |
echo "### 🔗 Broken Links Report( $BROKEN_COUNT found)" >> $GITHUB_STEP_SUMMARY | |
cat link-report.md >> $GITHUB_STEP_SUMMARY | |
- name: Upload Linkinator output | |
if: env.LINKINATOR_EXIT_CODE != 0 | |
uses: actions/upload-artifact@v4 | |
with: | |
name: linkinator-results-${{ env.SHORT_SHA }} | |
path: link-check-results.json | |
- name: Fail the build if there are broken links | |
if: env.LINKINATOR_EXIT_CODE != 0 | |
run: | | |
echo "Broken links detected. Check the artifact for details." | |
exit 1 |