Last active
December 27, 2015 07:59
-
-
Save jbgutierrez/7293187 to your computer and use it in GitHub Desktop.
Self-contained bash script to setup continuous deployment for nodejs apps built on top of ubuntu, git and foreman
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
DEPLOYER='deployer' | |
DEPLOYER_HOME="/home/$DEPLOYER" | |
PROJECT_NAME='node-js-sample' | |
PROJECT_REPO='https://github.com/heroku/node-js-sample.git' | |
# Provisining an ubuntu server | |
apt-get -y update | |
apt-get -y upgrade | |
apt-get -y install software-properties-common | |
add-apt-repository ppa:chris-lea/node.js | |
apt-get -y update | |
apt-get -y install git git-daemon-run curl nodejs | |
curl https://npmjs.org/install.sh | sh | |
npm install -g foreman | |
# Add a deployer user | |
adduser $DEPLOYER --disabled-login --gecos "" | |
# Use authorized_keys as a authorization mechanism for deploys | |
mkdir $DEPLOYER_HOME/.ssh | |
touch $DEPLOYER_HOME/.ssh/authorized_keys | |
# Setup the bare repo that will trigger deployments | |
git clone --bare $PROJECT_REPO $DEPLOYER_HOME/$PROJECT_NAME.git | |
cd $DEPLOYER_HOME/$PROJECT_NAME.git | |
# Configure pre hook to stop services | |
cat > $DEPLOYER_HOME/$PROJECT_NAME.git/hooks/pre-receive <<EOS | |
#!/bin/bash | |
function run { | |
cmd=\$1 | |
msg=\$2 | |
echo \$msg | |
# echo \$cmd | |
\${cmd} | |
} | |
run 'sudo stop foreman' 'Stopping services' | |
EOS | |
# Configure post hook to fetch changes and start services | |
cat > $DEPLOYER_HOME/$PROJECT_NAME.git/hooks/post-receive <<EOS | |
#!/bin/bash | |
unset GIT_DIR | |
function run { | |
cmd=\$1 | |
msg=\$2 | |
echo \$msg | |
# echo \$cmd | |
\${cmd} | |
} | |
cd $DEPLOYER_HOME/$PROJECT_NAME | |
run 'git pull -q origin' 'Pulling changes' | |
run 'sudo start foreman' 'Starting services' | |
EOS | |
# Clone git deployment repo | |
git clone $DEPLOYER_HOME/$PROJECT_NAME.git $DEPLOYER_HOME/$PROJECT_NAME | |
cd $DEPLOYER_HOME/$PROJECT_NAME | |
# Register project services | |
nf export -o /etc/init | |
# Fix ownership | |
chown -R $DEPLOYER.$DEPLOYER $DEPLOYER_HOME | |
# Ensure hooks execution permissions | |
chmod +x $DEPLOYER_HOME/$PROJECT_NAME.git/hooks/pre-receive | |
chmod +x $DEPLOYER_HOME/$PROJECT_NAME.git/hooks/post-receive | |
# Grant sudo permisions to restart services | |
cat >> /etc/sudoers <<EOS | |
$DEPLOYER ALL=NOPASSWD:/sbin/start foreman | |
$DEPLOYER ALL=NOPASSWD:/sbin/stop foreman | |
EOS | |
# Damoenize git server | |
cat > /etc/sv/git-daemon/run <<EOS | |
#!/bin/sh | |
exec 2>&1 | |
echo 'git-daemon starting.' | |
exec chpst -ugitdaemon \ | |
"$(git --exec-path)"/git-daemon --verbose --reuseaddr \ | |
--export-all --interpolated-path=/home/$DEPLOYER/%D \ | |
--base-path=/home/git | |
EOS | |
# Start git daemon and foreman services | |
sv restart git-daemon | |
start foreman | |
# | |
# Setup a developer machine | |
# | |
# → ssh root@server "cat >> /home/$DEPLOYER/.ssh/authorized_keys" < ~/.ssh/id_rsa.pub | |
# → git clone $PROJECT_REPO | |
# → git remote add deployment-server deployer@server | |
# → git push deployment-server master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment