Skip to content

Instantly share code, notes, and snippets.

@amarjanica
Last active May 15, 2024 09:25
Show Gist options
  • Save amarjanica/4ed810a60b08231f1f731ca859c1b1e1 to your computer and use it in GitHub Desktop.
Save amarjanica/4ed810a60b08231f1f731ca859c1b1e1 to your computer and use it in GitHub Desktop.
Bash based Basic Commit Linter Github Workflow

Github Workflow for Linting Git Commits

To enable this check, create a workflow file within the .github/workflows directory, such as check-commits.yml.
It will trigger when a pull request is created to main branch and will fail if a commit contains any of the special keywords like:

  • WIP - convention for commits that are work in progress
  • [skip ci] - skips github workflow.
  • Signed-off - git gui offers that option if you're lazy to write your own message.
  • Merge something-branch into something - extra commit that happens when you merge changes. Solution is to rebase it.

Check is case insensitive.

I wrote about it in my article https://www.amarjanica.com/git-commits-that-dont-suck-a-beginners-guide#Lazy_Persons_Commit_Linter. I also wrote about some other stuff like git rebasing and clean commits.

name: Check Commits
on:
pull_request:
branches:
- main
jobs:
check-commits:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Validate commit messages
run: |
git fetch origin ${{ github.base_ref }} ${{ github.head_ref }}
pattern='^.*(\bwip\b|\[skip ci\]|merge.+into|signed-off).*$'
commit_messages=$(git log origin/${{ github.base_ref }}..origin/${{ github.head_ref }} --pretty=format:%s)
echo "$commit_messages" | while read line; do
if [[ "${line,,}" =~ $pattern ]]; then
echo "Invalid commit message: '$line'"
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment