Reputation: 99
When I push commits to GitHub repo, I need to make checks on the commits message.
and reject the push if the commits do not follow the semantics.
(feat, fix, update, etc.)
I tried hooks but it only run locally.
I also tried GitHub Actions, but it does not reject the push it only rejects the merge.
Upvotes: -1
Views: 34
Reputation: 151
If you have control over your server, you can use a pre-receive hook in conjunction with GitHub Actions. However, GitHub Actions will be your primary CI/CD tool if you're using repositories hosted on GitHub.
1- Create github Action : Create a .github/workflows/commit-message-check.yml file then add
name: Commit Message Check
on:
push:
branches:
- main
jobs:
commit-message:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Check commit messages
run: |
commits=$(git log origin/main..HEAD --pretty=format:%s)
pattern="^(feat|fix|update|docs|style|refactor|perf|test|chore): .+"
for commit in $commits; do
if ! [[ $commit =~ $pattern ]]; then
echo "Invalid commit message: $commit"
echo "Commit messages must follow the format: feat: ..., fix: ..., etc."
exit 1
fi
done
The .github/workflows/commit-message-check.yml file should be committed and pushed to your repository. The action will now execute anytime you push to the designated branch, which is main in this case.
Workflow begins when a push is made to the specified branch. It compares the commit messages to a specified regex pattern to find the commits made in that push. The action fails if any commit message does not follow the pattern; it prints an error and exits with a non-zero status.
Upvotes: 1