|
| 1 | +# WHY: Automated link checking - behavior controlled by repo variable |
| 2 | +# OBS: Set IS_ACTIVE_REPO=true in GitHub Repository Settings > Secrets and variables > Actions > Variables |
| 3 | +# ALT: Default (no variable set) runs manual only - perfect for student repos |
| 4 | + |
| 5 | +name: Check Links |
| 6 | + |
| 7 | +on: |
| 8 | + workflow_dispatch: # WHY: Manual trigger - always available |
| 9 | + |
| 10 | + schedule: |
| 11 | + - cron: "0 6 1 * *" # WHY: Runs monthly (1st of month) if IS_ACTIVE_REPO=true |
| 12 | + |
| 13 | + pull_request: # WHY: Validates PR links if IS_ACTIVE_REPO=true |
| 14 | + |
| 15 | +# WHY: Prevent multiple simultaneous link checks on same ref |
| 16 | +concurrency: |
| 17 | + group: link-check-${{ github.ref }} |
| 18 | + cancel-in-progress: true |
| 19 | + |
| 20 | +jobs: |
| 21 | + lychee: |
| 22 | + # WHY: Skip scheduled/PR runs unless IS_ACTIVE_REPO=true or manual trigger |
| 23 | + # OBS: vars.IS_ACTIVE_REPO linted as error in VS Code but works on GitHub |
| 24 | + if: github.event_name == 'workflow_dispatch' || vars.IS_ACTIVE_REPO == 'true' |
| 25 | + |
| 26 | + runs-on: ubuntu-latest |
| 27 | + |
| 28 | + # WHY: Permissions needed for PR comments and issue creation |
| 29 | + permissions: |
| 30 | + contents: read |
| 31 | + issues: write |
| 32 | + pull-requests: write |
| 33 | + |
| 34 | + steps: |
| 35 | + - uses: actions/checkout@v6 # OBS: v6 current as of Dec 2025 |
| 36 | + |
| 37 | + # WHY: Check all documentation and config files for broken links |
| 38 | + - name: Check links with Lychee |
| 39 | + uses: lycheeverse/lychee-action@v2 # OBS: v2 current as of Dec 2025 |
| 40 | + with: |
| 41 | + args: > |
| 42 | + --verbose |
| 43 | + --no-progress |
| 44 | + --user-agent "${{ github.repository }}/lychee" |
| 45 | + './**/*.bib' |
| 46 | + './**/*.md' |
| 47 | + './**/*.html' |
| 48 | + './**/*.tex' |
| 49 | + './**/*.yml' |
| 50 | + './**/*.yaml' |
| 51 | + lycheeVersion: latest # OBS: Always use latest lychee release |
| 52 | + env: |
| 53 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 54 | + |
| 55 | + # WHY: Provide helpful feedback on PRs with broken links |
| 56 | + - name: Comment on PR if links broken |
| 57 | + if: failure() && github.event_name == 'pull_request' |
| 58 | + uses: actions/github-script@v8 # OBS: v8 current as of Dec 2025 |
| 59 | + with: |
| 60 | + script: | |
| 61 | + const comment = `## Link Check Results |
| 62 | +
|
| 63 | + Some links appear broken. Check the [workflow logs](${context.payload.repository.html_url}/actions/runs/${context.runId}) for details.`; |
| 64 | +
|
| 65 | + github.rest.issues.createComment({ |
| 66 | + issue_number: context.issue.number, |
| 67 | + owner: context.repo.owner, |
| 68 | + repo: context.repo.repo, |
| 69 | + body: comment |
| 70 | + }); |
| 71 | +
|
| 72 | + # WHY: Track broken links found during scheduled checks |
| 73 | + # OBS: Only creates issue if none already open with 'broken-links' label |
| 74 | + - name: Create issue for scheduled failures |
| 75 | + if: failure() && github.event_name == 'schedule' |
| 76 | + uses: actions/github-script@v8 # OBS: v8 current as of Dec 2025 |
| 77 | + with: |
| 78 | + script: | |
| 79 | + const title = `Link Check Failed - ${new Date().toISOString().split('T')[0]}`; |
| 80 | + const body = `Weekly link check found broken links. [Check logs](${context.payload.repository.html_url}/actions/runs/${context.runId})`; |
| 81 | +
|
| 82 | + // WHY: Avoid duplicate issues |
| 83 | + const existing = await github.rest.issues.listForRepo({ |
| 84 | + owner: context.repo.owner, |
| 85 | + repo: context.repo.repo, |
| 86 | + labels: 'broken-links', |
| 87 | + state: 'open' |
| 88 | + }); |
| 89 | +
|
| 90 | + if (existing.data.length === 0) { |
| 91 | + await github.rest.issues.create({ |
| 92 | + owner: context.repo.owner, |
| 93 | + repo: context.repo.repo, |
| 94 | + title: title, |
| 95 | + body: body, |
| 96 | + labels: ['maintenance', 'broken-links'] |
| 97 | + }); |
| 98 | + } |
0 commit comments