Atomic Node.js Deployment in Buddy
if [ -d "releases/${execution.to_revision.revision}" ] && [ "${execution.refresh}" = "true" ]; then echo "Removing: releases/${execution.to_revision.revision}" && rm -rf releases/${execution.to_revision.revision}; fi
if [ ! -d "releases/${execution.to_revision.revision}" ]; then echo "Creating: releases/${execution.to_revision.revision}" && cp -dR deploy-cache releases/${execution.to_revision.revision}; fi
echo "Checking if packages need to be updated"
cmp --silent ./releases/${execution.to_revision.revision}/package.json ./current/package.json || INSTALL_PACKAGES=true
if [ $INSTALL_PACKAGES == true ]; then cp -f ./releases/${execution.to_revision.revision}/package.json .; fi
if [ $INSTALL_PACKAGES == true ]; then echo "Installing packages" && npm install --production; fi
echo "Linking revision packages to cached packages" && cd ./releases/${execution.to_revision.revision} && ln -sfn /var/www/api/node_modules .
echo "Linking current to revision: ${execution.to_revision.revision}"
rm -f current
ln -s releases/${execution.to_revision.revision} current
echo "Restarting application"
pm2 startOrRestart current/ecosystem.config.js --env production
echo "Removing old releases"
cd releases && ls -t | tail -n +11 | xargs rm -rf