Deploy a git repo using git-hooks, because git push production
is just awesome. To get more details about automated tasks using git hooks please follow: how-to-use-git-hooks-to-automate-development-and-deployment-tasks or setting-up-push-to-deploy-with-git or using-git-to-deploy-code.
To get the server setup, please follow the Prerequisites section.
wget https://pymet.github.io/git-deploy/git-deploy.py
python3 git-deploy.py myproject
You can download the script here or here.
The default config.json
is the following:
{
"*": {
"allow": true,
"work-tree": null,
"pre-message": null,
"post-message": null,
"timeout-message": null,
"timeout": null,
"exec": null
}
}
The "*"
rule will match any of the branches except for those that have a custom rule set.
The following snippet will allow push for the master branch only.
{
"master": {
"allow": true
},
"*": {
"allow": false
}
}
- The
allow
parameter will be used in thepre-receive
step. Ifallow
is set tofalse
, the branch will be rejected. - If
work-tree
parameter is notnull
, the content of the branch will be checked out there. The folder will be created if does not exists.
{
"master": {
"work-tree": "/var/www/html"
}
}
pre-message
,post-message
andtimeout-message
will be printed in the git push result.
{
"master": {
"pre-message": "This is the pre-message",
"post-message": "This is the post-message",
"timeout-message": "This is the timeout-message",
"timeout": 1.0,
"exec": "sleep 3"
}
}
Running git push dev
Counting objects: 3, done.
Writing objects: 100% (3/3), 246 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: This is the pre-message
remote: This is the post-message
remote: This is the timeout-message
To 123.123.123.123:testrepo
b1556f2..9a3b525 master -> master
- If
exec
is notnull
, it will be executed after the repo is checked out.
{
"master": {
"work-tree": "/path/to/your/project",
"exec": "make -C /path/to/your/project"
}
}
"exec": "make -C /path/to/your/project"
is the same as:
"exec": ["make", "-C", "/path/to/your/project"]
{
"master": {
"allow": false,
"pre-message": "One does not simpli push the master here. (try dev)"
},
"dev": {
"allow": true,
"work-tree": "/var/www/html"
}
}
Running git push dev
Counting objects: 3, done.
Writing objects: 100% (3/3), 247 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: One does not simpli push the master here. (try dev)
To 123.123.123.123:testrepo
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '[email protected]:testrepo'
Place complex-task.sh next to the config.json. Once the master branch is pushed, the complex-task.sh will be called.
+ myproject-hooks/
| config.json
| complex-task.sh
{
"master": {
"exec": "sh complex-task.sh"
}
}
- If
timeout
is notnull
, the task will be killed aftertimeout
seconds and thetimeout-message
will be shown.
{
"master": {
"timeout-message": "No one can sleep 5 seconds under 2.5 seconds",
"timeout": 2.5,
"exec": "sleep 5"
}
}
Counting objects: 2, done.
Writing objects: 100% (2/2), 197 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote: Config file is broken: Expecting property name enclosed in double quotes: line 5 column 2 (char 106)
remote: Config file is broken: Expecting property name enclosed in double quotes: line 5 column 2 (char 106)
To 123.123.123.123:testrepo
9a3b525..47cd617 master -> master
usage: git-deploy.py [-h] [-o ORIGIN] [-b BRANCH] [-v] [--git-user GIT_USER]
[--git-email GIT_EMAIL] [--git-msg GIT_MSG] [--offline]
path
Generate repository and branch for git-hooks
positional arguments:
path Path for the git repository
optional arguments:
-h, --help show this help message and exit
-o ORIGIN, --origin ORIGIN Origin to use for the hint
-b BRANCH, --branch BRANCH Branch for the hooks
-v, --verbose Set verbosity
--git-user GIT_USER Username for the commit
--git-email GIT_EMAIL Email for the commit
--git-msg GIT_MSG Message for the commit
--offline Skip using online tools for detecting the host IP
$ python3 git-deploy.py myproject
Add or set the remote url:
git remote add dev [email protected]:myproject
git remote set-url dev [email protected]:myproject
Clone the hooks branch:
git clone -b hooks [email protected]:myproject myproject-hooks
$ python3 git-deploy.py myproject --offline
$ python3 git-deploy.py myproject --verbose --offline
Working in /home/git/myproject
git init --bare --quiet
Working in /tmp/tmp4bpkoxok
git init --quiet
git add config.json
git checkout -b hooks --quiet
git -c user.name=GitBot -c user.email=gitbot@localhost commit -m "Initial config" --quiet
git remote add origin /home/git/myproject
git push origin hooks --quiet
Working in /home/git/myproject/hooks
ln -s receive.py pre-receive
ln -s receive.py post-receive
chmod 755 pre-receive post-receive
Working in /home/git/myproject
git --work-tree=/home/git/myproject/hooks/custom checkout hooks -f --quiet
$ python3 git-deploy.py myproject -b custom-hooks
...
Clone the custom-hooks branch:
git clone -b custom-hooks [email protected]:myproject myproject-hooks
...
$ python3 git-deploy.py myproject \
--git-user "John Doe" \
--git-email "john.doe@localhost" \
--git-message "Hello World!"
Thank you for the ipify-api