#!/bin/bash | |
zero_commit="0000000000000000000000000000000000000000" | |
excludeExisting="--not --all" | |
while read oldrev newrev refname; do | |
# echo "payload" | |
echo $refname $oldrev $newrev | |
# branch or tag get deleted | |
if [ "$newrev" = "$zero_commit" ]; then | |
continue | |
fi | |
# Check for new branch or tag | |
if [ "$oldrev" = "$zero_commit" ]; then | |
span=`git rev-list $newrev $excludeExisting` | |
else | |
span=`git rev-list $oldrev..$newrev $excludeExisting` | |
fi | |
for COMMIT in $span; | |
do | |
for FILE in `git log -1 --name-only --pretty=format:'' $COMMIT`; | |
do | |
echo "rejecting all pushes" | |
exit 1 | |
done | |
done | |
done | |
exit 0 |
#!/bin/bash | |
# Place this script in gitlab server directory -> <path_to_your_gitlab_server_root>/hooks/pre-receive.d | |
# Create directory,if it does not exists -> mkdir -p <path_to_your_gitlab_server_root>/hooks/pre-receive.d | |
# Get input data passed along pre-receive hook | |
read old_sha new_sha refname | |
# Default separator is ' ', change to ',' | |
IFS="," | |
# Use env variable GL_USERNAME to get the matching details from users csv file | |
# This file can be easily generated from the database that you have configured for your gitlab instance. | |
# It contains records in following format - <username>,<user_email>,<user_name> | |
IFS=', ' read -r -a validuserarray <<< `grep -i "$GL_USERNAME," /tmp/gituser.csv ` | |
valid_user_email=${validuserarray[1]} | |
valid_user_name=${validuserarray[2]} | |
# Get the last log user details from git log | |
IFS=', ' read -r -a incoming_committer_array <<< `git log -1 "$new_sha" --pretty=%ce,%cn | tr '[:upper:]' '[:lower:]'` | |
IFS=', ' read -r -a incoming_author_array <<< `git log -1 "$new_sha" --pretty=%ae,%an | tr '[:upper:]' '[:lower:]'` | |
# If no match found, fail the push | |
if [[ ${#validuserarray[@]} < 3 ]]; then | |
echo "GL-HOOK-ERR: You are not authorised to perform this action." | |
exit 1 | |
fi | |
# Ensure no conflict markers are there | |
if git diff "$old_sha" "$new_sha" | grep -qE '^\+(<<<<<<<|>>>>>>>)'; then | |
echo "GL-HOOK-ERR: Code has conflict markers. Please resolve and retry." | |
exit 1 | |
fi | |
# Validate author email ends with domain.com | |
if ! [[ "${incoming_author_array[0]}" =~ ^[A-Za-z0-9.]+[@]domain\.com$ ]]; then | |
echo "GL-HOOK-ERR: Author email address ${incoming_author_array[0]} is invalid." | |
exit 1 | |
fi | |
# Validate committer email | |
if [ "${valid_user_email}" != "${incoming_committer_array[0]}" ]; then | |
echo "GL-HOOK-ERR: Committer email address ${incoming_committer_array[0]} is invalid." | |
exit 1 | |
fi | |
# Validate committer name | |
if [ "${valid_user_name}" != "${incoming_committer_array[1]}" ]; then | |
echo "GL-HOOK-ERR: Committer name ${incoming_committer_array[1]} is invalid." | |
exit 1 | |
fi | |
exit 0 |
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND