|
#!/bin/bash |
|
|
|
# exit when any command fails |
|
set -e |
|
|
|
HIGHLIGHT='\e[97m\e[40m\e[1m' |
|
NC='\033[39m\033[49m\e[0m' # Normal |
|
|
|
script=$(realpath "$0") |
|
repo="$1" |
|
environment="$2" |
|
commit="$3" |
|
home="$(dirname "$script")/$environment" |
|
user=deploy |
|
repos="$home/repos" |
|
keep=3 |
|
gitDepth=5 |
|
|
|
mkdir -p "$repos" |
|
cd $home |
|
|
|
usage() { echo -e "Usage:\n\t${HIGHLIGHT}$0 git@git.domain.de/group/repo.git environment-folder full-commit-sha${NC}\n"; } |
|
say() { echo -e "\n-->> ${HIGHLIGHT}$1${NC}"; } |
|
|
|
if [ `whoami` != $user ]; then |
|
echo "Script must be run as user $user."; exit 1 |
|
fi |
|
|
|
if [ "$repo" == "" ]; then |
|
usage; echo "No git repo given."; exit 1 |
|
fi |
|
|
|
if [ "$environment" == "" ]; then |
|
usage; echo "No environment folder given."; exit 1 |
|
fi |
|
|
|
if [ "$commit" == "" ]; then |
|
usage; commit="HEAD"; echo "No commit given. To fetch latest use 'HEAD'"; exit 1 |
|
fi |
|
|
|
if ! command -v php &> /dev/null; then |
|
echo "Php could not be found."; exit 1 |
|
fi |
|
|
|
if ! command -v composer &> /dev/null; then |
|
echo "Composer could not be found."; exit 1 |
|
fi |
|
|
|
|
|
clone="$repos/${commit:0:8}" |
|
echo -e "Will use git at\t\t$repo" |
|
echo -e "Will use commit\t\t$commit" |
|
echo -e "Home is set to\t\t$home" |
|
echo -e "Will cone to\t\t$clone" |
|
|
|
### Fetching from git |
|
# Clone the repo |
|
if [ ! -d "$clone" ]; then |
|
say "Will now clone to $clone:" |
|
git clone --depth $gitDepth --recurse-submodules "$repo" "$clone" |
|
else |
|
say "Using existing repo at "$clone"" |
|
fi |
|
|
|
# Set the desired commit |
|
# Notice: git reset will fail with --git-dir and instead clone to current dir (pwd) |
|
say "Hard-reset git at $clone:" |
|
cd "$clone" |
|
git reset --hard $commit |
|
cd "$home" |
|
|
|
### Preparing application |
|
# Install Laravel & requirements |
|
say "Installing Laravel and dependencies:" |
|
composer install --working-dir "$clone" --no-dev |
|
composer dumpautoload --working-dir "$clone" |
|
# Comment out if you don't have a post-deploy script defined |
|
composer run-script post-deploy --working-dir "$clone" |
|
|
|
# Remove link storage and .env to existing |
|
say "Removing freshly-cloned storage folder from git, and linking it and .env to global:" |
|
rm -rf "$clone/storage" |
|
ln -nfsv "$PWD/storage" "$clone/storage" |
|
ln -nfsv "$PWD/.env" "$clone/.env" |
|
|
|
# Remove caches |
|
say "Renewing Laravel caches:" |
|
php "$clone/artisan" opcache:clear |
|
php "$clone/artisan" config:cache |
|
php "$clone/artisan" route:cache |
|
sudo -u www-data php "$clone/artisan" cache:clear |
|
# Will make sure FPM looses its cache |
|
sudo /usr/sbin/service php*-fpm restart |
|
|
|
# Give www-data permission to the newly generated caches |
|
chgrp -R www-data "$clone/bootstrap/" |
|
|
|
### Migrate database if required |
|
say "Migrating database if required:" |
|
php "$clone/artisan" migrate --force |
|
|
|
|
|
### Bring the app to live |
|
ln -nfs "$clone" "$home/live" |
|
|
|
# Restart queue worker |
|
sudo /usr/bin/supervisorctl restart "laravel-worker-$environment:*" |
|
|
|
# Remove any but the last 3 cloned repos |
|
say "Will now purge any but the last $keep versions:" |
|
ls -tp $repos | tail -n +$(($keep+1)) | xargs -I {} rm -rf $repos/{} |
|
|
|
say "Process complete. New application from $clone is now live." |