Lint commit with GitHub Actions


Problem

Commitlint has CI setup documentation for Travis CI and CircleCI but not for GitHub Actions.

I added the step to run npx commitlint --from=HEAD~1 in my workflow:

# .github/workflows/build.yml
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/[email protected]
      - uses: actions/[email protected]
        with:
          node-version: 14
      - run: npm install
      - run: npx commitlint --from=HEAD~1

But got the error:

Error: fatal: ambiguous argument 'HEAD~1..HEAD': unknown revision or path not in the working tree.

Solution

I discovered that actions/[email protected] does a shallow checkout by default, which means it only checks out the last commit:

- uses: actions/[email protected]
  with:
    fetch-depth: 1

To fetch the entire commit history, set fetch-depth to 0:

- uses: actions/[email protected]
  with:
    fetch-depth: 0

Alternatively, a positive integer can be passed to fetch N commits.

However, if a shallow checkout is permissible (fetch-depth: 1), then update commitlint to lint only the last commit message:

- run: git log -1 --pretty=format:"%s" | npx commitlint

Resources

Check out “Migrate Travis CI to GitHub Actions (Node.js)” for more information on how to set up GitHub Actions.



Please support this site and join our Discord!