laravel通过envoy实现自动部署
部署的目录结构如下: 1:在Jenkins中先把env的配置写到服务器中的env中: 2:在Jenkins中配置执行envoy的命令 #其中:--env传参表示环境是哪个, --domain表示自定义的参数表示:所在的域名/目录 #!/bin/bash -il /home/deployer/.config/composer/vendor/bin/envoy run deploy --env=staging --domain=mpace-stage.ivideocloud.cn if [ $? -ne 0 ]; then echo "============================failed==================================" exit 1 else echo "===========================succeed==================================" fi #其中 run_migrate表示envoy中执行的migrate的故事块.详见envoy的配置 /home/deployer/.config/composer/vendor/bin/envoy run run_migrate --env=staging --domain=mpace-stage.ivideocloud.cn if [ $? -ne 0 ]; then echo "============================failed==================================" exit 1 else echo "===========================succeed==================================" fi 2:Laravel中的Envoy.blade.php中的配置如下: 其中:$env和$domain就是在Jenkins中运行envoy时添加的参数. //定义好服务器 @servers(['staging' => ['mpace-staging', 'huizhou-vpn', 'huizhou-internal', 'mpace-aws'], 'staging_run_migrate' => ['mpace-staging'],'mirror' => ['mpace-staging', 'huizhou-vpn', 'huizhou-internal', 'mpace-aws'], 'mirror_run_migrate' => ['mpace-staging'],'qa' => ['mpace-staging', 'huizhou-vpn', 'huizhou-internal', 'mpace-aws'], 'qa_run_migrate' => ['mpace-staging'],'production' => ['mpace-prod-node', 'huizhou-vpn', 'huizhou-internal', 'mpace-aws'], 'production_run_migrate' => ['mpace-prod-node']]) @setup $environment = isset($env) ? $env : "staging"; $domain = isset($domain) ? $domain : "mpace.ivideocloud.cn"; $environment_run_migrate = $environment . '_run_migrate'; $app_dir = '/var/www/html/'.$environment.'/'.trim($domain); $code_base = $app_dir.'/code_base'; $releases_dir = $app_dir.'/releases'; $release_dir = $releases_dir . '/' . date('YmdHis'); $staging_deployer_site_worker_name = 'mpace-worker:*'; @endsetup @story('deploy', ['on' => $environment, 'parallel' => true]) git_pull run_composer copy_release update_symlinks restart_queue remove_old_release @endstory @task('git_pull') echo 'Git pull' cd {{ $code_base }} git pull @endtask @task('run_composer') echo "Run Composer" cd {{ $code_base }} composer install --prefer-dist --no-scripts -q -o echo 'Linking .env file' ln -nfs {{ $app_dir }}/.env {{ $code_base }}/.env @endtask @task('copy_release') echo 'Copy release' cp -R {{ $code_base }} {{ $release_dir }} cd {{ $release_dir }} composer dumpautoload chown -R deployer:nginx {{ $release_dir }} chmod 775 {{ $release_dir }}/bootstrap/cache @endtask @task('update_symlinks') echo "Linking storage directory" rm -rf {{ $release_dir }}/storage ln -nfs {{ $app_dir }}/storage {{ $release_dir }}/storage echo 'Linking .env file' ln -nfs {{ $app_dir }}/.env {{ $release_dir }}/.env echo 'Linking current release' ln -nfs {{ $release_dir }} {{ $app_dir }}/current cd {{ $app_dir }}/current php artisan config:clear @endtask //重启队列 @task('restart_queue') echo "Restarting queue" cd {{ $app_dir }}/current php artisan queue:restart @endtask //运行migrate的task @task('run_migrate', ['on' => $environment_run_migrate]) echo "Starting run migrate" cd {{ $app_dir }}/current php artisan migrate --force @endtask //删除旧relese的脚本 @task('remove_old_release') echo 'Remove old release' /var/tmp/bin/remove_old_release.sh {{ $releases_dir }} @endtask
3:删除旧relese的脚本
#!/bin/sh
if [ $# -ne 1 ]
then
echo "Usage: $0 dst"
exit $E_BADARGS
fi
count=`ls $1 | wc -l`
MAX_RELEASE=3
while [ $count -gt $MAX_RELEASE ]
do
oldest=`ls -t $1 | tail -1`
rm -rf $1/$oldest
count=`ls $1 | wc -l`
done