Skip to content

Commit a7b06c4

Browse files
author
Martin Szymanski
committed
Merge branch 'release/0.6.3'
2 parents 0f0f66e + 88e3e2c commit a7b06c4

File tree

5 files changed

+149
-8
lines changed

5 files changed

+149
-8
lines changed

.env.example

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ DEPLOY_BRANCH=""
77
# Server root to project – required
88
DEPLOY_ROOT=""
99

10-
# Assets folder – required
11-
DEPLOY_ASSETS_DIR="uploads"
10+
# Public URL (used to curl deploy clear opcache script) – required
11+
DEPLOY_URL="https://my-site.com"
1212

13-
# Restart PHP if symlinks are cached – optional
13+
# Clear opcache to clear symlink cache. Set to 0 if not needed
14+
DEPLOY_CLEAR_OPCACHE=1
15+
16+
# Restart PHP if symlinks are cached and clearing opcache doesn't work. Leave empty if not needed
17+
# Restarting PHP may cause a short downtime on server – optional
1418
DEPLOY_RESTART_PHP=""
1519

1620
# Releases to keep

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
Things change, people change, everything changes.
44

5+
## [0.6.3](https://github.com/elfacht/craft-deploy/compare/0.6.2...0.6.3) - 2019-09-30
6+
### Added
7+
- Added [rollback.sh](rollback.sh) for rollbacks.
8+
- Added `DEPLOY_URL` option.
9+
- Added `DEPLOY_CLEAR_OPCACHE` option.
10+
- Added function to clear `opcache` to reset symlink cache without restarting PHP.
11+
512
## [0.6.2](https://github.com/elfacht/craft-deploy/compare/0.6.1...0.6.2) - 2019-07-22
613
### Added
714
- Added `DEPLOY_KEEP_RELEASES` constant.

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ Creates the necessary `releases`, `shared` and `shared/web` folders on the serve
4747
- Deletes oldest backup and keeps max. `[DEPLOY_KEEP_BACKUPS]` backups.
4848
- Restarts PHP to delete symlink cache (optional)
4949

50-
### gitlab-webhook-push.php
50+
### rollback.sh
5151

52-
Optional webhook script to run the bash scripts and creates a logfile. You can use your own scripts of course.
52+
Creates a symlink from `current/` to the second newest release folder (i.e. the former release), deletes the current release folder afterwards and runs `composer install`, `./craft migrate/all` and `./craft project-config/sync`. That's all. No further database actions.
5353

54-
## What does it not do?
54+
**If you run a major update with significant database migrations you do it on your own responsibility!**
5555

56-
This script doesn't do rollbacks. Although it doesn't re-link the `current` folder when the script throws an error. If an error occurs the previous release will still be the current one.
56+
### gitlab-webhook-push.php
57+
58+
Optional webhook script to run the bash scripts and creates a logfile. You can use your own scripts of course.
5759

5860
## Why should I use it?
5961

deploy.sh

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ read_var() {
3535
GIT_REPO=$(read_var DEPLOY_REPO .env)
3636
GIT_BRANCH=$(read_var DEPLOY_BRANCH .env)
3737
ROOT_PATH=$(read_var DEPLOY_ROOT .env)
38+
ROOT_URL=$(read_var DEPLOY_URL .env)
3839
ASSETS_DIR=$(read_var DEPLOY_ASSETS_DIR .env)
40+
CLEAR_OPCACHE=$(read_var DEPLOY_CLEAR_OPCACHE .env)
3941
RESTART_PHP=$(read_var DEPLOY_RESTART_PHP .env)
4042
KEEP_RELEASES=$(read_var DEPLOY_KEEP_RELEASES .env)
4143
KEEP_BACKUPS=$(read_var DEPLOY_KEEP_BACKUPS .env)
@@ -194,10 +196,34 @@ if composer install --no-interaction --prefer-dist --optimize-autoloader; then
194196
printf -- ' DONE!\n';
195197
fi
196198

199+
#######################################
200+
# Clear opcache
201+
#######################################
202+
if [[ $CLEAR_OPCACHE -eq 1 ]]; then
203+
printf -- "- Clear opcache .."
204+
205+
DONE=0;
206+
while [ $DONE -eq 0 ]; do
207+
WEBDIR=${ROOT_PATH}/current/web/
208+
RANDOM_NAME=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)
209+
echo "<?php opcache_reset(); ?>" > ${WEBDIR}${RANDOM_NAME}.php
210+
curl ${ROOT_URL}/${RANDOM_NAME}.php
211+
rm ${WEBDIR}${RANDOM_NAME}.php
212+
213+
if [ "$?" = "0" ]; then DONE=1; fi;
214+
printf -- '.';
215+
sleep 1;
216+
done
217+
218+
printf -- ' DONE!\n';
219+
fi
220+
197221
#######################################
198222
# Restart PHP
199223
#######################################
200-
if ${RESTART_PHP}; then
224+
if [[ -z $RESTART_PHP ]]; then
225+
echo ""
226+
else
201227
printf -- "- Restart PHP .."
202228

203229
DONE=0;

rollback.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/bash
2+
#
3+
# Rollback script for the Craft CMS deployment script on
4+
# staging/production servers.
5+
# @see https://github.com/elfacht/craft-deploy
6+
#
7+
# - Rollback to second newest release folder.
8+
# - This script Will not to any database work!
9+
#
10+
# @author
11+
# Martin Szymanski <martin@elfacht.com>
12+
# https://www.elfacht.com
13+
# https://github.com/elfacht
14+
#
15+
# @license MIT
16+
17+
#######################################
18+
# Exit if any command fails
19+
#######################################
20+
set -e
21+
22+
#######################################
23+
# Get constants from .env file:
24+
# - Server root path to project
25+
# - Restart PHP task (if symlinks are cached)
26+
#######################################
27+
read_var() {
28+
VAR=$(grep $1 $2 | xargs)
29+
IFS="=" read -ra VAR <<< "$VAR"
30+
echo ${VAR[1]}
31+
}
32+
33+
ROOT_PATH=$(read_var DEPLOY_ROOT .env)
34+
RESTART_PHP=$(read_var DEPLOY_RESTART_PHP .env)
35+
36+
#######################################
37+
# Get the current release folder
38+
#######################################
39+
CURRENT_RELEASE=$(ls -td $ROOT_PATH/releases/* | head -1 | tail -n 1)
40+
41+
#######################################
42+
# Get the second newest folder
43+
#######################################
44+
LAST_STABLE=$(ls -td $ROOT_PATH/releases/* | head -2 | tail -n 1)
45+
46+
#######################################
47+
# Symlink current release
48+
#######################################
49+
cd $ROOT_PATH
50+
printf -- "Symlink current release to $LAST_STABLE .."
51+
DONE=0;
52+
while [ $DONE -eq 0 ]; do
53+
ln -sfn $LAST_STABLE current
54+
55+
if [ "$?" = "0" ]; then DONE=1; fi;
56+
printf -- '.';
57+
sleep 1;
58+
done
59+
printf -- ' DONE!\n';
60+
61+
#######################################
62+
# Delete former release folder
63+
#######################################
64+
cd $ROOT_PATH
65+
printf -- "Delete former release folder $CURRENT_RELEASE .."
66+
DONE=0;
67+
while [ $DONE -eq 0 ]; do
68+
rm -rf $CURRENT_RELEASE
69+
70+
if [ "$?" = "0" ]; then DONE=1; fi;
71+
printf -- '.';
72+
sleep 1;
73+
done
74+
printf -- ' DONE!\n';
75+
76+
#######################################
77+
# Run composer and Craft scripts
78+
#######################################
79+
cd $ROOT_PATH/current
80+
if composer install --no-interaction --prefer-dist --optimize-autoloader; then
81+
php craft migrate/all
82+
php craft project-config/sync
83+
cd $ROOT_PATH
84+
fi
85+
86+
#######################################
87+
# Restart PHP
88+
#######################################
89+
if ${RESTART_PHP}; then
90+
printf -- "- Restart PHP .."
91+
92+
DONE=0;
93+
while [ $DONE -eq 0 ]; do
94+
${RESTART_PHP}
95+
96+
if [ "$?" = "0" ]; then DONE=1; fi;
97+
printf -- '.';
98+
sleep 1;
99+
done
100+
101+
printf -- ' DONE!\n';
102+
fi

0 commit comments

Comments
 (0)