HTML Proofer generic
Install and run the
html-proofer
gem with a generic Ruby action setup
How to use a Ruby gem as a CLI tool to validate links in your static HTML, for both internal and external links.
Related sections
- HTML Proofer gem page in the Web recipes.
- For use of
html-proofer
outside of GH Actions or how to add it to yourGemfile
, see the
- For use of
- HTML Proofer wrappers
Samples
Use a GH Actions workflow to set up Ruby and gems, build the site and then run the proofer on the build output.
You might want to run the checker every time you build your site on your main branch and on a PR (so you can block bad code from being merged). Or you might run on a schedule, maybe against an existing output directory on gh-pages
branch instead of as a fresh build. See my Workflow Builder for more info.
Jekyll site
The example here is targeted at a Jekyll static site and is based on this blog post - Automatically Validate Links on a Jekyll Website.
main.yml
jobs: check-links: name: Check links runs-on: ubuntu-latest steps: - name: Checkout ποΈ uses: actions/checkout@v2 - name: Set up Ruby π uses: ruby/setup-ruby@v1 with: ruby-version: '2.7' bundler-cache: true - name: Install HTML Proofer π§ run: gem install html-proofer - name: Build π run: bundle exec jekyll build --trace - name: Check for broken links π§π run: htmlproofer --log-level :debug _site 2> links.log - name: Archive checker log π uses: actions/upload-artifact@v1 with: name: links-check.log path: links.log
Notes:
- If you prefer to install
html-proofer
from gems in yourGemfile
:- Take out the
gem install GEM_NAME
step. - Change the Check for broken links step to be
bundle exec htmlproofer ARGS
.
- Take out the
- The tool prints
stdout
as a count of URLs and files (a few lines only). Thestdout
content is the actually check breakdown, which can very long. - This example persists the checker log as an uploaded file.
- This makes it easier to view rather than as a part of the long workflow log.
- Note that using
&>
will send bothstdout
andstderr
to the file log (and print nothing in the workflow log), while using2>
will send only the URL checks onstderr
to the file log and still print a summary numbers in the workflow log.
Generic
Here for project not based on Ruby or Jekyll, but still need Ruby set up.
Supply your own build command for the Build step. And point the proofer tool at your build output directory, like build
or out
.
steps:
# ...
- name: Build π
run: npm run build
- name: Check for broken links π§π
run: htmlproofer --log-level :debug build
Handling errors
The default behavior of the tool is to exit on an non-zero exit code on at least one URL error.
This would swallow any fatal errors like bad flags, aborting the build and therefore stopping a deploy.
But if you donβt care about breaking links, especially if the testing is intermittent, then you can use the continue-on-error field.
e.g.
- name: Check for broken links π§π
run: htmlproofer # ...
continue-on-error: true
This is so that the check step doesnβt stop the workflow and mark it as failed.