Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git post-receive hook and environment variables

I have git server running on Gitolite, under "git" system user. I added to ~/.profile of git user: export STAGE_PATH="/srv/http/stage" , as you can guess, I want to automagically put website from repo on stage when pushing changes. However it seems that variable STAGE_PATH is unavailable to me in post-receive hook. Is it normal? Is there a way to access environment variables from post-receive hook?

like image 503
Łukasz Zaroda Avatar asked Oct 20 '22 14:10

Łukasz Zaroda


2 Answers

The git hook will not login the git user to a shell and therefore things exported in the usual places won't be available. In fact only a rather small number of environment variables will be set when the hook is executed.

You may check this out by inserting for line in $(printenv); do echo $line; done) in your hook. Of course only for debugging purposes.

like image 88
uli_1973 Avatar answered Oct 23 '22 05:10

uli_1973


How about adding . ~git/.profile at the top of your post-receive hook script (assuming it's sh).

It's unclear exactly what you want, though. You either want:

1) just to push to an external spot, and don't care about commonality between repos. In which case, why do you need an environment variable from somewhere else? Why not put the final location inside the post-receive hook script itself?

2) You want to have some external variable that controls the root of where you're going to push a lot of different repos to, and it would be better to code that location into a single variable if you ever need to change it. If that's the case, what you're doing above makes sense. But you don't, necessarily, need to do it in the .profile in the first place. Clearly git is cleaning the environment for you before running your script. So, you should instead source an external file instead that contains your parameters (though I'd argue against using .profile for this).

like image 44
Wes Hardaker Avatar answered Oct 23 '22 03:10

Wes Hardaker