Skip to content

ci: Add script &CI to check dead links #42

ci: Add script &CI to check dead links

ci: Add script &CI to check dead links #42

name: Check Broken Links
on:
pull_request:
types: [opened, synchronize, reopened]
# Optional: Add scheduled checks
# schedule:
# - cron: "0 0 * * 0" # Runs once every Sunday
jobs:
check-links:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: recursive
# Build the Hugo site first to handle rendering
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: 'latest'
extended: true
- name: Build Hugo Site
run: |
hugo --minify
echo "Hugo site built successfully to ./public directory"
# Set up caching to reduce API requests
- name: Restore lychee cache
uses: actions/cache@v4
with:
path: .lycheecache
key: cache-lychee-${{ github.sha }}
restore-keys: cache-lychee-
- name: Setup link exclusion patterns (optional)
id: setup-exclude
run: |
if [ -f .lycheeignore ]; then
echo "Exclusion patterns found in .lycheeignore"
else
echo "# Add URL regex patterns to exclude, one per line" > .lycheeignore
echo "# Example: ^https://example.com" >> .lycheeignore
fi
# Run two separate link checks: one for external URLs and one for local files
- name: Check External Links
id: lychee-external
uses: lycheeverse/lychee-action@v2
env:
GITHUB_TOKEN: ${{ github.token }}
with:
args: >-
--cache
--max-cache-age 48h
--verbose
--no-progress
--exclude-path ".git"
--exclude-path "node_modules"
--exclude-path "public"
--scheme "https"
--scheme "http"
--max-retries 5
--timeout 30
--max-concurrency 8
--retry-wait-time 3
'./**/*.md'
'./**/*.html'
'./**/*.txt'
fail: false
format: markdown
output: ./lychee-external-report.md
# Check links in the generated Hugo site (public directory)
- name: Check Links in Generated Site
id: lychee-hugo
uses: lycheeverse/lychee-action@v2
env:
GITHUB_TOKEN: ${{ github.token }}
with:
args: >-
--cache
--max-cache-age 48h
--verbose
--no-progress
--exclude-path ".git"
--exclude-path "node_modules"
--base "file://$(pwd)/public"
--max-retries 3
--timeout 30
--max-concurrency 8
--retry-wait-time 3
'./public/**/*.html'
fail: true
format: markdown
output: ./lychee-hugo-report.md
# Combine reports
- name: Combine Reports
if: steps.lychee-external.outputs.exit_code != 0 || steps.lychee-hugo.outputs.exit_code != 0
run: |
echo "# Link Checker Report" > ./lychee-combined-report.md
echo "" >> ./lychee-combined-report.md
echo "## External Link Check Results" >> ./lychee-combined-report.md
cat ./lychee-external-report.md >> ./lychee-combined-report.md
echo "" >> ./lychee-combined-report.md
echo "## Generated Site Link Check Results" >> ./lychee-combined-report.md
cat ./lychee-hugo-report.md >> ./lychee-combined-report.md
# Fail the workflow if the Hugo site check fails
- name: Check for errors
if: steps.lychee-hugo.outputs.exit_code != 0
run: |
echo "Errors found in the generated Hugo site. See the report for details."
exit 1
# If you want to post check results as PR comments, uncomment the following step
# - name: Create Comment
# uses: peter-evans/create-or-update-comment@v3
# if: github.event_name == 'pull_request' && steps.lychee.outputs.exit_code != 0
# with:
# issue-number: ${{ github.event.pull_request.number }}
# body-file: ./lychee-report.md