Skip to content

Instantly share code, notes, and snippets.

@altherlex
Last active May 4, 2022 22:18
Show Gist options
  • Save altherlex/9fae1636c7ca206e069e5575c4aad190 to your computer and use it in GitHub Desktop.
Save altherlex/9fae1636c7ca206e069e5575c4aad190 to your computer and use it in GitHub Desktop.
Prepare Rails app for EB deploy
#!/bin/bash -eu
#
# Usage:
# $ cd $app_repo
# $ $infra_repo/myhi/deploy/ebbundle
#
# TODO: if branch == production, store an extra copy of the artifact (with the assets) in S3
# TODO: lifecycle for this
APP_ROOT="$(git rev-parse --show-toplevel)"
INFRA_ROOT="$(cd "$(dirname "$0")" && git rev-parse --show-toplevel)"
echo "Bundling ${APP_ROOT} using the infra repo at ${INFRA_ROOT}..."
# confirm repo roots
test -d "${APP_ROOT}/config"
test -d "${INFRA_ROOT}/myhi/ebextensions"
cd "${APP_ROOT}"
# TODO: Create TAG - increment latest version
TAG=$(git tag -l --sort=-creatordate | head -n 1 | cut -d "-" -f 1) ruby -e "puts ENV['TAG'].next+'-'+Time.now.strftime('%Y.%m.%d')"
git tag -a $TAG -m "$TAG"
git push origin tags/$TAG
git co $TAG
BRANCH="$(git branch --show-current)"
# Gets TAG name if BRANCH is empty
[ ! -L $BRANCH ] || BRANCH=$(git describe --tags --abbrev=0 --exact-match)
BUNDLE_PATH="../${BRANCH}.zip"
symlink_config() {
[ ! -L /etc/webapp/"$1" ] && ln -sf /etc/webapp/"$1" config/"$1"
}
# DOC: fallback for MYnd (devs already have config links set up)
echo "Preparing /config files"
# Save development config files
[ ! -f config/database.yml ] || mv config/database.yml config/database.yml.tmp
[ ! -f config/secrets.yml ] || mv config/secrets.yml config/secrets.yml.tmp
[ ! -f config/reports_database.yml ] || mv config/reports_database.yml config/reports_database.yml.tmp
mv config/database.yml.example config/database.yml
mv config/secrets.yml.example config/secrets.yml
mv config/reports_database.yml.example config/reports_database.yml
# echo "Running: rails assets:precompile"
rails assets:precompile
# Copy myhi ebextensions from the infra repo
echo "Copying .ebextensions"
mkdir -p .ebextensions
rm -f .ebextensions/*
cp "${INFRA_ROOT}"/myhi/ebextensions/* .ebextensions/
# Ebextensions-generated config is accessed from the app deployment via these
# symlinks
symlink_config secrets.yml
symlink_config database.yml
symlink_config reports_database.yml
# Zip with excludes
# mkdir -p log ## && touch log/.keep
echo "Running: zip -qdg -FS -yr \"${BUNDLE_PATH}\" * .[^.]* \ -x'./.git/*' \ -x'./log/[^.]*' \ -x'*.DS_Store'"
zip -qdg -FS -yr "${BUNDLE_PATH}" * .[^.]* \
-x'./.git/*' \
-x'./log/[^.]*' \
-x'*.DS_Store' \
-x'config/database.yml.tmp' \
-x'config/secrets.yml.tmp' \
-x'config/reports_database.yml.tmp' \
&& du -sh "${BUNDLE_PATH}"
echo "Reverting /config changes"
cd "${APP_ROOT}"
# Return saved development config files
git checkout config/database.yml.example
git checkout config/secrets.yml.example
git checkout config/reports_database.yml.example
[ ! -f config/database.yml.tmp ] || mv config/database.yml.tmp config/database.yml
[ ! -f config/secrets.yml.tmp ] || mv config/secrets.yml.tmp config/secrets.yml
[ ! -f config/reports_database.yml.tmp ] || mv config/reports_database.yml.tmp config/reports_database.yml
echo "Done: ${INFRA_ROOT}/${BUNDLE_PATH}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment