Skip to content

Using Pre-Receive Hooks in GitLab

Pre-receive hooks allow custom validation scripts to run on the GitLab server before commits are accepted.

  1. On the GitLab server, navigate to the repository’s custom_hooks directory.
  2. Create an executable script named pre-receive.
  3. Implement checks such as validating commit messages, scanning for secrets, or enforcing file size limits.
  4. Save the script and ensure it has execute permission:
    Terminal window
    chmod +x pre-receive
#!/bin/sh
while read oldrev newrev refname; do
if ! git log --format=%s $oldrev..$newrev | grep -q '^[A-Z]\+-[0-9]\+'; then
echo "JIRA ID missing in commit message" >&2
exit 1
fi
done
  • Keep hook scripts in version control for traceability.
  • Log hook failures to assist developers.
  • Test hooks in a staging environment before production use.