question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to run commitlint in GitHub workflow on every commit of a push

See original GitHub issue

I created a new Github repository and followed https://commitlint.js.org/#/guides-local-setup. After that I created this workflow

name: Run commitlint on pull request
'on': pull_request
jobs:
  run-commitlint-on-pull-request:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: 14.x
      - name: Install dependencies
        run: npm install
      - name: Validate all commits from PR
        run: >
          for commit in $(git rev-list ${{ github.base_ref }}..${{
          github.head_ref }}); do
              npx commitlint --from $commit --to HEAD --verbose
          done

to validate each commit message in a PR. I created a new branch and made a change 3 times with the following commit messages:

  • invalid one
  • feat: valid two
  • invalid three

Expected Behavior

I would expect that commit message 1 and 3 are invalid so the workflow rejects the pull request.

Current Behavior

Unfortunately the validation passes.

image

I also tried this

run: npx commitlint --from ${{ github.base_ref }} --to ${{ github.head_ref }} --verbose

This time the workflow didn’t pass but the output result is wrong

image

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

As shown in the image I think my syntax is wrong and needs to get fixed.

Steps to Reproduce (for bugs)

  • Create a new Github repository
  • Clone it
  • Setup npm
  • Setup commitlint with husky as shown in the docs
  • Create a Github workflow (take my code from above)
  • Push everything
  • Create a new branch
  • Make some changes with valid and invalid commit messages
  • Create a PR
  • The PR should get rejected by the workflow but it passes

Context

I would like to validate each commit message from commits in a PR

Your Environment

Executable Version
commitlint/cli --version 12.1.1
commitlint/config-conventional --version 12.1.1
git --version 2.25.1
node --version 14

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

5reactions
matthiashermsencommented, May 1, 2021

I think this comment provides a solution

https://github.com/conventional-changelog/commitlint/issues/586#issuecomment-657226800

so your command should be

npx commitlint --from HEAD~${{ github.event.pull_request.commits }} --to HEAD --verbose

1reaction
knoctecommented, Sep 11, 2022

so your command should be

         for commit in $(git rev-list ${{ github.base_ref }}..${{
         github.head_ref }}); do
             npx commitlint --from HEAD~${{ github.event.pull_request.commits }} --to HEAD --verbose```

That didn’t work for me.

I had to tweak it to this:

run: |
  echo 'Going to run commitlint for ${{ github.event.pull_request.commits }} commits'
  npx commitlint --from ${{ github.event.pull_request.head.sha }}~${{ github.event.pull_request.commits }} --to ${{ github.event.pull_request.head.sha }} --verbose
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to run commitlint in GitHub workflow on every commit of a ...
Solution. The straight-forward solution is to use the --to and --from arguments of commitlint with the SHA-1 values instead of the branch ...
Read more >
Lints Pull Request commits with commitlint - the Github Action
Lints Pull Request commits with commitlint. Usage. Create a github workflow in the .github folder, e.g. .github/workflows/commitlint.yml ...
Read more >
Document use of commitlint as a github action (possible own ...
Provide a github actions cli just like commitlint-travis . Context. It should easy setup for running commitlint as part of a github actions ......
Read more >
commitlint-github-action fails yet commitlint cli passes? #57
But if I run commitlint's CLI directly on my branch, then we get no errors: $ commitlint -f main -t HEAD --config commitlint.config.js...
Read more >
How lint every single commit in a PR? · Issue #36 - GitHub
Run wagoid/commitlint-github-action@v15s Run ... name: On Pull Request on: [pull_request] jobs: lint-commit: runs-on: ubuntu-latest steps: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found