Skip to content

Instantly share code, notes, and snippets.

@rajeshisnepali
Created June 12, 2019 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajeshisnepali/dc6cf9e7f62c6347bfcce1c0a46e2f65 to your computer and use it in GitHub Desktop.
Save rajeshisnepali/dc6cf9e7f62c6347bfcce1c0a46e2f65 to your computer and use it in GitHub Desktop.
Zero Downtime deploy project to server (production)
@servers(['production' => 'ubuntu@X.X.X.X'])
@setup
$repo = 'git@gitlab.com:<username>/<project-name>.git';
date_default_timezone_set('Asia/Kathmandu');
$release = date('YmdHis');
$releases_dir = '/var/www/html/final/releases';
$app_dir = '/var/www/html/final';
$new_release_dir = $releases_dir .'/'. $release;
$env = $app_dir . '/.env';
$storage = $app_dir . '/storage';
@endsetup
@story('deploy')
clone_repository
run_composer
update_symlinks
migrate
live
permissions
clean_old_releases
@endstory
@task('clone_repository')
echo 'Cloning Repository'
git clone --depth 1 -b {{ $branch }} {{ $repo }} {{ $new_release_dir }}
@endtask
@task('run_composer')
echo 'Installing composer dependencies';
cd {{ $new_release_dir }}
composer install --optimize-autoloader --no-dev
@endtask
@task('update_symlinks')
echo "Linking storage directory"
rm -rf {{ $new_release_dir }}/storage
ln -nfs {{ $storage }} {{ $new_release_dir }}/storage
echo 'Linking .env file'
ln -nfs {{ $env }} {{ $new_release_dir }}/.env
@endtask
@task('migrate')
echo 'Running migrations';
cd {{ $new_release_dir }};
php artisan migrate --force;
@endtask
@task('live')
echo 'Linking current release'
ln -nfs {{ $new_release_dir }} {{ $app_dir }}/current
@endtask
@task('permissions')
echo "Changing Permission ({{ $release }})"
sudo chown $USER:www-data -R bootstrap/cache storage public
sudo chmod 775 -R bootstrap/cache storage public
# sudo (chown | chmod) will only work if you enable NOPASSWD in /etc/sudoers for particular $USER.
@endtask
@task('clean_old_releases')
# This will list our releases by modification time and delete all but the 3 most recent.
# This will leave the two releases and delete old releases.
purging=$(ls -dt {{ $releases_dir }}/* | tail -n +3);
if [ "$purging" != "" ]; then
echo Purging old releases: $purging;
rm -rf $purging;
else
echo "No releases found for purging at this time";
fi
@endtask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment