Created
April 8, 2018 07:52
-
-
Save behroozam/54ffd583f10c453f92354eae5b5e4b6b to your computer and use it in GitHub Desktop.
php deoplyer for deploy docker-compose laravel project in test and production server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Deployer; | |
require 'recipe/common.php'; | |
const PRODUCTION_ENV = 'production'; | |
const TEST_ENV = 'test'; | |
// Project name | |
set('application', 'my_project'); | |
// Project repository | |
// [Optional] Allocate tty for git clone. Default value is false. | |
// Shared files/dirs between deploys | |
set('shared_files', [ | |
'code/.env', | |
'logstash/lastrun_file', | |
'logstash/lastrun_record', | |
'logstash/lastrun_suggestion_tag', | |
'logstash/lastrun_suggestion_record_title' | |
]); | |
set('shared_dirs', [ | |
'code/storage', | |
'mysql/data', | |
'nodejs/yarncache', | |
'elasticsearch/data' , | |
'composer/cache' | |
]); | |
// Writable dirs by web server | |
set('writable_dirs', [ | |
'code/bootstrap/cache', | |
'code/storage', | |
'code/storage/app', | |
'code/storage/app/public', | |
'code/storage/framework', | |
'code/storage/framework/cache', | |
'code/storage/framework/sessions', | |
'code/storage/framework/views', | |
'code/storage/logs', | |
]); | |
// Hosts | |
host('testhostIP') | |
->stage(TEST_ENV) | |
->user('root') | |
->set('deploy_path', '/var/www/html/') | |
->set('writable_mode', 'chmod') | |
->set('branch', 'master') | |
->set('repository', 'git@gyourgitserver:yourname/yourproject.git') | |
->set('second_repository', 'git@gyourgitserver:yourname/yourproject.git'); | |
host('productionhostIP') | |
->stage(PRODUCTION_ENV) | |
->user('root') | |
->set('deploy_path', '/var/www/html/') | |
->set('writable_mode', 'chmod') | |
->set('branch', 'master') | |
->set('repository', 'git@gyourgitserver:yourname/yourproject.git') | |
->set('second_repository', 'git@gyourgitserver:yourname/yourproject.git'); | |
// Tasks | |
desc('Deploy your project'); | |
task('deploy', [ | |
'git:update', | |
'deploy:info', | |
'deploy:prepare', | |
'deploy:lock', | |
'deploy:release', | |
'deploy:update_code', | |
'deploy:eagle' , | |
'deploy:shared', | |
'deploy:writable', | |
'deploy:clear_paths', | |
'composer:install', | |
'deploy:nodejs', | |
'deploy:yarn', | |
'deploy:symlink', | |
'deploy:pagespeed', | |
'deploy:unlock', | |
'cleanup', | |
'success', | |
'docker:stop', | |
'deploy:compose', | |
'deploy:update_link', | |
'deploy:artisan' | |
]); | |
after('deploy:failed', 'deploy:unlock'); | |
task('composer:install', function () { | |
run('cd {{release_path}} && docker-compose -f docker-compose-deploy.yml run composer composer install', [ | |
'timeout' => 1800, | |
]); | |
})->desc('install composer component'); | |
task('deploy:eagle', function () { | |
run('cd {{release_path}}/code && git clone --recursive {{second_repository}}'); | |
})->desc('clone eagle'); | |
task('deploy:compose', function () { | |
run('cd {{deploy_path}}/current && docker-compose -f docker-compose-deploy.yml up --build -d', [ | |
'timeout' => 1800, | |
]); | |
})->desc('up service'); | |
task('docker:stop', function () { | |
$releases = get('releases_list'); | |
if (isset($releases[1])) { | |
$releaseDir = "{{deploy_path}}/releases/{$releases[1]}"; | |
run("cd $releaseDir && docker-compose down"); | |
} else { | |
writeln("<comment>No more releases you can revert to.</comment>"); | |
} | |
})->desc('Stop last release containers'); | |
task('deploy:nodejs', function () { | |
run("cd {{release_path}} && docker-compose -f docker-compose-deploy.yml run nodejs yarn install --verbose", [ | |
'timeout' => 1800, | |
]); | |
}); | |
task('deploy:yarn', function () { | |
run('cd {{release_path}} && docker-compose -f docker-compose-deploy.yml run nodejs yarn run production'); | |
})->desc('up nodejs service'); | |
task('deploy:update_link', function () { | |
run('cd {{deploy_path}}/current && docker-compose -f docker-compose-deploy.yml run php-fpm php artisan storage:link'); | |
})->desc('update links'); | |
task('deploy:artisan', function () { | |
run('cd {{deploy_path}}/current && docker-compose -f docker-compose-deploy.yml run php-fpm php artisan cache:clear'); | |
run('cd {{deploy_path}}/current && docker-compose -f docker-compose-deploy.yml run php-fpm php artisan config:cache'); | |
//run('cd {{deploy_path}}/current && docker-compose -f docker-compose-deploy.yml run php-fpm php artisan route:cache'); | |
run('cd {{deploy_path}}/current && docker-compose -f docker-compose-deploy.yml run php-fpm php artisan view:clear'); | |
})->desc('clear cash'); | |
task('deploy:pagespeed', function () { | |
run('cd {{deploy_path}}/current/nginx && chown -R 65534 var/'); | |
})->desc('change owner cache directory'); | |
task('deploy:migrate', function () { | |
run("d {{deploy_path}}/current && docker-compose -f docker-compose-deploy.yml run php-fpm php artisan migrate", [ | |
'timeout' => 1800, | |
]); | |
}); | |
//update servers git repository on production stage | |
task('git:update', function () { | |
$stage = input()->hasArgument('stage') ? input()->getArgument('stage') : null; | |
if ($stage === PRODUCTION_ENV) { | |
writeln('<info>updating project code</info>'); | |
runLocally('git pull origin master && git push server master'); | |
writeln('<info>updating second repo code</info>'); | |
runLocally('cd second && git pull origin master'); | |
runLocally('cd second && git push server master'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment