Skip to content

Instantly share code, notes, and snippets.

@RenatoExpert
Created May 14, 2024 15:05
Show Gist options
  • Save RenatoExpert/003db6e50d1f51fa9f916241b5e8596d to your computer and use it in GitHub Desktop.
Save RenatoExpert/003db6e50d1f51fa9f916241b5e8596d to your computer and use it in GitHub Desktop.
Make CI/CD pipelines on git servers with this hook. Paste as .git/hooks/post-receive with run privilege
#!/usr/bin/env bash
GIT_DIR="/home/git/myrepo"
DEPLOY_DIR="/home/git/ops/deploy"
TEST_DIR="/home/git/ops/test"
MAIN="main"
TEST="test"
while read oldrev newrev ref
do
if [[ $ref = refs/heads/$MAIN ]];
then
echo "Ref $ref received. Deploying ${MAIN} branch to production..."
#rm -rf $DEPLOY_DIR/*
git --work-tree=$DEPLOY_DIR --git-dir=$GIT_DIR checkout -f $MAIN
cd $DEPLOY_DIR && docker compose -f docker-compose.yml -f docker-compose-prod.yml up --build -d
elif [[ $ref = refs/heads/$TEST ]];
then
echo "Ref $ref received. Deploying ${TEST} branch to test enviroment..."
#rm -rf $TEST_DIR/*
git --work-tree=$TEST_DIR --git-dir=$GIT_DIR checkout -f $TEST
cd $TEST_DIR && docker compose -f docker-compose.yml -f docker-compose-test.yml up --build -d
else
echo "Ref $ref received. Doing nothing: only $MAIN and $TEST branches may be deployed on this server."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment