-
-
Save shazow/4429856 to your computer and use it in GitHub Desktop.
post-receive hook to push-deploy with git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# post-receive hook to push-deploy multiple branches using a git repo. | |
# | |
# Deploy by pushing $DEPLOY_BRANCH | |
# Rollback by force-pushing a new reference to $OVERRIDE_TAG | |
# | |
# 1. Setup your git repo: | |
# | |
# git init --bare | |
# | |
# 2. Put this hook script in hooks/post-receive | |
# 3. Make as many worktree target dirs as you want and configure | |
# `which_worktree` and `deploy` below accordingly. Such as... | |
# | |
# mkdir -p ../branches/{master,staging}/src | |
# | |
# 4. Push to the repo and the appropriate worktree will be deployed. | |
# Or run the script manually with the branch name: | |
# | |
# hooks/post-receive master | |
# | |
# Enjoy. | |
## | |
# Override these: | |
function which_worktree() { | |
# Given a branch name, print the expected path of the worktree. | |
# If the path does not exist, deployment will be skipped for the branch. | |
branch="$1" | |
if [ "$branch" == "master" ]; then | |
echo "../app/src" | |
fi | |
} | |
function deploy() { | |
# Given a branch name and worktree path, perform desired deployment steps. | |
branch="$1" | |
worktree="$2" | |
cd "${worktree}" | |
. ../env/bin/activate | |
make | |
# Or maybe `docker run ...` something. Whatever floats your config. | |
} | |
## | |
# The meaty logic that shouldn't need touching: | |
git_dir="$PWD" | |
if [ "$1" ]; then | |
branch="$1" | |
worktree="$(which_worktree $branch)" | |
echo "Manual deploy on branch: ${branch}" | |
if [ ! -d "$worktree" ]; then | |
echo "Worktree does not exist, skipping: ${worktree}" | |
exit 1; | |
fi | |
deploy "$branch" "$worktree" | |
exit 0; | |
fi | |
while read oldrev newrev ref; do | |
branch="$(echo $ref | cut -d '/' -f3-)" | |
branch="${ref##*/}" | |
echo "Received branch: ${branch}" | |
worktree="$(which_worktree $branch)" | |
if [ "$worktree" ] && [ -d "$worktree" ]; then | |
echo "Deploying $branch." | |
cd "$git_dir" | |
git --work-tree="$worktree" checkout -q -f "$branch" | |
deploy "$branch" "$worktree" | |
fi | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# post-receive hook to push-deploy into a git repo. | |
# | |
# Deploy by pushing $DEPLOY_BRANCH. | |
# Rollback by force-pushing into the branch. | |
# | |
# 1. Setup your git repo: | |
# | |
# git init --bare | |
# | |
# 2. Put this hook script in hooks/post-receive | |
# 3. Make as many worktree target dirs as you want and configure | |
# `which_worktree` and `deploy` below accordingly. Such as... | |
# | |
# mkdir -p ../branches/{master,staging}/src | |
# | |
# 4. Push to the repo and the appropriate worktree will be deployed. | |
# | |
# Enjoy. | |
## | |
# Override these: | |
function which_worktree() { | |
# Given a branch name, print the expected path of the worktree. | |
# If the path does not exist, deployment will be skipped for the branch. | |
branch="$1" | |
if [ "$branch" == "master" ]; then | |
echo "../app/src" | |
fi | |
} | |
function deploy() { | |
# Given a branch name and worktree path, perform desired deployment steps. | |
branch="$1" | |
worktree="$2" | |
container_id="XXX" | |
docker run -volumes-from "${container_id}" -i uwsgi bash -c "source /app/env/bin/activate && cd /app/src && make -e 'INI_FILE=production.ini' && touch .reload" | |
if docker ps | grep -q "${container_id}"; then | |
echo -e "${CGREEN}Server is happy. :)${CNONE}" | |
else | |
echo -e "${CRED}SERVER IS DOWN! Recent log:${CYELLOW}" | |
docker logs "${container_id}" | tail -n 40 | |
echo -en "$CNONE"} | |
fi | |
} | |
## | |
# The meaty logic that shouldn't need touching: | |
# Symbol and color shortcuts, borrowed from Gitolite-Hooks <3 | |
BLANK=$(echo -e "\e[1G ") | |
ARROW=$(echo -e "\e[1G------> ") | |
CNONE=$(echo -e "\e[00m") | |
CBLACK=$(echo -e "\e[1;30m") | |
CWHITE=$(echo -e "\e[0;37m") | |
CGREEN=$(echo -e "\e[1;32m") | |
CBLUE=$(echo -e "\e[1;34m") | |
CRED=$(echo -e "\e[1;31m") | |
CYELLOW=$(echo -e "\e[1;33m") | |
function prefix() { | |
sed "s/^/$@/" | |
} | |
if [ "$1" ]; then | |
branch="$1" | |
worktree="$(which_worktree $branch)" | |
echo -e "${ARROW}${CBLUE}Manual deploy on branch:${CNONE} ${branch}" | |
if [ ! -d "$worktree" ]; then | |
echo -e "${BLANK}Worktree does not exist, skipping: ${worktree}" | |
exit 1; | |
fi | |
git --work-tree="$worktree" checkout -q -f "$branch" | |
echo -en "${CWHITE}" | |
deploy "$branch" "$worktree" | prefix "${BLANK}" | |
echo -en "${CNONE}" | |
exit 0; | |
fi | |
while read oldrev newrev ref; do | |
branch="$(echo $ref | cut -d '/' -f3)" | |
echo "${ARROW}Received branch: ${branch}" | |
worktree="$(which_worktree $branch)" | |
if [ "$worktree" ] && [ -d "$worktree" ]; then | |
echo "${ARROW}${CBLUE}Deploying branch:${CNONE} ${branch}" | |
cd "$GIT_DIR" | |
git --work-tree="$worktree" checkout -q -f "$branch" | |
echo -en "${CWHITE}" | |
deploy "$branch" "$worktree" | prefix "${BLANK}" | |
echo -en "${CNONE}" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment