Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1 update config and craft commands #2

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
# Git repo – required
DEPLOY_REPO=""

# Git branch – optional (default: master)
# Git branch – optional (default: your repo's default branch)
DEPLOY_BRANCH=""

# Server root to project – required
DEPLOY_ROOT=""

# Craft CMS folder – optional
# If Craft is installed in a subfolder, set the folder name here
DEPLOY_CRAFT_DIR=""

# Assets folder – required
DEPLOY_ASSETS_DIR="uploads"

# Public URL (used to curl deploy clear opcache script) – required
DEPLOY_URL="https://my-site.com"
DEPLOY_URL="https://example.com"

# Clear opcache to clear symlink cache. Set to 0 if not needed
DEPLOY_CLEAR_OPCACHE=1
# Clear opcache to clear symlink cache.
# 0 = disabled, 1 = enabled
DEPLOY_CLEAR_OPCACHE=0

# Restart PHP if symlinks are cached and clearing opcache doesn't work. Leave empty if not needed
# Restarting PHP may cause a short downtime on server – optional
# Restart PHP cmd if symlinks are cached and clearing opcache doesn't work – optional
# Leave empty if not needed
DEPLOY_RESTART_PHP=""

# Releases to keep
Expand Down
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ A bash script for zero-downtime Craft CMS deployment to run on production server
- Upload `web/.htaccess` into `shared/web/`.
- Upload `web/cpresources` folder into `shared/web/`.
- Upload your `[ASSETS_DIR]` folder and `web/cpresources` folder into `shared/web/`.
- Setup a [webhook](https://docs.gitlab.com/ee/user/project/integrations/webhooks.html) to call the scripts or run `./deploy.sh` manually.

## .gitignore

Expand Down Expand Up @@ -53,21 +52,18 @@ Creates a symlink from `current/` to the second newest release folder (i.e. the

**If you run a major update with significant database migrations you do it on your own responsibility!**

### gitlab-webhook-push.php

Optional webhook script to run the bash scripts and creates a logfile. You can use your own scripts of course.

## Why should I use it?

When you don't want to spend money on deployment services and tools like Capistrano are just too much to set up for smaller projects.

## Roadmap

- Set project folder name in .env
- ~~Add `.env` for better config handling.~~
- ~~Delete releases folder if an error occurs during deployment.~~
- ~~Delete not only the oldest release folder, but multiple release folders if there's more than 5 folders (occurs if an deployment fails).~~ (Corrupt folders will be removed if installation fails)
- ~~Integrate `update.sh` scripts into `deploy.sh` and/or create flags.~~
- [ ] Add DB rollback script
- [x] Set project folder name in .env
- [x] dd `.env` for better config handling.
- [x] Delete releases folder if an error occurs during deployment.
- [x] Delete not only the oldest release folder, but multiple release folders if there's more than 5 folders (occurs if an deployment fails). (Corrupt folders will be removed if installation fails)
- [x] Integrate `update.sh` scripts into `deploy.sh` and/or create flags.

## License

Expand Down
15 changes: 9 additions & 6 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ GIT_REPO=$(read_var DEPLOY_REPO .env)
GIT_BRANCH=$(read_var DEPLOY_BRANCH .env)
ROOT_PATH=$(read_var DEPLOY_ROOT .env)
ROOT_URL=$(read_var DEPLOY_URL .env)
CRAFT_DIR=$(read_var DEPLOY_CRAFT_DIR .env)
ASSETS_DIR=$(read_var DEPLOY_ASSETS_DIR .env)
CLEAR_OPCACHE=$(read_var DEPLOY_CLEAR_OPCACHE .env)
RESTART_PHP=$(read_var DEPLOY_RESTART_PHP .env)
Expand All @@ -57,6 +58,7 @@ fi
# Exit if any command fails
#######################################
set -e
trap 'echo "An error occurred on line $LINENO. Exiting." >&2; exit 1' ERR

#######################################
# Set timestamp as realease folder name.
Expand Down Expand Up @@ -87,7 +89,7 @@ CURRENT_RELEASE=$(timestamp)
# Backup database
#######################################
if [ -d "./current/" ]; then
php current/craft backup/db
php current/$CRAFT_DIR/craft db/backup
fi

#######################################
Expand All @@ -110,7 +112,7 @@ fi
# Run composer install and
# create symlinks
#######################################
if composer install --no-interaction --prefer-dist --optimize-autoloader; then
if cd "$CRAFT_DIR" && composer install --no-interaction --prefer-dist --optimize-autoloader; then
#######################################
# Create symlinks of shared files
# and folders.
Expand Down Expand Up @@ -153,7 +155,7 @@ if composer install --no-interaction --prefer-dist --optimize-autoloader; then
printf -- ' DONE!\n';

#######################################
# Kepp max. 5 releases,
# Keep max. $KEEP_RELEASES releases,
# delete old release directories
#######################################
COUNT=`/bin/ls -l $ROOT_PATH/releases | /usr/bin/wc -l`
Expand All @@ -176,7 +178,7 @@ if composer install --no-interaction --prefer-dist --optimize-autoloader; then
fi

#######################################
# Kepp max. X backups,
# Keep max. $KEEP_BACKUPS backups,
# delete oldest backup
#######################################
COUNT=`/bin/ls -l $ROOT_PATH/shared/storage/backups | /usr/bin/wc -l`
Expand All @@ -200,14 +202,15 @@ if composer install --no-interaction --prefer-dist --optimize-autoloader; then

#######################################
# Clear opcache
# This will only work if the SSL certs are working correctly on your systems.
#######################################
if [[ $CLEAR_OPCACHE -eq 1 ]]; then
printf -- "- Clear opcache .."

DONE=0;
while [ $DONE -eq 0 ]; do
WEBDIR=${ROOT_PATH}/current/web/
RANDOM_NAME=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)
WEBDIR=${ROOT_PATH}/current/$CRAFT_DIR/web/
RANDOM_NAME=$(LANG=C tr -dc A-Za-z0-9 </dev/urandom | head -c 13)
echo "<?php opcache_reset(); ?>" > ${WEBDIR}${RANDOM_NAME}.php
curl ${ROOT_URL}/${RANDOM_NAME}.php
rm ${WEBDIR}${RANDOM_NAME}.php
Expand Down
104 changes: 0 additions & 104 deletions gitlab-webhook-push.php

This file was deleted.