Git post-receive hook and environment variables
Asked Answered
G

2

6

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?

Garvey answered 21/2, 2014 at 11:14 Comment(0)
T
1

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).

Trice answered 21/2, 2014 at 14:53 Comment(1)
This is a second case. I'm trying to checkout "stage" branch to staging area and perform some actions (like importing db from file), this is staging server so I though that the "staging path" is pretty important environment feature, so I tried to set it up as environment variable (it probably should be set in /etc/profile , but I'm just experimenting now). If hooks doesn't have information about environment - it will make things much less clean. :(Cheapen
S
2

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.

Severance answered 14/9, 2015 at 15:34 Comment(0)
T
1

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).

Trice answered 21/2, 2014 at 14:53 Comment(1)
This is a second case. I'm trying to checkout "stage" branch to staging area and perform some actions (like importing db from file), this is staging server so I though that the "staging path" is pretty important environment feature, so I tried to set it up as environment variable (it probably should be set in /etc/profile , but I'm just experimenting now). If hooks doesn't have information about environment - it will make things much less clean. :(Cheapen

© 2022 - 2024 — McMap. All rights reserved.