This guide will help you setup a bare git repository (essentially a repository which serves as a "server") to be able to automatically deploy its content when a push is received.
This can be useful if you have by example a web server (like bluehost or simple vps) where you which to deploy your code without setting up a full code deployment Pipeline through utilities like Jenkins.
The strategy used in this guide to automate the deployment is through the post-receive git hook.
The hook configuration is flexible and modular so you can split your deployment procedure through multiple files and it doesn't enforce a language as long as you can run the script with the first line as a shebang like this on the first line: #!/bin/bash
(replace /bin/bash
with the actual executable which should be used to run your script)
Note: This guide doesn't focus on setting up transfert solution like ssh or http nor how to install the git client, you'll need to figure it out on your own.
- Basic git and bash understanding
- A bash terminal (kinda hard to miss on linux, on windows you can use msys2 (git bash) or cygwin)
- A server an client computer with the git client installed
- A server setup with a supported mean to clone and push a git repository (ssh/git/http(s)) Be certain the client is able to login (ssh) or have access to the content (http(s))
- On your server
- On your server locate where you want your git repository to be and create a
{repo name}.git
folder - In that folder initialize your repository with the command:
git init --bare
- Go into the
hooks
folder and copy thepost-receive
script which is available a bit lower. - Make the hook script executable with:
chmod +x post-receive
- Make sure the appropriate user can read/write the repository.
- On your server locate where you want your git repository to be and create a
- On your client
- Create a folder and initialize an empty repository with
git init
- Add your server's remote repository with:
git remote add origin {remote address}
- Add your code
- Now you can run the regular:
git add .; git commit -m '{message}'; git push -u origin master
.
- Create a folder and initialize an empty repository with
At this point your code should by synced to the remote repository and be automatically deployed.
You have two solution to be able to deploy your code. You can make a deploy script which will be executed once the code is received by the server. The other way involve making a collection of smaller script which will handle all the operations needed to deploy the code.
At the root of your repository working tree add a script named: .gitdeploy
. The language your script is using doesn't matter as long as you can run your script with a shebang at the top of the file. The post-receive hook uses the shebang information to find which runtime needs to be called to execute the script.
At the root of your repository working tree add a folder named: .gitdeploy
. In that folder you are free to create as many scripts as you wish to actually deploy your application. The scripts are executed in alphabetical order (numbers). Like for the single script solution, you aren't restricted when it comes to the scripting language.
When it comes to the multiple file solution, some sanity checking is done. If your script doesn't end with an error code 0, then the deployment is canceled.
It's important each script has its own shebang (#!/path/to/bin
) on the very first line of the script. The shebang is used for runtime detection therefore it is mandatory.
Your script will be called by the runtime with the following positional arguments:
- Git work tree root path
- Branch name
- Git ref
Don't hesitate to provide feedback and suggest changes to the hook.