|
| 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 | + |
| 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) |
0 commit comments