Skip to content

Instantly share code, notes, and snippets.

@harrygr
Last active January 19, 2023 23:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save harrygr/370c273d8cb3aa457232 to your computer and use it in GitHub Desktop.
Save harrygr/370c273d8cb3aa457232 to your computer and use it in GitHub Desktop.
Envoy deploy script for Laravel
{{--
INSTRUCTIONS
1. On your local/build/deployment system:
- Ensure Laravel Envoy is installed on your deployment box (local computer, build server etc - not your production server).
(see https://laravel.com/docs/5.2/envoy)
- Set up a certificate or ssh config such that you can ssh to your production server without prompt.
- Update the @servers array below with your production ssh details or ssh config.
- Update the $php variable to represent the php app server for your server.
2. On your production server:
- Create a user 'deployer' (or whatever user you set for the previous step) and add them to the webserver group (e.g. www-data).
- Give them sudo privilages to restart php, nginx etc without a password.
See https://serversforhackers.com/video/sudo-and-sudoers-configuration
- Create your project directory which will be the value of $site_dir. Give it write permissions for the www-data group.
- Create your .env in the project directory file and populate it according to your project.
4. Push your project to a remote git repository and update the value of $repo to correspond to it.
- If your repo is private, add your server's public key to your repo hosting account so it can be cloned.
5. When your build is ready (tests pass, ready to deploy) run `envoy run deploy` on your build system.
If all goes well your project will be updated on the server and seamlessly deployed without downtime.
The storage folder will be symlinked to the project directory level one, which will be created if it doesn't yet exist (for first deploy).
--}}
@servers(['web' => 'deployer@host'])
@setup
$repo = 'https://github.com/user/repo.git';
$site_dir = '/var/www';
$release_dir = $site_dir.'/releases';
$app_dir = $site_dir.'/app';
$release = 'release_' . date('Ymd_Hi_s');
$php = 'php7.0-fpm';
@endsetup
@macro('deploy', ['on' => 'web'])
fetch_repo
run_composer
update_permissions
update_symlinks
@endmacro
@task('fetch_repo')
echo "fetching repo"
[ -d {{ $release_dir }} ] || mkdir {{ $release_dir }};
cd {{ $release_dir }};
git clone -b master --depth=1 {{ $repo }} {{ $release }};
@endtask
@task('run_composer')
cd {{ $release_dir }}/{{ $release }};
composer install --prefer-dist;
@endtask
@task('update_permissions')
echo "setting permissions"
cd {{ $release_dir }};
chgrp -R www-data {{ $release }};
chmod -R ug+rwx {{ $release }};
@endtask
@task('update_symlinks')
echo "updating symlinks";
{{-- project folder --}}
echo "- linking project folder";
ln -nfs {{ $release_dir }}/{{ $release }} {{ $app_dir }};
chgrp -h www-data {{ $app_dir }};
{{-- environment file --}}
echo "- linking environment file";
cd {{ $release_dir }}/{{ $release }};
ln -nfs ../../.env .env;
chgrp -h www-data .env;
{{-- storage folder --}}
echo "- linking storage folder";
{{-- Build up the storage folder if it doesn't exist --}}
[ -d {{ $site_dir }}/storage ] || { cp -a {{ $release_dir }}/{{ $release }}/storage {{ $site_dir }}/storage; chgrp -R www-data {{ $site_dir }}/storage; chmod -R ug+rwx {{ $site_dir }}/storage;}
{{-- Remove the release storage dir and symlink to the external one --}}
rm -rf {{ $release_dir }}/{{ $release }}/storage;
cd {{ $release_dir }}/{{ $release }};
ln -nfs ../../storage storage;
chgrp www-data storage;
{{-- Deploying user must have permission to restart php via sudo without password --}}
sudo service {{ $php }} reload;
@endtask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment