2019-12-07 19:00:31 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Set up environment
|
|
|
|
source ~/bin/philomena-env
|
|
|
|
|
|
|
|
read oldrev newrev ref
|
|
|
|
echo "Updating $oldrev -> $newrev ($ref)"
|
|
|
|
|
|
|
|
# Clear variable set to '.' so git commands don't complain
|
|
|
|
unset GIT_DIR
|
|
|
|
|
2019-12-07 19:04:44 +01:00
|
|
|
cd ~/philomena
|
2019-12-07 19:04:04 +01:00
|
|
|
|
2019-12-07 19:00:31 +01:00
|
|
|
die() {
|
|
|
|
echo "$*" 1>&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if git diff --name-only $oldrev $newrev | grep "^mix.exs"; then
|
|
|
|
echo "Fetching deps"
|
|
|
|
mix deps.get || die "mix failed to update"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Run migrations
|
|
|
|
if git diff --name-only $oldrev $newrev | grep "^priv/repo/migrations"; then
|
|
|
|
echo "Running database migrations"
|
|
|
|
mix ecto.migrate || die "ecto.migrate failed"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Compile assets
|
|
|
|
if git diff --name-only $oldrev $newrev | grep "^assets/"; then
|
|
|
|
echo "Compiling assets"
|
2020-03-26 18:25:22 +01:00
|
|
|
npm install --prefix ./assets || die "assets install failed"
|
2019-12-07 19:00:31 +01:00
|
|
|
npm run deploy --prefix ./assets
|
|
|
|
mix phx.digest || die "assets compile failed"
|
|
|
|
fi
|
|
|
|
|
2019-12-07 19:58:45 +01:00
|
|
|
# TODO: fix this when I can figure out how to avoid recompiling
|
|
|
|
# the entire project when the version changes
|
|
|
|
#
|
|
|
|
# # Generate release name to always be the current timestamp so that
|
|
|
|
# # it will be considered an upgrade
|
|
|
|
# export PHILOMENA_VERSION="0.1.$(date +%s)"
|
2019-12-07 19:20:41 +01:00
|
|
|
|
2019-12-07 19:58:45 +01:00
|
|
|
echo "Building release"
|
|
|
|
mix distillery.release --quiet || die "failed to generate release"
|
2019-12-07 19:00:31 +01:00
|
|
|
|
2020-04-07 07:28:22 +02:00
|
|
|
# Find which instance is already started, if either
|
|
|
|
lsof -i :4000
|
|
|
|
BLUE_STARTED=$?
|
|
|
|
|
|
|
|
lsof -i :4001
|
|
|
|
GREEN_STARTED=$1
|
|
|
|
|
|
|
|
if [ $BLUE_STARTED -eq 1 -a $GREEN_STARTED -eq 1 ]; then
|
|
|
|
die "both blue and green instances are running; is another deploy in progress?"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $BLUE_STARTED -eq 0 -a $GREEN_STARTED -eq 0 ]; then
|
|
|
|
echo "app not started, starting it now"
|
|
|
|
NODENAME=philomena_0 PORT=4000 _build/prod/rel/philomena/bin/philomena start
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $BLUE_STARTED -eq 1 -a $GREEN_STARTED -eq 0 ]; then
|
|
|
|
echo "rolling blue (4000) over to green (4001)"
|
|
|
|
BLUE_PID=$(lsof -Fp -i :4000 | head -n1 | sed 's/^p//')
|
|
|
|
NODENAME=philomena_1 PORT=4001 _build/prod/rel/philomena/bin/philomena start
|
|
|
|
kill -TERM $BLUE_PID
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $BLUE_STARTED -eq 0 -a $GREEN_STARTED -eq 1 ]; then
|
|
|
|
echo "rolling green (4001) over to blue (4000)"
|
|
|
|
GREEN_PID=$(lsof -Fp -i :4001 | head -n1 | sed 's/^p//')
|
|
|
|
NODENAME=philomena_0 PORT=4000 _build/prod/rel/philomena/bin/philomena start
|
|
|
|
kill -TERM $GREEN_PID
|
|
|
|
fi
|