Skip to content

Instantly share code, notes, and snippets.

@jeremyjs
Last active August 2, 2017 08:35
Show Gist options
  • Save jeremyjs/40a95359dd9a490b7139d1ac1e64e24f to your computer and use it in GitHub Desktop.
Save jeremyjs/40a95359dd9a490b7139d1ac1e64e24f to your computer and use it in GitHub Desktop.
bin/
#!/bin/bash
set -eo pipefail
# TODO: check and perform stash if needed
# TODO: save current branch and return after release
# git add -A
# git stash
LATEST_TAG="$(git describe --tags `git rev-list --tags --max-count=1`)"
DEPLOY_TAG=${1:-$LATEST_TAG}
# TODO: use command line args and flags
DEPLOY_ENVIRONMENT="${DEPLOY_ENVIRONMENT:-production}"
PRODUCTION_URL="example.com"
STAGING_URL="staging.example.com"
APP_USER="root"
APP_NAME="example-app"
if [ $ENVIRONMENT = "production" ]; then
REMOTE_URL=$PRODUCTION_URL
elif [ $ENVIRONMENT = "staging" ]; then
REMOTE_URL="$STAGING_URL"
else
echo -e "\nERROR: unknown environment \"$ENVIRONMENT\"\n"
exit 1
fi
echo -e "deploying $DEPLOY_TAG to $REMOTE_URL\n"
echo "updating git remote refs and checking out master..."
git remote update -p
git checkout master
echo "pushing tags to git remote..."
git push --tags
echo "checking out deploy tag $DEPLOY_TAG..."
git checkout $DEPLOY_TAG
echo
echo -e "\nbuilding application bundle..."
rm -rf /tmp/$APP_NAME.tar.gz
meteor build /tmp --architecture os.linux.x86_64
echo -e "copying bundle to remote host $REMOTE_URL...\n"
scp /tmp/$APP_NAME.tar.gz $APP_USER@$REMOTE_URL:/tmp/$APP_NAME.tar.gz
rm -rf /tmp/$APP_NAME.tar.gz
echo -e "deploying to remote host...\n"
ssh -T $APP_USER@$REMOTE_URL 'bash -s' << EOF
rm -rf /tmp/bundle
cd /tmp/
tar -xzf $APP_NAME.tar.gz
sudo cp -rf /tmp/bundle/* /var/www/$APP_NAME/
cd /var/www/$APP_NAME/programs/server && sudo npm install
pm2 restart $APP_NAME
EOF
git checkout master
# git stash pop
# git reset HEAD
#!/usr/bin/env node --harmony
const git = require('simple-git')();
const program = require('commander');
const semver = require('semver');
const shell = require('shelljs');
program
.usage('-- [-d <env>] [-l <major | minor | patch>] [-t <custom tag name>] [-m <message>]')
.description('perform a git tag of the current master branch with a version bump and push')
.option('-d, --deploy', 'also deploy <env> to this version, defaulting to production')
.option('-l, --level <level>', 'level to bump [major | minor | patch]', /^(major|minor|patch)$/i)
.option('-t, --tag-name', 'custom tag name')
.option('-m, --message', 'the release tag annotation')
.parse(process.argv);
if (program.deploy) {
console.warn('Warning: `--deploy <env>` not implemented, defaulting to `--deploy production`');
console.warn('Warning: `--deploy` flag suppresses logging');
}
const nextVersionTag = (latestTag, level) => {
const latestVersion = semver.clean(latestTag);
const newVersion = semver.inc(latestVersion, level);
return `v${newVersion}`;
};
git .checkout('master')
.exec(() => {
shell.exec('git remote update -p');
})
.pull('origin/master', { '--ff-only': null })
.push('origin', 'master')
.tags((err, tags) => {
// TODO: exit if the latest tag is even with origin/master
const latestTag = tags.latest;
const level = program.level || 'patch'
const newTag = program.tagName || nextVersionTag(latestTag, level);
const tagMessage = program.message || '';
console.log(`Performing ${level} version bump from ${latestTag} to ${newTag}`);
if (tagMessage) console.log(tagMessage);
git .addAnnotatedTag(newTag, tagMessage)
.pushTags('origin')
.exec(() => {
// TODO: don't suppress logging
if (program.deploy) shell.exec('npm run deploy');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment