-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy
executable file
·173 lines (125 loc) · 4.74 KB
/
deploy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env bash
#---------------------------------------------------------------------
usage ()
{
cat <<EOT
${0##*/}
Designed to perform all the necessary steps to update a local
copy of the project from the remote repo, which is assumed to
be "ahead" of the local copy. Mainly intended for use in
staging and production environments to ensure that code "pushes"
are performed consistently every time. Should be run from the
project root folder.
Usage:
bin/${0##*/} [tag|branch|sha|ref] [dev|prod|staging]
\$1 = The new commit to which to update.
Will be merged into the current branch/commit.
Default: (The remote tracking branch for the
currently-checked-out branch.)
\$2 = The local config file "environment" to use.
Default: $APP_ENV if set, 'prod' if not.
Environment Variables:
APP_ENV = If set and \$2 is blank, will be used as the active
environment for the update run.
LOADSYS_EMAIL = If set, an email report for the update run will be
generated and sent to the email address contained within.
EOT
exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
}
if [ "$1" = '-h' ]; then
usage
fi
echo "## Starting update process."
# Set variables.
DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )"
BIN_DIR="$DIR/bin"
CONFIG_DIR="$DIR/config"
MIGRATION_DIR="$CONFIG_DIR/Migrations"
SLEEP_TIME=10
EXISTINGCOMMIT=$( git rev-parse HEAD )
EXISTINGBRANCH=$( git rev-parse --abbrev-ref HEAD )
if [ -d "$MIGRATION_DIR" ] && [ "$(ls -A $MIGRATION_DIR)" ]; then
DB_UPDATES_METHOD='migrations'
else
DB_UPDATES_METHOD='none'
fi
# Input processing.
if [ $1 ]; then
ARG_NEWCOMMIT=$1
else
#Ref: http://stackoverflow.com/a/9753364/70876
ARG_NEWCOMMIT=$( git rev-parse --abbrev-ref --symbolic-full-name @{u} )
fi
if [ $2 ]; then
ARG_APPENV="$2"
elif [ -n "$APP_ENV" ]; then
ARG_APPENV="$APP_ENV"
else
ARG_APPENV="prod"
fi
#@TODO: Maybe check for an executable `scripts/pre-deploy` project-specific script and run it? Abort this script if the pre-deploy exists non-zero?
# Check for local changes that would cause a merge (without -f) to fail later.
$BIN_DIR/git-localchanges
if [ $? -gt 0 ]; then
echo "!! You will need to resolve any differences between the working copy !!"
echo "!! and the active commit before proceeding with the update. !!"
echo ""
exit 1
fi
# Backup the live DB.
$BIN_DIR/db-backup
echo ''
echo "## Sleeping for ${SLEEP_TIME} seconds to consider the backup size."
echo "## (Press Ctrl-C to abort the update)..."
echo "## (Press ENTER to continue immediately)..."
echo ''
read -t ${SLEEP_TIME} NOTUSED
# Save the "current" commit for later.
echo "## Current commit is $EXISTINGBRANCH ($EXISTINGCOMMIT)";
# Update repository.
echo "## Pulling changes from remote repo.";
git fetch origin refs/heads/*:refs/remotes/origin/* +refs/tags/*:refs/tags/*
# Apply database updates, one way or another.
if [ "$DB_UPDATES_METHOD" = 'migrations' ]; then
echo "## Deferring database migrations until after merge."
elif [ "$DB_UPDATES_METHOD" = 'none' ]; then
echo "## Could not automatically determine database change tracking system."
read -p "## Make sure any pending DB changes have been made and press [ENTER] to continue."
fi
# This if() should really be looking to see if we're being asked to fast
# forward on the same branch, or switch to the HEAD of a different branch.
# if [ "$ARG_NEWCOMMIT" = "origin/master" ]; then
echo "## Merging upstream changes from: $ARG_NEWCOMMIT";
git merge $ARG_NEWCOMMIT
# else
# echo "## Checking out new commit: $ARG_NEWCOMMIT";
# git checkout $ARG_NEWCOMMIT
# fi
echo "## New commit is now active:";
GIT_LOG=$( git log -1 )
echo "${GIT_LOG}"
if [ "$DB_UPDATES_METHOD" = 'migrations' ]; then
echo "## Applying database migrations."
$BIN_DIR/migrations
fi
# Make sure any external packages are installed and available.
# The current environment can export a COMPOSER_NODEV flag to disable
# the installation of require-dev dependencies.
composer install --no-interaction
# Purge cache folders.
$BIN_DIR/cache-clear
# Send an email report if the ENV var is set in Config/env_vars.txt
if [ -n "${LOADSYS_EMAIL}" ]; then
echo "## Generating email report."
MSG_BODY=$( mktemp -t "${0##*/}.email_report.XXXXXXXXXX" )
cat > "$MSG_BODY" <<-EOD
A code push has been completed for ${LOADSYS_PROJECT_NAME}.
(Previous commit: ${EXISTINGCOMMIT})
-------------------------------------------------------
${GIT_LOG}
EOD
bin/email "${LOADSYS_EMAIL}" "${LOADSYS_PROJECT_NAME}: Code push complete" "${MSG_BODY}"
rm -f "${MSG_BODY}"
fi
#@TODO: Maybe check for an executable `scripts/post-deploy` project-specific script and run it?
echo "## Update process complete.";