Skip to content

Commit 4fa5a75

Browse files
Document junit (#147)
* Simplify GitHub CI action recipes * Document JUnit report annotations * Document GitLab CI
1 parent 763604e commit 4fa5a75

File tree

13 files changed

+218
-163
lines changed

13 files changed

+218
-163
lines changed

.github/workflows/check-links.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ jobs:
2323
id: lychee
2424
uses: lycheeverse/lychee-action@v2
2525
with:
26-
args: '--root-dir ${{ github.workspace }}/dist --exclude-all-private dist'
26+
args: "--root-dir ${{ github.workspace }}/dist --exclude-all-private dist"
2727
fail: false

astro.config.mjs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,10 @@ export default defineConfig({
7474
],
7575
},
7676
{
77-
label: "GitHub Action Recipes",
77+
label: "Continuous integration",
7878
items: [
79-
"github_action_recipes/check-repository",
80-
"github_action_recipes/pull-requests",
81-
"github_action_recipes/archived-links",
82-
"github_action_recipes/add-pr-comment",
83-
"github_action_recipes/caching",
79+
"continuous-integration/github",
80+
"continuous-integration/gitlab",
8481
],
8582
},
8683
{
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
---
2+
title: GitHub CI recipes
3+
---
4+
5+
import CodeBlock from "../../../components/code.astro";
6+
import { Aside } from "@astrojs/starlight/components";
7+
8+
## Basic example
9+
10+
This recipe demonstrates how to set up an automated workflow
11+
that will check all repository links on push.
12+
13+
```yaml
14+
name: Links
15+
16+
on:
17+
push:
18+
19+
jobs:
20+
linkChecker:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v5
24+
- name: Link Checker
25+
id: lychee
26+
uses: lycheeverse/lychee-action@v2
27+
with:
28+
format: "markdown"
29+
args: ". --verbose"
30+
```
31+
32+
## Pull request comment
33+
34+
[sticky-pull-request-comment](https://github.com/marocchino/sticky-pull-request-comment)
35+
is a practical way to display lychee's results on a pull request using a sticky comment.
36+
37+
```yaml
38+
name: Check links
39+
40+
on:
41+
pull_request:
42+
types: [opened, synchronize, reopened]
43+
44+
jobs:
45+
check-links:
46+
runs-on: ubuntu-latest
47+
steps:
48+
- uses: actions/checkout@v5
49+
- uses: lycheeverse/lychee-action@v2
50+
- name: Comment broken links
51+
uses: marocchino/sticky-pull-request-comment@v2
52+
with:
53+
path: lychee/out.md
54+
```
55+
56+
## Annotate link check errors
57+
58+
Using the `junit` format it's possible to output the link check results
59+
as a [JUnit test report](https://github.com/testmoapp/junitxml).
60+
As shown in the below screenshot, this output can then be used
61+
to annotate link check failures directly in the code of a pull request
62+
using [action-junit-report](https://github.com/mikepenz/action-junit-report).
63+
64+
![The PR diff view showing an annotation on line 2 of JUNIT-DEMO.md, indicating a failed link check on that line](images/junit-annotations-github.png)
65+
66+
```yaml
67+
name: Check links
68+
on:
69+
pull_request:
70+
71+
jobs:
72+
junit:
73+
runs-on: ubuntu-latest
74+
75+
steps:
76+
- uses: actions/checkout@v4
77+
78+
- name: Check links
79+
id: lychee
80+
uses: lycheeverse/lychee-action@v2
81+
with:
82+
fail: false # continue on failures
83+
format: "junit"
84+
output: "results.xml"
85+
86+
- name: Publish link check report
87+
uses: mikepenz/action-junit-report@v6
88+
with:
89+
report_paths: "results.xml"
90+
annotate_only: true
91+
fail_on_failure: true # only fail in the end
92+
```
93+
94+
## Caching
95+
96+
Caching is a great way to speed up your CI/CD pipeline and
97+
to reduce the number of requests performed.
98+
Caching is achieved by providing the `--cache` flag
99+
and reusing the `.lycheecache` file between consecutive runs.
100+
Below is an example workflow that caches lychee results.
101+
102+
```yaml
103+
name: Check URLs with lychee
104+
105+
on:
106+
push:
107+
108+
jobs:
109+
linkChecker:
110+
runs-on: ubuntu-latest
111+
steps:
112+
# Cache lychee results (e.g. to avoid hitting rate limits)
113+
- name: Restore lychee cache
114+
uses: actions/cache@v4
115+
with:
116+
path: .lycheecache
117+
key: cache-lychee-${{ github.sha }}
118+
restore-keys: cache-lychee-
119+
120+
- uses: actions/checkout@v5
121+
122+
- name: lychee URL checker
123+
uses: lycheeverse/lychee-action@v2
124+
with:
125+
# arguments with file types to check
126+
args: >-
127+
--cache
128+
--verbose
129+
--no-progress
130+
'./**/*.md'
131+
'./**/*.html'
132+
env:
133+
# to be used in case rate limits are surpassed
134+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
135+
```
136+
137+
This pipeline will automatically cache the results of the lychee run.
138+
Note that the cache will only be created if the run was successful.
139+
If you need more control over when caches are restored and saved, you can split
140+
the cache step and e.g. ensure to always save the cache (also when the link
141+
check step fails):
142+
143+
```yaml
144+
- name: Restore lychee cache
145+
id: restore-cache
146+
uses: actions/cache/restore@v3
147+
with:
148+
path: .lycheecache
149+
key: cache-lychee-${{ github.sha }}
150+
restore-keys: cache-lychee-
151+
152+
- name: Run lychee
153+
uses: lycheeverse/lychee-action@v2
154+
with:
155+
args: "--cache --max-cache-age 1d ."
156+
157+
- name: Save lychee cache
158+
uses: actions/cache/save@v3
159+
if: always()
160+
with:
161+
path: .lycheecache
162+
key: ${{ steps.restore-cache.outputs.cache-primary-key }}
163+
```
164+
165+
## Fixing broken links with archived versions
166+
167+
[David Gardiner](https://david.gardiner.net.au/) wrote a series of blog posts
168+
about how he used lychee-action to replace broken links on his website with
169+
archived versions. The posts are a great example of how to use lychee-action
170+
together with other actions to achieve a specific goal. Thanks David for the
171+
great write-up!
172+
173+
To achieve this, David used the [Wayback Machine Query Github Action](https://github.com/marketplace/actions/wayback-machine-query) to query
174+
for archived versions of broken websites. The results of the query were then fed
175+
into the [Replace multiple strings in files action](https://github.com/marketplace/actions/replace-multiple-strings-in-files)
176+
to update the files.
177+
178+
Here are the links to the blog posts which explain the process in more detail:
179+
180+
- [Fixing my blog (part 2) - Broken links](https://david.gardiner.net.au/2022/04/blog-fix-part2.html)
181+
- [Fixing my blog (part 3) - Querying the Wayback Machine](https://david.gardiner.net.au/2022/04/blog-fix-part3.html)
182+
- [Fixing my blog (part 4) - Updating the files](https://david.gardiner.net.au/2022/04/blog-fix-part4.html)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: GitLab CI recipes
3+
---
4+
5+
import CodeBlock from "../../../components/code.astro";
6+
import { Aside } from "@astrojs/starlight/components";
7+
8+
## Basic example
9+
10+
This recipe demonstrates how to set up a CI job to check links in a repository.
11+
GitLab integrates nicely with the JUnit XML report format.
12+
This makes it easy to visualise link check results as test reports,
13+
as shown in the below screenshot.
14+
15+
![The GitLab PR showing a test summary where 2 out of 12 tests failed, where each test indicates a link check](images/junit-annotations-gitlab.png)
16+
17+
```yaml
18+
Check links:
19+
stage: test
20+
script:
21+
- apt-get update -qq && apt-get install -y -qq wget
22+
# recommended to pin a stable version such as "lychee-v0.24.0"
23+
- version="nightly"
24+
- wget -qO- "https://github.com/lycheeverse/lychee/releases/download/$version/lychee-x86_64-unknown-linux-gnu.tar.gz" | tar -xz
25+
- ./lychee --verbose --include-mail --format junit --output results.xml TEST.md
26+
artifacts:
27+
when: always
28+
reports:
29+
junit: results.xml
30+
```
183 KB
Loading
55.7 KB
Loading

src/content/docs/github_action_recipes/add-pr-comment.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/content/docs/github_action_recipes/archived-links.md

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/content/docs/github_action_recipes/caching.md

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/content/docs/github_action_recipes/check-repository.mdx

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)