This repository was archived by the owner on Oct 20, 2025. It is now read-only.
forked from vierbergenlars/authserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·151 lines (145 loc) · 6.9 KB
/
deploy.sh
File metadata and controls
executable file
·151 lines (145 loc) · 6.9 KB
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
#!/bin/bash
set -e # Quit the script on error
cd "$( dirname "${BASH_SOURCE[0]}" )" # cd to the directory containg the script
trap '[[ -e "maintenance" ]] && printf "\x1b[30;43mMaintenance mode is still enabled\x1b[0m\n"' EXIT
ARGS=$(getopt -o nse:luh --long no-pull,shell,env:,skip-maintenance,lock-maintenance,unlock-maintenance,help,commit:,revert -n "deploy.sh" -- "$@")
no_pull=0
shell=0
skip_maintenance=0
target_commit="origin/HEAD"
[[ -z "$SYMFONY_ENV" ]] && [[ -e ".staging" ]] && export SYMFONY_ENV="staging" # If no environment is set and staging flag exists, use staging
[[ -z "$SYMFONY_ENV" ]] && export SYMFONY_ENV="prod" # If no environment is set, use production
eval set -- "$ARGS"
show_help() { cat <<EOL
deploy.sh - A simple application deployment tool
Usage:
bash deploy.sh --[un]lock-maintenance
bash deploy.sh --shell [--env <env>] [--skip-maintenance]
bash deploy.sh [--no-pull] [--env <env>] [--revert|--commit <commit>] [--skip-maintenance]
Options:
-n, --no-pull Do not pull a new version from the git repository, re-deploy the currently checked out version.
-s, --shell Start a new maintenance shell, with the right environment set up.
-e <env>, --env <env> Change the default symfony environment to this one.
-l, --lock-maintenance Enable maintenance mode, and do not disable it until the unlock command is given.
-u, --unlock-maintenance Disable maintenance mode.
--skip-maintenance Perform an action without enabling maintenance mode.
--commit <commit> Switches application to this commit. Defaults to origin/HEAD
--revert Reverts the application to the previous version. (sets --commit HEAD@{1} --no-pull)
When invoked without any parameters, it executes the following sequence:
* Set up deployment environment
* Enable maintenance mode
* Fetches changes from the remote repository
* Check out the requested commit
* Run application update scripts
* Disable maintenance mode
The deployment environment can be overridden with --env <environment>;
Pulling the latest version can be disabled with --no-pull;
Enabling and disabling maintenance mode can be skipped with --skip-maintenance.
The application can be forced in maintenance mode without deployment with --lock-maintenance;
this maintenance must be disabled with --unlock-maintenance, as it will not be disabled by a simple deploy.
Finally, --shell can be used to get a shell in maintenance mode that automatically disables maintenance on exit,
or together with --skip-maintenance to only set up the right environment variables for a deployment.
EOL
}
while true; do
case "$1" in
-n|--no-pull)
no_pull=1
shift
;;
-s|--shell)
shell=1
shift
;;
-e|--env)
export SYMFONY_ENV="$2"
shift 2
;;
--commit)
[[ "$target_commit" != "origin/HEAD" ]] && (echo "--commit and --revert cannot be used together" && show_help && exit 1)
target_commit=$(git rev-parse --revs-only "$2" --)
shift 2
;;
--revert)
[[ "$target_commit" != "origin/HEAD" ]] && (echo "--commit and --revert cannot be used together" && show_help && exit 1)
target_commit=$(git rev-parse --revs-only 'HEAD@{1}' --)
no_pull=1
shift
;;
--skip-maintenance)
skip_maintenance=1
shift
;;
-l|--lock-maintenance)
touch maintenance.lock
cp _maintenance.html maintenance
echo "Maintenance mode locked"
exit
;;
-u|--unlock-maintenance)
rm -f maintenance.lock maintenance
echo "Maintenance mode unlocked"
exit
;;
--)
shift;
break;
;;
-h|--help)
show_help
exit
break
;;
*)
echo "Unknown flag $1."
echo ""
show_help
exit 1
;;
esac
done
if [[ ! -e "maintenance" ]] && [[ skip_maintenance -eq 0 ]]
then
cp _maintenance.html maintenance && echo "Maintenance mode enabled"
fi
PS1="\$(basename \$(pwd))[$SYMFONY_ENV]"
[[ -e "maintenance" ]] && PS1="$PS1(maint)"
export PS1="$PS1\$ "
if [[ shell -ne 0 ]]
then
bash --norc
else
original_commit=$(git rev-parse HEAD)
[[ no_pull -eq 0 ]] && git fetch
removed_migrations=$(git diff --name-status -R "$target_commit" -- app/DoctrineMigrations | grep ^D | cut -f2 | sed -r 's/^.*\/Version([0-9]+)\.php$/\1/')
if [[ -n ${removed_migrations} ]] # There are migrations that are removed when going to this version
then
earliest_migration=$(echo "$removed_migrations" | sort -n | head -1) # Find the earlies migration to revert
prev_migration=$(ls app/DoctrineMigrations/Version*.php | sed -r 's/^.*\/Version([0-9]+)\.php$/\1/' | sort | grep -C1 "$earliest_migration" | head -1) # And find the version before that
printf "\x1b[37;41mWARNING:\x1b[0m Migrations \x1b[33m$earliest_migration\x1b[0m and later will be removed when moving to $target_commit.\n"
php app/console doctrine:migrations:migrate --dry-run "$prev_migration"
printf "\x1b[37;41mDANGER!\x1b[0m You are about to \x1b[32mdowngrade\x1b[0m the database to version \x1b[33m$prev_migration\x1b[0m\n"
read -p "WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)" cont
[[ "$cont" != "y" && "$cont" != "Y" ]] && (printf "\x1b[37;41mMigration cancelled\x1b[0m\n"; exit 1)
php app/console doctrine:migrations:migrate "$prev_migration" -n
fi
git checkout "$target_commit"
composer install --no-dev --optimize-autoloader
npm install
rm -rf app/cache/$SYMFONY_ENV/* # Prevent class not found errors during cache clear
php app/console cache:clear
php app/console assets:install
php app/console assetic:dump
php app/console braincrafted:bootstrap:install
# Only execute migrations when there are new migrations available.
php app/console doctrine:migrations:status | grep "New Migrations:" | cut -d: -f2 |grep "^ *0" > /dev/null || \
php app/console doctrine:migrations:migrate
if [[ "$original_commit" == $(git rev-parse "$target_commit") ]]
then
printf "\x1b[30;42mRe-deploy finished\x1b[0m \x1b[30:46m$(git rev-parse --short "$original_commit")\x1b[0m\n"
else
printf "\x1b[30;42mDeploy finished\x1b[0m \x1b[30;46m$(git rev-parse --short "$original_commit")\x1b[0m -> \x1b[30;46m$(git rev-parse --short HEAD)\x1b[0m\n"
fi
fi
[[ -e "maintenance.lock" ]] && echo "Warning: maintenance mode is locked. Not disabling maintenance mode"
[[ ! -e "maintenance.lock" ]] && [[ skip_maintenance -eq 0 ]] && rm maintenance && echo "Maintenance mode disabled"