Skip to content

Instantly share code, notes, and snippets.

@reshadman
Last active April 26, 2021 22:04
Show Gist options
  • Save reshadman/23ecebb392d21f9d5207 to your computer and use it in GitHub Desktop.
Save reshadman/23ecebb392d21f9d5207 to your computer and use it in GitHub Desktop.
<?php
require_once 'recipe/common.php';
// A deploy script for for deployer.org used at Jobinja.ir
// The script creates a folder for each one of your releases
// and makes them zero downtime deploys, this is tuned for laravel
// but the logic can be used for anything which needed transactional folder
// based changes.
// As some of the npm packages are banned in our server ISP we use
// a combination of shadowsocks and polipo(for reversing https and socks)
// to allow npm to install packages through an HTTP proxy which
// is based on shadowsocks.
// You will need ssh key based login on your remote server.
// Local passphrase for the key between your machine and remote machine
$localKeyPass = file_get_contents('.keypass');
$shadowSocksPath = '/path/to/server/shadowsocks.json';
// Shared dirs between releases
set('shared_dirs', [
'storage/app',
'storage/framework/cache',
'storage/framework/sessions',
'storage/framework/views',
'storage/logs'
]);
// Shared files between releases
set('shared_files', ['.env', '.number_of_deploys']);
// Writable dirs after each release
set('writable_dirs', ['storage', 'vendor', 'bootstrap/cache']);
// Start shadowsocks and map polipo for
// making npm able to download packages through an HTTPS proxy
task('deploy:proxy-on', function () use($shadowSocksPath) {
run('service polipo stop');
// Temp : allow server to connect to shadowsocks server
run('ufw allow __YOUR_PORT_NUMBER__/tcp');
run('ufw allow __YOUR_PORT_NUMBER__/udp');
// Run shadowsocks client on server
run("sslocal -c {$shadowSocksPath} --user srvuser -d start &");
// Map
run('polipo socksParentProxy=localhost:__YOUR_PORT_NUMBER__ &');
// Ensure (ok try to ensure :D) shadowsocks is connected.
run("sleep 3");
});
// Stop proxy after deploy
task('deploy:proxy-off', function () use($shadowSocksPath) {
run("service polipo stop");
run("sslocal -c {$shadowSocksPath} -d stop");
run("ufw deny __YOUR_PORT_NUMBER__/tcp");
run("ufw deny __YOUR_PORT_NUMBER__/udp");
});
task('deploy:build', function () {
cd("{{release_path}}");
run('npm --proxy http://localhost:__PORT__ install --silent');
run('bower install --silent --quiet');
run('grunt build --silent');
// @TODO move this to grunt.
run('wget http://www.google-analytics.com/analytics.js -O public/assets/js/ga.js');
});
// Restart php fpm on each deploy
task('deploy:fpm', function () {
run('service php5-fpm restart');
});
// Run DB migrations on each deploy
task('deploy:migrations', function () {
cd('{{release_path}}');
run('php artisan migrate --force');
});
// Optimize laravel
task('deploy:optimize', function () {
run('php artisan optimize');
});
// Configure permissions make www-data their owner
task('deploy:perms', function () {
run('cd {{release_path}} && cd ../ && chown -R www-data:www-data ./');
});
// Restart workers on each deploy
task('deploy:workers', function () {
cd('{{release_path}}');
run('php artisan queue:restart');
});
// Reset cache
task('deploy:cache', function () {
cd('{{release_path}}');
run('php artisan clear-compiled');
run('php artisan eloquent:clear');
run('php artisan config:clear');
run('php artisan view:clear');
run('php artisan route:clear');
run('php artisan route:cache');
run('php artisan config:cache');
run('php artisan eloquent:cache --quiet');
});
// Copy .env
task('deploy:env', function () {
run('cd {{release_path}} && cd ../../ && cp shared/.env {{release_path}}/.env');
});
// Show success message at the end of the deploy
task('jobinja_success', function () {
writeln('New Jobinja release successfully deployed, Cheers!');
});
// Notify team of the new deploy
task('deploy:increment', function () {
run('cd {{deploy_path}}/current && php artisan increment-deploy-command-name');
});
task('deploy', [
'deploy:proxy-on',
'deploy:prepare',
'deploy:release',
'deploy:update_code',
'deploy:vendors',
'deploy:env',
'deploy:shared',
'deploy:cache',
'deploy:migrations',
'deploy:optimize',
'deploy:build',
'deploy:perms',
'deploy:fpm',
'deploy:symlink',
'cleanup',
'deploy:increment',
'deploy:workers',
'deploy:proxy-off'
])->desc('Deploy jobinja web application');
after('deploy', 'jobinja_success');
server('prod', 'youserver.com', 'your_server_ssh_port')
->user('srvuser')
->identityFile('path/to/ssh_key/id_rsa.pub', 'path/to/ssh_key/id_rsa', $localKeyPass)
->stage('production')
->env('branch', 'your-git-branch')
->env('deploy_path', '/path/to/keep/releases');
set('repository', 'git@github.com:project/name.git');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment