Skip to content

Instantly share code, notes, and snippets.

@seancheung
Created November 8, 2017 07:42
Show Gist options
  • Save seancheung/b867767e1030f59d0e7f861cb636b619 to your computer and use it in GitHub Desktop.
Save seancheung/b867767e1030f59d0e7f861cb636b619 to your computer and use it in GitHub Desktop.
git post-receive hook(docker)
#!/bin/sh
WORK_BRANCH=production
WORK_DIR=/path/to/workdir
COMPOSE_FILE=/path/to/docker-compose.yml
SU_PWD=sudo_pass
SERVICE=service_name
CMD=command
restart()
{
echo "$SU_PWD" | sudo -S docker-compose --file "$COMPOSE_FILE" restart $SERVICE
}
exec()
{
echo "$SU_PWD" | sudo -S docker-compose --file "$COMPOSE_FILE" exec $SERVICE "$CMD"
}
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "$WORK_BRANCH" == "$branch" ]; then
GIT_WORK_TREE="$WORK_DIR" git checkout $WORK_BRANCH -f
# restart, exec
fi
done
@bf
Copy link

bf commented Dec 6, 2020

I don't think it is safe to hard code the sudo password into a file. Why don't you just give the user permissions to interact with docker daemon by adding to docker group? :)

@seancheung
Copy link
Author

I don't think it is safe to hard code the sudo password into a file. Why don't you just give the user permissions to interact with docker daemon by adding to docker group? :)

You're right about that but this is used in a CI environment :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment