Skip to content

Instantly share code, notes, and snippets.

@chrispage1
Last active July 29, 2020 09:49
Show Gist options
  • Save chrispage1/835131fe8e8e0556ae1dd405b32c7f9b to your computer and use it in GitHub Desktop.
Save chrispage1/835131fe8e8e0556ae1dd405b32c7f9b to your computer and use it in GitHub Desktop.
Deployer - deploy Laravel instance with local npm compile (optional) and tag validation for production deployments
<?php
namespace Deployer;
require 'recipe/laravel.php';
// deployment configuration options.
set('repository', 'git@github.com/username/repository.git');
// set('http_user', 'username'); // optionally set file user
// set('bin/php', '/usr/local/bin/ea-php74'); // optionally set PHP path
// setup our staging host.
host('staging')
->stage('staging')
->hostname('yourhostname.example')
->user('username')
->set('deploy_path', '/path/to/your/site/staging');
// setup our production host.
host('production')
->stage('production')
->hostname('yourhostname.example')
->user('username')
->set('deploy_path', '/path/to/your/site/production');
// determine the path to our service.
set('bin/service', function () {
return locateBinaryPath('service');
});
// configurable options.
add('shared_files', []);
add('shared_dirs', []);
add('writable_dirs', []);
set('allow_anonymous_stats', false);
// ask if we are compiling local resources.
task('validate:compile', function () {
$compile_resources = ask('Would you like to compile resources [y|yes]?');
$compile_resources = in_array(strtolower($compile_resources), ['y', 'yes']);
set('compile_resources', $compile_resources);
});
before('deploy', 'validate:compile');
// ensure we have a tag on production.
task('validate:tag', function () {
if (!input()->getOption('tag')) {
throw new \Exception(
'You must supply a valid tag when deploying to ' .
input()->getArgument('stage')
);
}
})->onStage('production');
before('validate:compile', 'validate:tag');
// load in our remote .env file for local compilation.
task('env:load', function () {
runLocally('cp .env .env.deployer');
download('{{deploy_path}}/shared/.env', '.env');
})->desc('Load the contents of our remote .env file so we can compile with it locally');
after('validate:compile', 'env:load');
// restore our local .env file after it has been used.
task('env:restore', function () {
if(test('[ -f ./.env.deployer ]')) {
run('mv .env.deployer .env');
}
})->desc('Recover local environment file after env:load has been used')->local();
// compile our local assets for production.
task('npm:build', function () {
set('locally_compiled', false);
if (get('compile_resources') || !test('[ -d {{previous_release}}/public ]')) {
writeLn("Compiling assets locally for {{stage}}. This can take a while...");
runLocally('npm run prod -s');
set('locally_compiled', true);
} else {
writeLn("Skipping asset compilation for {{stage}}.");
}
})->desc('Compile npm files locally');
after('deploy:shared', 'npm:build');
// upload our locally compiled assets.
task('deploy:upload-public', function () {
if (get('locally_compiled')) {
upload('public', '{{release_path}}', ['options' => ['--no-links']]);
} else {
writeLn('Copying public directory from previous release.');
run('cp -R {{previous_release}}/public {{release_path}}');
}
})->desc('Upload the public directory to the environment');
after('npm:build', 'deploy:upload-public');
after('deploy:upload-public', 'deploy:public_disk');
// if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');
after('deploy:failed', 'env:restore');
// migrate database & link our storage before linking to new release.
before('deploy:symlink', 'artisan:migrate');
before('deploy:symlink', 'artisan:storage:link');
// restore our environment file & restart our queue.
after('deploy', 'env:restore');
after('deploy', 'artisan:queue:restart');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment