|
| 1 | +name: Link check |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + paths: |
| 8 | + - "**/*.md" |
| 9 | + - ".github/.linkspector.yml" |
| 10 | + - ".github/workflows/link-check.yaml" |
| 11 | + schedule: |
| 12 | + # Mondays at 14:00 UTC. |
| 13 | + - cron: "0 14 * * 1" |
| 14 | + workflow_dispatch: |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + |
| 19 | +concurrency: |
| 20 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 21 | + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} |
| 22 | + |
| 23 | +jobs: |
| 24 | + check-links: |
| 25 | + # Later versions of Ubuntu have disabled unprivileged user namespaces, |
| 26 | + # which the linkspector action requires. |
| 27 | + runs-on: ubuntu-22.04 |
| 28 | + permissions: |
| 29 | + contents: read |
| 30 | + pull-requests: write |
| 31 | + env: |
| 32 | + CHROME_BUILD_ID: "145.0.7632.77" |
| 33 | + steps: |
| 34 | + - name: Harden Runner |
| 35 | + uses: step-security/harden-runner@f808768d1510423e83855289c910610ca9b43176 # v2.17.0 |
| 36 | + with: |
| 37 | + egress-policy: audit |
| 38 | + |
| 39 | + - name: Checkout |
| 40 | + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 |
| 41 | + with: |
| 42 | + persist-credentials: false |
| 43 | + |
| 44 | + - name: Set up Node.js |
| 45 | + uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6 |
| 46 | + with: |
| 47 | + node-version: "22" |
| 48 | + |
| 49 | + - name: Cache Puppeteer Chrome |
| 50 | + id: chrome-cache |
| 51 | + uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5 |
| 52 | + with: |
| 53 | + path: ~/.cache/puppeteer |
| 54 | + key: puppeteer-${{ runner.os }}-${{ runner.arch }}-chrome-${{ env.CHROME_BUILD_ID }} |
| 55 | + |
| 56 | + - name: Install Linkspector Chrome |
| 57 | + id: install-chrome |
| 58 | + run: | |
| 59 | + set -euo pipefail |
| 60 | + # @puppeteer/browsers is idempotent: when the cache hit restored the |
| 61 | + # binary it just prints the existing path; on a cache miss it |
| 62 | + # downloads Chrome into ~/.cache/puppeteer for actions/cache to save |
| 63 | + # at the end of the job. |
| 64 | + chrome_path="$(npx -y @puppeteer/browsers install "chrome@${CHROME_BUILD_ID}" --path "${HOME}/.cache/puppeteer" --format '{{path}}')" |
| 65 | + echo "path=${chrome_path}" >> "$GITHUB_OUTPUT" |
| 66 | +
|
| 67 | + # action-linkspector has a pnpm install step that fails without an |
| 68 | + # existing pnpm store. See: |
| 69 | + # https://github.com/UmbrellaDocs/action-linkspector/issues/54 |
| 70 | + - name: Enable corepack and create pnpm store |
| 71 | + run: | |
| 72 | + corepack enable pnpm |
| 73 | + mkdir -p "$(pnpm store path --silent)" |
| 74 | +
|
| 75 | + # action-linkspector pipes linkspector through reviewdog, which only |
| 76 | + # surfaces failures and stays silent on success. List discovered URLs |
| 77 | + # and run linkspector standalone first so the workflow log shows every |
| 78 | + # URL that was checked, then let action-linkspector run for the PR |
| 79 | + # review comment integration. |
| 80 | + - name: List URLs found in Markdown files |
| 81 | + run: | |
| 82 | + set -euo pipefail |
| 83 | + echo "URLs discovered in Markdown files (subject to linkspector ignorePatterns):" |
| 84 | + echo |
| 85 | + grep -rhoE 'https?://[^[:space:])"`<>]+' \ |
| 86 | + --include='*.md' \ |
| 87 | + --exclude-dir=node_modules \ |
| 88 | + --exclude-dir=.git \ |
| 89 | + . | sort -u |
| 90 | +
|
| 91 | + - name: Run linkspector |
| 92 | + env: |
| 93 | + PUPPETEER_EXECUTABLE_PATH: ${{ steps.install-chrome.outputs.path }} |
| 94 | + run: npx -y @umbrelladocs/linkspector check --showstat --check-archived -c .github/.linkspector.yml |
| 95 | + |
| 96 | + - name: Check Markdown links |
| 97 | + if: always() |
| 98 | + uses: umbrelladocs/action-linkspector@036f295d12b67b0c4b445bc83db0538afb78db69 # v1.5.2 |
| 99 | + env: |
| 100 | + # Use the Chrome build prepared from the pinned Puppeteer instead |
| 101 | + # of letting linkspector download a mutable browser at runtime. |
| 102 | + # See: https://github.com/UmbrellaDocs/action-linkspector/issues/62 |
| 103 | + PUPPETEER_EXECUTABLE_PATH: ${{ steps.install-chrome.outputs.path }} |
| 104 | + with: |
| 105 | + reporter: ${{ github.event_name == 'pull_request' && 'github-pr-review' || 'github-check' }} |
| 106 | + config_file: .github/.linkspector.yml |
| 107 | + fail_on_error: "true" |
| 108 | + filter_mode: "file" |
| 109 | + show_stats: "true" |
0 commit comments