Skip to content

Instantly share code, notes, and snippets.

@AdrienPoupa
Created June 2, 2019 20:45
Show Gist options
  • Save AdrienPoupa/24f77e03269f9b398a4f7f778c526d63 to your computer and use it in GitHub Desktop.
Save AdrienPoupa/24f77e03269f9b398a4f7f778c526d63 to your computer and use it in GitHub Desktop.
@setup
require __DIR__.'/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::create(__DIR__);
try {
$dotenv->load();
$dotenv->required(['DEPLOY_USER', 'DEPLOY_SERVER', 'DEPLOY_BASE_DIR', 'DEPLOY_REPO'])->notEmpty();
} catch ( Exception $e ) {
echo $e->getMessage();
}
$repo = env('DEPLOY_REPO');
if (!isset($baseDir)) {
$baseDir = env('DEPLOY_BASE_DIR');
}
if (!isset($branch)) {
throw new Exception('--branch must be specified');
}
$releaseDir = $baseDir . '/releases';
$currentDir = $baseDir . '/current';
$release = date('YmdHis');
$currentReleaseDir = $releaseDir . '/' . $release;
function logMessage($message) {
return "echo '\033[32m" .$message. "\033[0m';\n";
}
@endsetup
@servers(['prod' => env('DEPLOY_USER').'@'.env('DEPLOY_SERVER'), 'localhost' => '127.0.0.1'])
@story('deploy', ['on' => 'prod'])
git
composer
npm_install
npm_run_prod
update_symlinks
cache
migrate
clean_old_releases
@endstory
@story('rollback')
rollback
@endstory
@task('git')
{{ logMessage("Cloning repository") }}
git clone {{ $repo }} --branch={{ $branch }} --depth=1 -q {{ $currentReleaseDir }}
@endtask
@task('composer')
{{ logMessage("Running composer") }}
cd {{ $currentReleaseDir }}
composer install --no-interaction --quiet --no-dev --prefer-dist --optimize-autoloader
@endtask
@task('cache')
{{ logMessage("Building cache") }}
php {{ $currentReleaseDir }}/artisan route:cache --quiet
php {{ $currentReleaseDir }}/artisan config:cache --quiet
@endtask
@task('update_symlinks')
{{ logMessage("Updating symlinks") }}
# Remove the storage directory and replace with persistent data
{{ logMessage("Linking storage directory") }}
rm -rf {{ $currentReleaseDir }}/storage
ln -nfs {{ $baseDir }}/storage {{ $currentReleaseDir }}/storage
# Remove the public uploads directory and replace with persistent data
{{ logMessage("Linking uploads directory") }}
rm -rf {{ $currentReleaseDir }}/public/uploads
ln -nfs {{ $baseDir }}/uploads {{ $currentReleaseDir }}/public/uploads
# Import the environment config
{{ logMessage("Linking .env file") }}
ln -nfs {{ $baseDir }}/.env {{ $currentReleaseDir }}/.env
# Symlink the latest release to the current directory
{{ logMessage("Linking current release") }}
ln -nfs {{ $currentReleaseDir }} {{ $currentDir }}
@endtask
@task('migrate')
{{ logMessage("Running migrations") }}
php {{ $currentReleaseDir }}/artisan migrate --force
@endtask
@task('npm_install')
{{ logMessage("NPM install") }}
cd {{ $currentReleaseDir }}
npm install --silent --no-progress
@endtask
@task('npm_run_prod')
{{ logMessage("NPM run prod") }}
cd {{ $currentReleaseDir }}
npm run prod --silent --no-progress
@endtask
@task('clean_old_releases')
# Delete all but the 5 most recent releases
{{ logMessage("Cleaning old releases") }}
cd {{ $releaseDir }}
ls -dt {{ $releaseDir }}/* | tail -n +6 | xargs -d "\n" rm -rf;
@endtask
@task('init')
if [ ! -d {{ $baseDir }}/current ]; then
cd {{ $baseDir }}
git clone {{ $repo }} --branch={{ $branch }} --depth=1 -q {{ $release }}
{{ logMessage("Repository cloned") }}
mv {{ $release }}/storage {{ $baseDir }}/storage
ln -s {{ $baseDir }}/storage {{ $release }}/storage
ln -s {{ $baseDir }}/storage/public {{ $release }}/public/storage
{{ logMessage("Storage directory set up") }}
cp {{ $release }}/.env.example {{ $baseDir }}/.env
ln -s {{ $baseDir }}/.env {{ $release }}/.env
{{ logMessage("Environment file set up") }}
rm -rf {{ $release }}
{{ logMessage("Deployment path initialised. Run 'envoy run deploy' now.") }}
else
{{ logMessage("Deployment path already initialised (current symlink exists)!") }}
fi
@endtask
@task('rollback')
cd {{ $releaseDir }}
ln -nfs {{ $releaseDir }}/$(find . -maxdepth 1 -name "20*" | sort | tail -n 2 | head -n1) {{ $baseDir }}/current
echo "Rolled back to $(find . -maxdepth 1 -name "20*" | sort | tail -n 2 | head -n1)"
@endtask
@finished
echo "Envoy deployment complete.\r\n";
@endfinished
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment