Blame view

resources/post-receive.git-hook 1.89 KB
65f2cab9   hitier   Auto-Deploy files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
# 
# Continuous Deployment
# ---------------------
#
# This file is a post-receive hook to be installed in the
# ./hooks/ dir of a bare git repository.
#
# When pushing to that repo from outside (a dev machine,
# or a jenkins/gitlab ci_cd rule), it updates a working dir previously
# set by cd to ${TARGET} and get the latest code .

# How to set it up:
# -----------------
#
# On the server to deploy:
#
# 1- create a bare repository and set hook
#    git clone url-to-central-repo deploy-repo.git
#    cp post-receive.git-hook deploy-repo.git/hooks/post-receive 
#    
# 2- set a working dir and configure
#    cd /var/www/
#    git clone  deploy-repo.git your-app-dir
#    do_any_config_tasks
#
#
# On the desktop computer:
# 
# 1- add the server as a remote source
#    git remote add deploy url-to-deploy-repo.git
# 
# 2- try it out
#    git push deploy HEAD:BRANCH_NAME 

#
#    $TARGET: is the working dir you want to update at last
#    $BRANCH: is the branch you want to update from
#
3c0785d6   hitier   Auto deploy to ap...
40
TARGET="/var/www/pdc-web/"
65f2cab9   hitier   Auto-Deploy files
41
BRANCH="DEV"
3c0785d6   hitier   Auto deploy to ap...
42
GIT_DIR="/path/to/pdc-web.git/"
65f2cab9   hitier   Auto-Deploy files
43
44
45
46
47
48
49

while read oldrev newrev ref
do
	# only checking out the branch you would like to deploy
	if [ "$ref" = "refs/heads/$BRANCH" ];
	then
		echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
ee0bfaeb   hitier   Fix wrong shell c...
50
        [ -d $TARGET ] || exit
3c0785d6   hitier   Auto deploy to ap...
51
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
65f2cab9   hitier   Auto-Deploy files
52
        cd $TARGET || exit
65f2cab9   hitier   Auto-Deploy files
53
54
        touch *wsgi # now, trigger wsgidaemons restarting
        /bin/sh scripts/post-deploy.sh
65f2cab9   hitier   Auto-Deploy files
55
56
57
58
59
	else
		echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
	fi
done

3c0785d6   hitier   Auto deploy to ap...
60
61
62
63
64
# Alternately, it is possible to just update a git repo
# git stash # remove any modification, sorry guy.
# git checkout ${BRANCH} # set branch if not done yet, sorry guy
# git pull origin ${BRANCH} # get latest modifications, assumes  remote origin previously set
# exec git update-server-info
65f2cab9   hitier   Auto-Deploy files
65
66

# vim: ft=sh