Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Szymanski committed Jun 23, 2019
2 parents 604060b + fd0adb2 commit 2cc348b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 147 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

Things change, people change, everything changes.

## [0.4.0](https://github.com/elfacht/craft-deploy/compare/0.3.1...0.4.0) - 2019-06-22
### Added
- Added `err` timestamp.
### Changed
- Changed path in instructions step (README).
### Fixed
- Removed duplicate code.
- Fixed bash formatting.

## [0.3.1](https://github.com/elfacht/craft-deploy/compare/0.3.0...0.3.1) - 2019-06-20
### Added
- Added missing step to usage.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A bash script for zero-downtime Craft CMS deployment to run on production server
- Upload `.env` into `shared/`.
- Upload `storage` folder into `shared/`.
- Upload `web/.htaccess` into `shared/web/`.
- Upload `cpresources` folder 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.

Expand Down
157 changes: 11 additions & 146 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -1,141 +1,3 @@
#!/bin/bash
#
# Craft CMS deployment script on staging/production servers.
# @see https://github.com/elfacht/craft-deploy
#
# - Creating a release folder
# - Cloning the git repo.
# - Running `composer install`
# - Creating symlinks to shared folders and files.
# - Symlink on current folder to newest release.
# - Deleting old releases (keeping max. 5)
#
# @author
# Martin Szymanski <martin@elfacht.com>
# https://www.elfacht.com
# https://github.com/elfacht
#
# @license MIT

#######################################
# Set constants:
# - Git repo URL
# - Assets directory name (web/[ASSETS_DIR])
#######################################
GIT_REPO="[GIT_REPO_URL]"
ASSETS_DIR="[ASSETS_DIR]"


#######################################
# Set timestamp as realease folder name.
# Example:
# 201903271201
# Arguments:
# None
# Returns:
# Sting
#######################################
timestamp() {
date +"%Y%m%d%H%M%S"
}

#######################################
# Current release number
#######################################
CURRENT_RELEASE=$(timestamp)

#######################################
# Backup database
#######################################
if [ -d "./current/" ]
then
php current/craft backup/db
fi

#######################################
# Create release directory and
# clone git repo
#######################################
if [ -d "./releases/" ]
then
### Make current release dir
cd "./releases"
mkdir $CURRENT_RELEASE && cd "$_"
echo "Created release $CURRENT_RELEASE folder."

### Clone repo
git clone $GIT_REPO .
else
exit 1
fi

#######################################
# Run composer install and
# create symlinks
#######################################
if composer install
then
### Create symlinks
printf -- "- Create symlinks .."
DONE=0;
while [ $DONE -eq 0 ]; do
ln -sfn "../../shared/.env"
ln -sfn "../../shared/storage" .
cd web && ln -sfn "../../../shared/web/.htaccess"
ln -sfn "../../../shared/web/$ASSETS_DIR"
ln -sfn "../../../shared/web/cpresources"

if [ "$?" = "0" ]; then DONE=1; fi;
printf -- '.';
sleep 1;
done
printf -- ' DONE!\n';

### Symlink current release
cd "../../../"
printf -- "- Create release $CURRENT_RELEASE .."
DONE=0;
while [ $DONE -eq 0 ]; do
ln -sfn "releases/$CURRENT_RELEASE/" current

if [ "$?" = "0" ]; then DONE=1; fi;
printf -- '.';
sleep 1;
done
printf -- ' DONE!\n';

#######################################
# Kepp max. 5 releases,
# delete old release directories
#######################################
COUNT=`/bin/ls -l releases | /usr/bin/wc -l`
MINDIRS=6 # Keep 5 releases

if [[ $COUNT -gt $MINDIRS ]]
then
OLDEST_RELEASE=$(ls -tr releases/* | head -1)
printf -- "- Delete oldest release '$OLDEST_RELEASE' .."

DONE=0;
while [ $DONE -eq 0 ]; do
rm -rf $(find releases/*/ -maxdepth 1 -type d | sort -r | tail -n 1 | sed 's/[0-9]*\.[0-9]*\t//')

if [ "$?" = "0" ]; then DONE=1; fi;
printf -- '.';
sleep 1;
done

printf -- ' DONE!\n';
fi
else

#######################################
# Delete release folder if composer fails.
#######################################
cd "../"
rm -rf $CURRENT_RELEASE
fi

#!/bin/bash
#
# Craft CMS deployment script on staging/production servers.
Expand Down Expand Up @@ -180,6 +42,13 @@ timestamp() {
date +"%Y%m%d%H%M%S"
}

#######################################
# Error timestamp
#######################################
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
}

#######################################
# Current release number
#######################################
Expand All @@ -188,17 +57,15 @@ CURRENT_RELEASE=$(timestamp)
#######################################
# Backup database
#######################################
if [ -d "./current/" ]
then
if [ -d "./current/" ]; then
php current/craft backup/db
fi

#######################################
# Create release directory and
# clone git repo
#######################################
if [ -d "./releases/" ]
then
if [ -d "./releases/" ]; then
### Make current release dir
cd "./releases"
mkdir $CURRENT_RELEASE && cd "$_"
Expand All @@ -214,8 +81,7 @@ fi
# Run composer install and
# create symlinks
#######################################
if composer install
then
if composer install; then
#######################################
# Create symlinks of shared files
# and folders.
Expand Down Expand Up @@ -264,8 +130,7 @@ then
COUNT=`/bin/ls -l releases | /usr/bin/wc -l`
MINDIRS=6 # Keep 5 releases

if [[ $COUNT -gt $MINDIRS ]]
then
if [[ $COUNT -gt $MINDIRS ]]; then
OLDEST_RELEASE=$(ls -tr releases/* | head -1)
printf -- "- Delete oldest release '$OLDEST_RELEASE' .."

Expand Down

0 comments on commit 2cc348b

Please sign in to comment.