Using hooks to deploy

Hi, I would like to use Git Hooks to push the changes to a production server.

I already configured a pre-receive and post-receive hooks and it is working, but I need to push to different locations depending on the branch that I am working on.

I think that I need to access the $GIT_PUSH_OPTION_COUNT and $GIT_PUSH_OPTION_X variables, but they are always empty in the pre-receive and post-receive hooks.

Can someone point me to the right direction on this?

Below is my post-receive hook. The master branch is hardcoded, but I need it to be dynamic.

#!/bin/sh
SERVER="remoteipaddress"
PROJECT="projectname"
PROJECT_FOLDER="/var/www/clients/client0/web1/web"
USERNAME=web1
USERGROUP=client0

echo "starting post-receive"
git remote add origin [email protected]$SERVER:/var/repo/$PROJECT.git
git push origin master
ssh [email protected]$SERVER PROJECT=$PROJECT PROJECT_FOLDER=$PROJECT_FOLDER USERNAME=$USERNAME USERGROUP=$USERGROUP 'bash -s' <<'ENDSSH'
  	echo "connected";
	cd $PROJECT_FOLDER
	echo "updating project"
    git reset --hard
    git pull origin master
    chown -Rf $USERNAME:$USERGROUP .
ENDSSH
echo "done post-receive"

Thanks!

Hi, which Gogs version you have?

After some digging, I don’t think there is an environment variable tells you which branch the changes belong, but I think it’s easy doable if you’re running 0.10 or higher.

Please file an issue on GitHub for this request (with a link to this post for context), thanks!

I believe Git provides the branch name as a parameter to the post_receive hook…

1 Like

Hi, I ended up using git rev-parse to get the branch name.

I did something like this:

while read oldrev newrev refname
do
BRANCH=$(git rev-parse --symbolic --abbrev-ref $refname)
done

Unfortunately, I cold not access the parameters that git sent along to my post_receive script. But the 4 lines above helped me.

Thanks for the help!