|
| 1 | +#Checks links in a PR to ensure they are valid. If link is valid but failing, it can be added to the .lycheeignore file |
| 2 | +#Code source: https://github.com/lycheeverse/lychee-action/issues/238 |
| 3 | + |
| 4 | +name: link check on PR |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + branches: [main] |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-links: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Clone repository |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 |
| 18 | + ref: ${{github.event.pull_request.head.ref}} |
| 19 | + repository: ${{github.event.pull_request.head.repo.full_name}} |
| 20 | + |
| 21 | + - name: Check out main branch |
| 22 | + run: git checkout main |
| 23 | + |
| 24 | + - name: Dump all links from main |
| 25 | + id: dump_links_from_main |
| 26 | + uses: lycheeverse/lychee-action@v1 |
| 27 | + with: |
| 28 | + args: | |
| 29 | + --dump |
| 30 | + --include-fragments |
| 31 | + . |
| 32 | + output: ./links-main.txt |
| 33 | + |
| 34 | + - name: Stash untracked files |
| 35 | + run: git stash push --include-untracked |
| 36 | + |
| 37 | + - name: Check out feature branch |
| 38 | + run: git checkout ${{ github.head_ref }} |
| 39 | + |
| 40 | + - name: Apply stashed changes |
| 41 | + # Apply stashed changes, ignore errors if stash is empty |
| 42 | + run: git stash pop || true |
| 43 | + |
| 44 | + - name: Append links-main.txt to .lycheeignore |
| 45 | + run: cat links-main.txt >> .lycheeignore |
| 46 | + |
| 47 | + - name: Check links |
| 48 | + uses: lycheeverse/lychee-action@v1 |
| 49 | + with: |
| 50 | + args: | |
| 51 | + --no-progress |
| 52 | + --include-fragments |
| 53 | + . |
| 54 | + # Fail action on broken links |
| 55 | + fail: true |
| 56 | + |
| 57 | + - name: Suggestions |
| 58 | + if: failure() |
| 59 | + run: | |
| 60 | + echo -e "\nPlease review the links reported in the Check links step above." |
| 61 | + echo -e "If a link is valid but fails due to a CAPTCHA challenge, IP blocking, login requirements, etc., |
| 62 | + consider adding such links to .lycheeignore file to bypass future checks.\n" |
| 63 | + exit 1 |
0 commit comments