Last active
October 2, 2017 20:27
-
-
Save floptwo/fc6e73721b346a6b6d11634dde7e1762 to your computer and use it in GitHub Desktop.
envoy run deploy (no downtime)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@setup | |
$server = 'myserver'; | |
$repository_path = "~/git/myapp.git"; | |
$application_path = "~/webroot/myapp.com/www"; | |
$env = $env ?? 'production'; | |
$branch = $branch ?? 'master'; | |
$branch = ($env == 'production') ? 'production' : $branch; | |
$shared_dirs = [ | |
'storage/app/public', | |
'storage/framework/cache', | |
'storage/framework/sessions', | |
'storage/framework/views', | |
'storage/logs', | |
'public/assets', | |
]; | |
$linked_dirs = [ | |
'storage/app', | |
'storage/framework', | |
'storage/logs', | |
'public/assets', | |
]; | |
$linked_files = [ | |
'.env', | |
]; | |
$current_path = "{$application_path}/current"; | |
$releases_path = "{$application_path}/releases"; | |
$shared_path = "{$application_path}/shared"; | |
$release_tmp_dir = "_releasing"; | |
$release_tmp_path = "{$releases_path}/{$release_tmp_dir}"; | |
$release_dir = '' . (new DateTime())->format('YmdHis'); | |
$release_path = "{$releases_path}/{$release_dir}"; | |
$releases_count = 6; | |
$php = '/usr/bin/php'; | |
$composer = '~/bin/composer'; | |
@endsetup | |
@servers(['local' => 'localhost', 'web' => $server]) | |
@task('init', ['on' => 'web']) | |
mkdir -p {{ $releases_path }}; | |
mkdir -p {{ $shared_path }}; | |
@foreach($shared_dirs as $dir) | |
mkdir -p {{ $shared_path }}/{{ $dir }}; | |
@endforeach | |
echo "Repository initialised"; | |
@endtask | |
@task('intro', ['on' => 'local']) | |
echo "Start deploy on {{ $server }} to {{ $env }}"; | |
@endtask | |
@task('git', ['on' => 'web']) | |
cd {{ $releases_path }}; | |
rm -rf {{ $release_tmp_dir }}; | |
git clone {{ $repository_path }} --branch={{ $branch }} {{ $release_tmp_dir }}; | |
echo "Repository cloned on branch \"{{ $branch }}\""; | |
@endtask | |
@task('share_paths', ['on' => 'web']) | |
cd {{ $release_tmp_path }}; | |
@foreach($linked_dirs as $dir) | |
rm -rf {{ $release_tmp_path }}/{{ $dir }}; | |
ln -s {{ $shared_path }}/{{ $dir }} {{ $release_tmp_path }}/{{ $dir }}; | |
echo "Shared directory {{ $dir }} set up"; | |
@endforeach | |
@foreach($linked_files as $file) | |
rm -f {{ $release_tmp_path }}/{{ $file }}; | |
ln -s {{ $shared_path }}/{{ $file }} {{ $release_tmp_path }}/{{ $file }}; | |
echo "Shared file {{ $file }} set up"; | |
@endforeach | |
@endtask | |
@task('share_storage', ['on' => 'web']) | |
cd {{ $release_tmp_path }}; | |
ln -s {{ $shared_path }}/storage/app/public {{ $release_tmp_path }}/public/storage; | |
echo "Storage set up"; | |
@endtask | |
@task('composer', ['on' => 'web']) | |
cd {{ $release_tmp_path }}; | |
echo "Downloading Composer packages..."; | |
{{ $php }} {{ $composer }} install --no-dev --prefer-dist --quiet --optimize-autoloader --no-scripts; | |
echo "Composer packages installed"; | |
@endtask | |
@task('migration', ['on' => 'web']) | |
cd {{ $release_tmp_path }}; | |
{{ $php }} artisan migrate --force; | |
@endtask | |
@task('assets_compile', ['on' => 'local']) | |
yarn run production --no-progress; | |
@endtask | |
@task('assets_rollout', ['on' => 'local']) | |
rsync -avz -e 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' public/assets/ {{ $server }}:{{ $shared_path }}/public/assets/; | |
echo "Assets published"; | |
@endtask | |
@task('deployment_finish', ['on' => 'web']) | |
cd {{ $releases_path }}; | |
mv {{ $release_tmp_dir }} {{ $release_dir }}; | |
echo "Deployment ({{ $release_dir }}) completed"; | |
@endtask | |
@task('cache_clear', ['on' => 'web']) | |
cd {{ $release_path }}; | |
{{ $php }} artisan clear-compiled; | |
{{ $php }} artisan view:clear; | |
{{ $php }} artisan cache:clear; | |
{{ $php }} artisan config:clear; | |
{{ $php }} artisan route:clear; | |
echo "Cache cleared"; | |
@endtask | |
@task('cache_make', ['on' => 'web']) | |
cd {{ $release_path }}; | |
{{ $php }} artisan config:cache; | |
{{ $php }} artisan route:cache; | |
echo "Cache make"; | |
@endtask | |
@task('deployment_publish', ['on' => 'web']) | |
cd {{ $application_path }}; | |
rm -f current; | |
ln -s {{ $release_path }} current; | |
echo "Deployment published !"; | |
@endtask | |
@task('workers_restart', ['on' => 'web']) | |
cd {{ $current_path }}; | |
{{ $php }} artisan queue:restart; | |
echo "Workers restarted"; | |
@endtask | |
@task('down', ['on' => 'web']) | |
cd {{ $current_path }}; | |
{{ $php }} artisan down; | |
echo "DOWN"; | |
@endtask | |
@task('up', ['on' => 'web']) | |
cd {{ $current_path }}; | |
{{ $php }} artisan up; | |
echo "UP"; | |
@endtask | |
@task('deployments_cleanup', ['on' => 'web']) | |
cd {{ $releases_path }}; | |
rm -rf {{ $release_tmp_dir }}; | |
{{-- https://stackoverflow.com/a/10119963 --}} | |
ls -tr | head -n -{{ $releases_count }} | xargs rm -rf; | |
echo "Old deploys removed"; | |
@endtask | |
@task('rollback', ['on' => 'web']) | |
cd {{ $releases_path }}; | |
{{-- TODO: rollback with argument OR last item --}} | |
echo "Rollback to..."; | |
@endtask | |
@story('deploy') | |
intro | |
git | |
share_paths | |
share_storage | |
composer | |
migration | |
assets_compile | |
assets_rollout | |
deployment_finish | |
cache_clear | |
cache_make | |
deployment_publish | |
workers_restart | |
deployments_cleanup | |
@endstory | |
@story('deploy-code') | |
intro | |
git | |
share_paths | |
share_storage | |
composer | |
deployment_finish | |
cache_clear | |
cache_make | |
deployment_publish | |
workers_restart | |
deployments_cleanup | |
@endstory | |
@task('artisan', ['on' => 'web']) | |
cd {{ $current_path }}; | |
{{ $php }} artisan {{ $command ?? '' }}; | |
@endtask |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment