Skip to content

Instantly share code, notes, and snippets.

@boris-glumpler
Last active July 13, 2023 17:39
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 25 You must be signed in to fork a gist
  • Save boris-glumpler/08206c515e3fe97e5391 to your computer and use it in GitHub Desktop.
Save boris-glumpler/08206c515e3fe97e5391 to your computer and use it in GitHub Desktop.
{{-- Define all our servers --}}
@servers(['staging' => '', 'production' => ''])
@setup
{{-- The timezone your servers run in --}}
$timezone = 'Europe/Amsterdam';
{{-- The base path where your deployments are sitting --}}
$path = '/var/www/site.com/htdocs';
{{-- The git repository location --}}
$repo = '';
{{-- The git branch to deploy --}}
$branch = 'master';
{{-- The number of releases to keep --}}
$keep = 6;
{{-- Is the HTMLPurifier library installed --}}
$hasHtmlPurifier = true;
{{-- Files and direcrtories that need permissions of 755 and www-data as owner --}}
$chmods = [
'app/storage',
'app/database/production.sqlite',
'public',
];
{{-- All directories symlinked to the shared folder --}}
$symlinks = [
'storage/views' => 'app/storage/views',
'storage/sessions' => 'app/storage/sessions',
'storage/logs' => 'app/storage/logs',
'storage/cache' => 'app/storage/cache',
];
{{-- DO NOT EDIT BELOW THIS LINE --}}
$date = new DateTime('now', new DateTimeZone($timezone));
$release = $path .'/releases/'. $date->format('YmdHis');
@endsetup
{{-- Clone task, creates release directory, then shallow clones into it --}}
@task('clone', ['on' => $on])
mkdir -p {{ $release }}
git clone --depth 1 -b {{ $branch }} "{{ $repo }}" {{ $release }}
echo "Repository has been cloned"
@endtask
{{-- Updates composer, then runs a fresh installation --}}
@task('composer', ['on' => $on])
composer self-update
cd {{ $release }}
composer install --no-interaction --no-dev --prefer-dist
echo "Composer dependencies have been installed"
@endtask
{{-- Set permissions for various files and directories --}}
@task('chmod', ['on' => $on])
@foreach($chmods as $file)
chmod -R 755 {{ $release }}/{{ $file }}
chmod -R g+s {{ $release }}/{{ $file }}
chown -R www-data:www-data {{ $release }}/{{ $file }}
echo "Permissions have been set for {{ $file }}"
@endforeach
@if($hasHtmlPurifier)
chmod -R 777 {{ $release }}/vendor/ezyang/htmlpurifier/library/HTMLPurifier/DefinitionCache/Serializer
@endif
echo "Permissions for HTMLPurifier have been set"
@endtask
{{-- Symlink some folders --}}
@task('symlinks', ['on' => $on])
@foreach($symlinks as $folder => $symlink)
ln -s {{ $path }}/shared/{{ $folder }} {{ $release }}/{{ $symlink }}
echo "Symlink has been set for {{ $symlink }}"
@endforeach
echo "All symlinks have been set"
@endtask
{{-- Set the symlink for the current release --}}
@task('update-symlink', ['on' => $on])
rm -rf {{ $path }}/current
ln -s {{ $release }} {{ $path }}/current
echo "Release symlink has been set"
@endtask
{{-- Migrate all databases --}}
@task('migrate', ['on' => $on])
php {{ $release }}/artisan migrate
@endtask
{{-- Just a done message :) --}}
@task('done', ['on' => $on])
echo "Deployment finished successfully!"
@endtask
{{-- Run all deployment tasks --}}
@macro('deploy')
clone
composer
chmod
migrate
symlinks
update-symlink
done
@endmacro
@boris-glumpler
Copy link
Author

The @servers and @setup sections need to be updated with your information. Copy to your Laravel project root and deploy:

envoy run deploy --on=staging

Folder structure:

The deploy task expects a certain folder structure in $path, similar to Rocketeer and Capistrano

+-+ current
|
+-+ releases
|
+-+ shared
  |
  +-+ storage
    |
    +- cache
    |
    +- logs
    |
    +- sessions
    |
    +- views

Still to do:

  • Write a command that cleans up the releases, taking $keep into account
  • Rollback functionality

@Leenug
Copy link

Leenug commented Feb 17, 2015

This was really helpful - did you ever get around to creating the rollback / cleanup tasks?

@atinder
Copy link

atinder commented Mar 9, 2015

chown -R www-data:www-data (changing ownership : Operation not permitted )
any solution for this ?

Also this will create a symlink inside the storage folders like so

storage/views/views because directory already exist.

correct me if i am wrong here

@juukie
Copy link

juukie commented Mar 10, 2015

Thanks for sharing.

@dancourse
Copy link

I'm never sure where to run the database migration scripts. Because if you run it early and the deployment fails before changing the current symlink then the db is out of sync...?

@tommymarshall
Copy link

This still the same deploy script you're using?

@tommymarshall
Copy link

One small but very important note is that Envoy doesn't utilize Blade comments the same way, so the above code wouldn't compile. Replace the {{-- --}}'s with #'s and you'll be fine.

@jasperf
Copy link

jasperf commented May 9, 2017

@tommymarshall is this difference in the use of comments documented somewhere?

@nguyenphuocnhatthanh
Copy link

How to keep file uploaded in server (not cloud) ?:(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment