Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Super simple git post-receive hook for Node.js + nvm + npm + node-foreman + init (Ubuntu) deployment
#!/usr/bin/env bash
set -u
set -e
export GIT_WORK_TREE="/var/www/example.com"
export NODE_VERSION="0.10"
echo "--> Checking out..."
git checkout -f
echo "--> Selecting Node version $NODE_VERSION"
. $HOME/.nvm/nvm.sh
nvm use $NODE_VERSION
echo "--> Installing libraries..."
cd "$GIT_WORK_TREE"
npm install
echo "--> Exporting Foreman files..."
node_modules/.bin/nf export -o /etc/init
echo "--> Restarting..."
stop foreman
start foreman
@tlrobinson

This comment has been minimized.

Copy link
Owner Author

@tlrobinson tlrobinson commented Dec 20, 2013

On the remote server

mkdir -p /var/www/example.com
mkdir example.git && cd example.git
git init --bare
cat > hooks/post-receive
chmod +x hooks/post-receive

Copy the hook in. Put to the repo

Based on http://toroid.org/ams/git-website-howto

@yyx990803

This comment has been minimized.

Copy link

@yyx990803 yyx990803 commented Jan 2, 2014

Checkout https://github.com/yyx990803/pod which handles these for you, plus using pm2 for process monitoring.

@narqo

This comment has been minimized.

Copy link

@narqo narqo commented Jan 4, 2014

Why use bare-repo with separate working tree instead of direct clone into www directory?

@jmcbee

This comment has been minimized.

Copy link

@jmcbee jmcbee commented Nov 24, 2014

@narqo ermeged, why not try using a bare and a non-bare repo and tell us why we use a bare repo.

@jasonnathan

This comment has been minimized.

Copy link

@jasonnathan jasonnathan commented Jul 8, 2015

@fbm-static rofl!

@chovy

This comment has been minimized.

Copy link

@chovy chovy commented Nov 26, 2015

I get nvm: command not found in my post-receive hook

it works fine from ssh login

@Ridder90

This comment has been minimized.

Copy link

@Ridder90 Ridder90 commented Mar 25, 2019

Thanks for this! Unfortunatly i get nvm and npm not found.

@williamjingo

This comment has been minimized.

Copy link

@williamjingo williamjingo commented Dec 13, 2019

Just incase anyone is still getting the npm not found.
after the exit immediately if a command exits with a non-zero status. set -e
add

export NVM_DIR="$HOME/.nvm"
. "$NVM_DIR/nvm.sh"

then you can go a head and set your variables.
export..
I hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.