git post-receive hook to update multiple servers
I'开发者_JAVA百科m using git post-receive hook to deploy versions of the web application from three branches (master, staging, and stable) on three servers (development, testing and production). The pairing between branches and servers is currently hard-coded in the script. However I'd like to remove this restriction and make this hook possible to manage unlimited quantity of branches. It can be done in the following way:
- move all per-branch config options to some separate files, for example
.git/???/<branch_name>
- the main script will check if such file is available for every branch, source it and then deploy on the remote server using configuration parameters from that file.
However I don't know where exactly in .git
directory can I place such files. Or maybe there is a better solution?
Your options, as far as I can tell:
Use a stronger convention for server naming, so that config isn't necessary, beyond the base domain name. Leave that in the script. (That is, branchX -> branchX-deploy.example.com.)
Put a config file wherever you feel like in the .git directory, like you suggested. If it's not a filename git cares about, it'll never notice it. I'm not sure why you'd want to do this, though.
Put it in .git/config. Git lets you define arbitrary config parameters of the form
branch.<name>.foo
. (I have no idea if this is a feature or an oversight. It's not documented, as far as I know.) In your case, something likebranch.master.deploy_server
set todevelopment.example.com
. Your script could then just go through all branches, and check whether that config option is set or not. (Usegit config --get
.)Put a config file in your repository. This seems a lot better than hiding it away. You might as well track the settings. If necessary, provide a way to override them - supply a different config file as an argument, supply individual branches/servers as options, interactive prompts, whatever suits you.
Personally, I'd probably do the last one. Tracking settings, even if they're only default settings, can't hurt you.
精彩评论