Skip to content

Commit d44da61

Browse files
authored
Add Link Checker Report Github Action (#94)
* add link checker workflow * fix cron syntax * update packageManager from yarn to pnpm * add --frozen-lockfile flag * update url in args in link-checker.yaml
1 parent 22bd955 commit d44da61

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

.github/workflows/link-checker.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: Link Checker
2+
3+
on:
4+
repository_dispatch:
5+
workflow_dispatch:
6+
schedule:
7+
- cron: '0 0 1 * *' # Run at midnight on the first of every month
8+
9+
jobs:
10+
linkChecker:
11+
name: Check and report broken links
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout Repository
15+
uses: actions/checkout@v4
16+
with:
17+
submodules: 'recursive'
18+
19+
- name: Corepack enable
20+
run: corepack enable
21+
22+
- name: Set up Node.js
23+
uses: actions/setup-node@v4
24+
with:
25+
node-version: '20'
26+
27+
- name: Get Token
28+
uses: actions/create-github-app-token@v1
29+
id: get_workflow_token
30+
with:
31+
app-id: ${{ vars.APP_ID }}
32+
private-key: ${{ secrets.PRIVATE_KEY }}
33+
34+
- name: Install Dependencies
35+
run: pnpm install --frozen-lockfile
36+
37+
- name: Serve App Locally
38+
run: pnpm run dev &
39+
40+
- name: Wait for App to Start
41+
run: sleep 20
42+
43+
# This will restore the lychee cache
44+
- name: Restore lychee cache
45+
uses: actions/cache@v4
46+
with:
47+
path: .lycheecache
48+
key: cache-lychee-${{ github.sha }}
49+
restore-keys: cache-lychee-
50+
51+
# This will run the link checker on all markdown files in the pages directory
52+
- name: Link Checker
53+
id: lychee
54+
uses: lycheeverse/lychee-action@v1
55+
with:
56+
args: --base https://tour.json-schema.org --verbose --no-progress --accept 200,204,429,403 './content/**/*.mdx' --cache --max-cache-age 1d https://tour.json-schema.org
57+
token: ${{secrets.GITHUB_TOKEN}}
58+
59+
- name: Install Octokit
60+
run: pnpm add @octokit/[email protected]
61+
62+
# This will create an issue with the link checker report if it does not exist, otherwise it will update the existing issue.
63+
64+
- name: Create Issue
65+
if: env.lychee_exit_code != 0
66+
uses: actions/github-script@v7
67+
env:
68+
AUTH_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
69+
with:
70+
script: |
71+
const { Octokit } = require("@octokit/core");
72+
const octokit = new Octokit({ auth: process.env.AUTH_TOKEN });
73+
const allIssues = await octokit.request('GET /repos/{owner}/{repo}/issues', {
74+
owner: context.repo.owner,
75+
repo: context.repo.repo
76+
});
77+
78+
const existingIssue = allIssues.data.find(issue => issue.title === 'Link Checker Report');
79+
if (existingIssue) {
80+
await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
81+
owner: context.repo.owner,
82+
repo: context.repo.repo,
83+
issue_number: existingIssue.number,
84+
body: '## Link Checker Report\n\n' + require('fs').readFileSync('./lychee/out.md', 'utf8')
85+
});
86+
} else {
87+
await octokit.request('POST /repos/{owner}/{repo}/issues', {
88+
owner: context.repo.owner,
89+
repo: context.repo.repo,
90+
title: 'Link Checker Report',
91+
body: '## Link Checker Report\n\n' + require('fs').readFileSync('./lychee/out.md', 'utf8')
92+
});
93+
}

0 commit comments

Comments
 (0)