Skip to content

Check Links in Pull Requests

This recipe shows how to check for broken links in pull requests. It only checks newly added links by comparing against the base branch, reducing noise from existing broken links.

The workflow is triggered on pull request events against the main branch. It clones the repository and checks out the main branch to dump all links. It then stashes untracked files and checks out the feature branch. The stashed changes are applied and the links from the main branch are appended to the .lycheeignore file. Finally, the links in the feature branch are checked and suggestions are provided if the check fails.

.github/workflows/check-pr-links.yml
name: PR Links
on:
pull_request:
branches: [master]
workflow_dispatch:
jobs:
linkChecker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- name: Build site
uses: withastro/action@v5
with:
package-manager: pnpm@latest
- name: Check links
uses: lycheeverse/lychee-action@v2
with:
# Remap live URLs to build directory because the links are potentially not live (not yet on master)
args: |
--root-dir $PWD/dist
--exclude-all-private
--remap 'https://lychee\.cli\.rs/(.*)/ file://'$PWD'/dist/$1/index.html'
dist/
src/
fail: true
- name: Suggestions
if: failure()
run: |
echo -e "\nPlease review the links reported in the 'Check links in PR changes' step above."
echo -e "If a link is valid but fails due to a CAPTCHA challenge, IP blocking, login requirements, etc.,"
echo -e "consider adding such links to .lycheeignore file to bypass future checks.\n"
exit 1