|
| 1 | +name: Check Links in Pull Requests |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - '**/*.md' |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-links: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + # 1️⃣ Checkout repository |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + # 2️⃣ Get changed Markdown files in the PR |
| 22 | + - name: Get changed Markdown files |
| 23 | + id: changed-files |
| 24 | + run: | |
| 25 | + CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '\.md$' || true) |
| 26 | + CHANGED_FILES="${CHANGED_FILES//$'\n'/ }" |
| 27 | + echo "CHANGED_FILES=$CHANGED_FILES" >> $GITHUB_ENV |
| 28 | + echo "Changed Markdown files: $CHANGED_FILES" |
| 29 | +
|
| 30 | + # 3️⃣ Skip if no Markdown files changed |
| 31 | + - name: Skip if no Markdown files changed |
| 32 | + if: env.CHANGED_FILES == '' |
| 33 | + run: | |
| 34 | + echo "No Markdown files changed. Skipping link check." |
| 35 | + exit 0 |
| 36 | +
|
| 37 | + # 4️⃣ Run Lychee on changed files |
| 38 | + - name: Run Lychee |
| 39 | + id: run-lychee |
| 40 | + uses: lycheeverse/lychee-action@v2 |
| 41 | + with: |
| 42 | + args: | |
| 43 | + --no-progress |
| 44 | + --include-fragments |
| 45 | + --format detailed |
| 46 | + ${{ env.CHANGED_FILES }} |
| 47 | + output: lychee/out_raw.md |
| 48 | + fail: false # ✅ don't fail yet, let us capture output |
| 49 | + |
| 50 | + # 5️⃣ Format Lychee output (user-friendly, relative paths) |
| 51 | + - name: Format Lychee report |
| 52 | + id: format-report |
| 53 | + if: always() |
| 54 | + run: | |
| 55 | + mkdir -p lychee |
| 56 | + : > lychee/comment.md # start with empty file |
| 57 | + |
| 58 | + awk ' |
| 59 | + /^Errors in / { |
| 60 | + file=$3 |
| 61 | + gsub("^/home/runner/work/UmbracoDocs/UmbracoDocs/", "", file) |
| 62 | + print "\nBroken links found in:\n" file >> "lychee/comment.md" |
| 63 | + next |
| 64 | + } |
| 65 | + |
| 66 | + /\[ERROR\]/ { |
| 67 | + msg = $0 |
| 68 | + sub(/^- \[ \] /, "", msg) |
| 69 | + sub(/^\[ERROR\] /, "", msg) |
| 70 | + gsub("^file:///home/runner/work/UmbracoDocs/UmbracoDocs/", "", msg) |
| 71 | + print "\n⚓ Anchor not found → " msg >> "lychee/comment.md" |
| 72 | + next |
| 73 | + } |
| 74 | + |
| 75 | + /\[404\]/ { |
| 76 | + msg = $0 |
| 77 | + sub(/^- \[ \] /, "", msg) |
| 78 | + sub(/^\[404\] /, "", msg) |
| 79 | + print "\n❌ 404 Not Found → " msg >> "lychee/comment.md" |
| 80 | + next |
| 81 | + } |
| 82 | + |
| 83 | + /\[301\]|\[302\]/ { |
| 84 | + msg = $0 |
| 85 | + sub(/^- \[ \] /, "", msg) |
| 86 | + sub(/^\[(301|302)\] /, "", msg) |
| 87 | + print "\n🔀 Redirect → " msg >> "lychee/comment.md" |
| 88 | + next |
| 89 | + } |
| 90 | + |
| 91 | + /Timeout/ && !/Timeouts/ { |
| 92 | + msg = $0 |
| 93 | + sub(/^- \[ \] /, "", msg) |
| 94 | + print "\n⏳ Timeout → " msg >> "lychee/comment.md" |
| 95 | + next |
| 96 | + } |
| 97 | + ' lychee/out_raw.md |
| 98 | + |
| 99 | + # Add header only if we found content |
| 100 | + if [ -s lychee/comment.md ]; then |
| 101 | + sed -i '1i **The Link Checker found broken links in your PR**.\n Please review the following list:\n' lychee/comment.md |
| 102 | + echo "has_content=true" >> $GITHUB_OUTPUT |
| 103 | + else |
| 104 | + echo "has_content=false" >> $GITHUB_OUTPUT |
| 105 | + fi |
| 106 | + |
| 107 | + # 6️⃣ Comment broken links on PR (if present) |
| 108 | + - name: Comment broken links |
| 109 | + if: always() && (env.CHANGED_FILES != '') && (steps.format-report.outputs.has_content == 'true') |
| 110 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 111 | + with: |
| 112 | + path: lychee/comment.md |
| 113 | + recreate: true |
| 114 | + |
| 115 | + # 7️⃣ Fail workflow if broken links exist |
| 116 | + - name: Fail workflow if broken links |
| 117 | + if: steps.format-report.outputs.has_content == 'true' |
| 118 | + run: | |
| 119 | + echo "❌ Broken links detected. Please review the PR comment for details." |
| 120 | + exit 1 |
0 commit comments