From ff09ca60cf5c1fb75cdf41f0de1f19eaccc3cb01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Mon, 10 Jan 2022 11:18:57 +0800 Subject: [PATCH] Deploy scripts --- deploy/Capfile | 38 +++++ deploy/config/deploy.rb | 54 +++++++ deploy/config/deploy/admin.rb | 62 ++++++++ deploy/config/deploy/production.rb | 62 ++++++++ deploy/config/deploy/staging.rb | 63 ++++++++ deploy/lib/capistrano/tasks/composer.rake | 41 +++++ deploy/lib/capistrano/tasks/laravel.rake | 176 ++++++++++++++++++++++ deploy/log/.gitignore | 2 + 8 files changed, 498 insertions(+) create mode 100644 deploy/Capfile create mode 100644 deploy/config/deploy.rb create mode 100644 deploy/config/deploy/admin.rb create mode 100644 deploy/config/deploy/production.rb create mode 100644 deploy/config/deploy/staging.rb create mode 100644 deploy/lib/capistrano/tasks/composer.rake create mode 100644 deploy/lib/capistrano/tasks/laravel.rake create mode 100644 deploy/log/.gitignore diff --git a/deploy/Capfile b/deploy/Capfile new file mode 100644 index 00000000..42dd3a47 --- /dev/null +++ b/deploy/Capfile @@ -0,0 +1,38 @@ +# Load DSL and set up stages +require "capistrano/setup" + +# Include default deployment tasks +require "capistrano/deploy" + +# Load the SCM plugin appropriate to your project: +# +# require "capistrano/scm/hg" +# install_plugin Capistrano::SCM::Hg +# or +# require "capistrano/scm/svn" +# install_plugin Capistrano::SCM::Svn +# or +require "capistrano/scm/git" +install_plugin Capistrano::SCM::Git + +# Include tasks from other gems included in your Gemfile +# +# For documentation on these, see for example: +# +# https://github.com/capistrano/rvm +# https://github.com/capistrano/rbenv +# https://github.com/capistrano/chruby +# https://github.com/capistrano/bundler +# https://github.com/capistrano/rails +# https://github.com/capistrano/passenger +# +# require "capistrano/rvm" +# require "capistrano/rbenv" +# require "capistrano/chruby" +# require "capistrano/bundler" +# require "capistrano/rails/assets" +# require "capistrano/rails/migrations" +# require "capistrano/passenger" + +# Load custom tasks from `lib/capistrano/tasks` if you have any defined +Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r } diff --git a/deploy/config/deploy.rb b/deploy/config/deploy.rb new file mode 100644 index 00000000..bef2324e --- /dev/null +++ b/deploy/config/deploy.rb @@ -0,0 +1,54 @@ +# config valid for current version and patch releases of Capistrano +lock "~> 3.16.0" + +set :application, "zcsmall" +set :repo_url, "git@gitee.com:zi-chunsheng-e-commerce/mall-server.git" + +# Default branch is :master +# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp +set :branch, "release" + +# Default deploy_to directory is /var/www/my_app_name +# set :deploy_to, "/var/www/my_app_name" + +# Default value for :format is :airbrussh. +# set :format, :airbrussh + +# You can configure the Airbrussh format using :format_options. +# These are the defaults. +# set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto + +# Default value for :pty is false +# set :pty, true + +# Default value for :linked_files is [] +# append :linked_files, "config/database.yml" + +# Default value for linked_dirs is [] +# append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system" + +# Default value for default_env is {} +# set :default_env, { path: "/opt/ruby/bin:$PATH" } + +# Default value for local_user is ENV["USER"] +# set :local_user, -> { `git config user.name`.chomp } + +# Default value for keep_releases is 5 +# set :keep_releases, 5 + +# Uncomment the following to require manually verifying the host key before first deploy. +# set :ssh_options, verify_host_key: :secure + +# 发布时是否上传 .env 文件 +set :laravel_upload_dotenv_file_on_deploy, false + +namespace :php_fpm do + desc "重启 PHP-FPM" + task :restart do + on release_roles :all do + execute :sudo, :systemctl, :restart, "php8.0-fpm.service" + end + end + + after "deploy:finished", "php_fpm:restart" +end diff --git a/deploy/config/deploy/admin.rb b/deploy/config/deploy/admin.rb new file mode 100644 index 00000000..73721a80 --- /dev/null +++ b/deploy/config/deploy/admin.rb @@ -0,0 +1,62 @@ +# server-based syntax +# ====================== +# Defines a single server with a list of roles and multiple properties. +# You can define all roles on a single server, or split them: + +# server "example.com", user: "deploy", roles: %w{app db web}, my_property: :my_value +# server "example.com", user: "deploy", roles: %w{app web}, other_property: :other_value +# server "db.example.com", user: "deploy", roles: %w{db} +server "47.108.202.152", user: "deployer" + + +# role-based syntax +# ================== + +# Defines a role with one or multiple servers. The primary server in each +# group is considered to be the first unless any hosts have the primary +# property set. Specify the username and a domain or IP for the server. +# Don't use `:all`, it's a meta role. + +# role :app, %w{deploy@example.com}, my_property: :my_value +# role :web, %w{user1@primary.com user2@additional.com}, other_property: :other_value +# role :db, %w{deploy@example.com} + + + +# Configuration +# ============= +# You can set any configuration variable like in config/deploy.rb +# These variables are then only loaded and set in this stage. +# For available Capistrano configuration variables see the documentation page. +# http://capistranorb.com/documentation/getting-started/configuration/ +# Feel free to add new variables to customise your setup. + +set :deploy_to, "/www/wwwroot/admin.zichunsheng.cn" + + +# Custom SSH Options +# ================== +# You may pass any option but keep in mind that net/ssh understands a +# limited set of options, consult the Net::SSH documentation. +# http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start +# +# Global options +# -------------- +# set :ssh_options, { +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(password) +# } +# +# The server-based syntax can be used to override options: +# ------------------------------------ +# server "example.com", +# user: "user_name", +# roles: %w{web app}, +# ssh_options: { +# user: "user_name", # overrides user setting above +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(publickey password) +# # password: "please use keys" +# } diff --git a/deploy/config/deploy/production.rb b/deploy/config/deploy/production.rb new file mode 100644 index 00000000..028bfe1d --- /dev/null +++ b/deploy/config/deploy/production.rb @@ -0,0 +1,62 @@ +# server-based syntax +# ====================== +# Defines a single server with a list of roles and multiple properties. +# You can define all roles on a single server, or split them: + +# server "example.com", user: "deploy", roles: %w{app db web}, my_property: :my_value +# server "example.com", user: "deploy", roles: %w{app web}, other_property: :other_value +# server "db.example.com", user: "deploy", roles: %w{db} +server "47.108.202.152", user: "deployer" + + +# role-based syntax +# ================== + +# Defines a role with one or multiple servers. The primary server in each +# group is considered to be the first unless any hosts have the primary +# property set. Specify the username and a domain or IP for the server. +# Don't use `:all`, it's a meta role. + +# role :app, %w{deploy@example.com}, my_property: :my_value +# role :web, %w{user1@primary.com user2@additional.com}, other_property: :other_value +# role :db, %w{deploy@example.com} + + + +# Configuration +# ============= +# You can set any configuration variable like in config/deploy.rb +# These variables are then only loaded and set in this stage. +# For available Capistrano configuration variables see the documentation page. +# http://capistranorb.com/documentation/getting-started/configuration/ +# Feel free to add new variables to customise your setup. + +set :deploy_to, "/www/wwwroot/api.zichunsheng.cn" + + +# Custom SSH Options +# ================== +# You may pass any option but keep in mind that net/ssh understands a +# limited set of options, consult the Net::SSH documentation. +# http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start +# +# Global options +# -------------- +# set :ssh_options, { +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(password) +# } +# +# The server-based syntax can be used to override options: +# ------------------------------------ +# server "example.com", +# user: "user_name", +# roles: %w{web app}, +# ssh_options: { +# user: "user_name", # overrides user setting above +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(publickey password) +# # password: "please use keys" +# } diff --git a/deploy/config/deploy/staging.rb b/deploy/config/deploy/staging.rb new file mode 100644 index 00000000..46de935d --- /dev/null +++ b/deploy/config/deploy/staging.rb @@ -0,0 +1,63 @@ +# server-based syntax +# ====================== +# Defines a single server with a list of roles and multiple properties. +# You can define all roles on a single server, or split them: + +# server "example.com", user: "deploy", roles: %w{app db web}, my_property: :my_value +# server "example.com", user: "deploy", roles: %w{app web}, other_property: :other_value +# server "db.example.com", user: "deploy", roles: %w{db} +server "47.108.227.246", user: "deployer" + + +# role-based syntax +# ================== + +# Defines a role with one or multiple servers. The primary server in each +# group is considered to be the first unless any hosts have the primary +# property set. Specify the username and a domain or IP for the server. +# Don't use `:all`, it's a meta role. + +# role :app, %w{deploy@example.com}, my_property: :my_value +# role :web, %w{user1@primary.com user2@additional.com}, other_property: :other_value +# role :db, %w{deploy@example.com} + + + +# Configuration +# ============= +# You can set any configuration variable like in config/deploy.rb +# These variables are then only loaded and set in this stage. +# For available Capistrano configuration variables see the documentation page. +# http://capistranorb.com/documentation/getting-started/configuration/ +# Feel free to add new variables to customise your setup. + +set :branch, "develop" +set :deploy_to, "/www/wwwroot/test.zichunsheng.cn" + + +# Custom SSH Options +# ================== +# You may pass any option but keep in mind that net/ssh understands a +# limited set of options, consult the Net::SSH documentation. +# http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start +# +# Global options +# -------------- +# set :ssh_options, { +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(password) +# } +# +# The server-based syntax can be used to override options: +# ------------------------------------ +# server "example.com", +# user: "user_name", +# roles: %w{web app}, +# ssh_options: { +# user: "user_name", # overrides user setting above +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(publickey password) +# # password: "please use keys" +# } diff --git a/deploy/lib/capistrano/tasks/composer.rake b/deploy/lib/capistrano/tasks/composer.rake new file mode 100644 index 00000000..1dd2b0dc --- /dev/null +++ b/deploy/lib/capistrano/tasks/composer.rake @@ -0,0 +1,41 @@ +namespace :load do + task :defaults do + set :composer_roles, :all + set :composer_download_url, "https://getcomposer.org/installer" + set :composer_install_flags, "--no-dev --prefer-dist --no-interaction --optimize-autoloader" + end +end + +namespace :composer do + desc "检查 Composer 是否已安装" + task :check do + on release_roles(fetch(:composer_roles)) do + next if test("[ -f #{deploy_path}/.dep/composer.phar ]") + + composer_version = fetch(:composer_version, nil) + composer_version_flag = composer_version ? "-- --version=#{composer_version}" : "" + + execute :mkdir, "-p", "#{deploy_path}/.dep" + execute :curl, "-sS", fetch(:composer_download_url), "|", :php, composer_version_flag + execute :mv, "composer.phar #{deploy_path}/.dep/composer.phar" + end + end + + desc "执行 Composer 命令" + task :run, :command do |t, args| + args.with_defaults(:command => :list) + + on release_roles(fetch(:composer_roles)) do + within release_path do + execute :php, "#{deploy_path}/.dep/composer.phar", args[:command], *args.extras + end + end + end + + desc "安装应用依赖" + task :install do + invoke "composer:run", :install, fetch(:composer_install_flags) + end + + after "deploy:started", "composer:check" +end diff --git a/deploy/lib/capistrano/tasks/laravel.rake b/deploy/lib/capistrano/tasks/laravel.rake new file mode 100644 index 00000000..8ac0bd98 --- /dev/null +++ b/deploy/lib/capistrano/tasks/laravel.rake @@ -0,0 +1,176 @@ +namespace :load do + task :defaults do + set :laravel_roles, :all + + # 是否在部署时上传 .env 文件 + set :laravel_upload_dotenv_file_on_deploy, true + + # 上传时的本地 .env 文件 + set :laravel_dotenv_file, ".env.#{fetch(:stage)}" + + # 软链文件 + set :laravel_linked_files, [".env"] + + # 软链文件夹 + set :laravel_linked_dirs, ["storage"] + + # 可写文件夹 + set :laravel_writable_dirs, [ + "storage", + "storage/app", + "storage/app/public", + "storage/framework", + "storage/framework/cache", + "storage/framework/sessions", + "storage/framework/views", + "storage/logs", + ] + end +end + +namespace :laravel do + desc "上传 .env 文件到共享目录" + task :upload_dotenv_file do + next unless fetch(:laravel_upload_dotenv_file_on_deploy) + + dotenv_file = fetch(:laravel_dotenv_file) + + run_locally do + execute :touch, dotenv_file + end + + on release_roles(fetch(:laravel_roles)) do + upload! dotenv_file, "#{shared_path}/.env" + end + end + + desc "准备软链文件" + task :resolve_linked_files do + on release_roles(fetch(:laravel_roles)) do + laravel_linked_files = fetch(:laravel_linked_files) + + next unless laravel_linked_files + + # 创建用于软链的文件 + within shared_path do + execute :touch, laravel_linked_files.join(" ") + end + + set :linked_files, fetch(:linked_files, []).push(*laravel_linked_files) + end + end + + desc "准备软链目录" + task :resolve_linked_dirs do + laravel_linked_dirs = fetch(:laravel_linked_dirs) + + if fetch(:laravel_linked_dirs) + set :linked_dirs, fetch(:linked_dirs, []).push(*laravel_linked_dirs) + end + end + + desc "准备可写文件" + task :resolve_writable_dirs do + on release_roles(fetch(:laravel_roles)) do + laravel_writable_dirs = fetch(:laravel_writable_dirs) + + next unless laravel_writable_dirs + + within release_path do + execute :mkdir, "-p", laravel_writable_dirs.join(" ") + end + end + end + + desc "执行 artisan" + task :artisan, :command do |t, args| + args.with_defaults(:command => :list) + + on release_roles(fetch(:laravel_roles)) do + within release_path do + execute :php, :artisan, args[:command], *args.extras + end + end + + # 允许多次调用 laravel:artisan 任务 + # https://github.com/capistrano/capistrano/issues/1686 + Rake::Task["laravel:artisan"].reenable + end + + desc "执行 artisan config:cache" + task "artisan:config:cache" do + invoke "laravel:artisan", "config:cache" + end + + desc "执行 artisan db:seed" + task "artisan:db:seed" do + invoke "laravel:artisan", "db:seed", "--force" + end + + desc "执行 artisan migrate" + task "artisan:migrate" do + invoke "laravel:artisan", :migrate, "--force" + end + + desc "执行 artisan migrate:fresh" + task "artisan:migrate:fresh" do + invoke "laravel:artisan", "migrate:fresh", "--force" + end + + desc "执行 artisan migrate:rollback" + task "artisan:migrate:rollback" do + invoke "laravel:artisan", "migrate:rollback", "--force" + end + + desc "执行 artisan optimize" + task "artisan:optimize" do + invoke "laravel:artisan", :optimize + end + + desc "执行 artisan optimize:clear" + task "artisan:optimize:clear" do + invoke "laravel:artisan", "optimize:clear" + end + + desc "执行 artisan queue:restart" + task "artisan:queue:restart" do + invoke "laravel:artisan", "queue:restart" + end + + desc "执行 artisan storage:link" + task "artisan:storage:link" do + invoke "laravel:artisan", "storage:link" + end + + desc "执行 artisan route:cache" + task "artisan:route:cache" do + invoke "laravel:artisan", "route:cache" + end + + desc "执行 artisan view:cache" + task "artisan:view:cache" do + invoke "laravel:artisan", "view:cache" + end + + desc "执行 artisan view:clear" + task "artisan:view:clear" do + invoke "laravel:artisan", "view:clear" + end +end + +namespace :deploy do + task :vendors do + invoke "composer:install" + end + + before "deploy:starting", "laravel:resolve_linked_dirs" + after "deploy:check:directories", "laravel:upload_dotenv_file" + after "deploy:check:directories", "laravel:resolve_linked_files" + after "deploy:updated", "laravel:resolve_writable_dirs" + after "deploy:updated", "deploy:vendors" + after "deploy:vendors", "laravel:artisan:config:cache" + after "deploy:vendors", "laravel:artisan:route:cache" + after "deploy:vendors", "laravel:artisan:view:cache" + after "deploy:vendors", "laravel:artisan:storage:link" + after "deploy:vendors", "laravel:artisan:migrate" +end diff --git a/deploy/log/.gitignore b/deploy/log/.gitignore new file mode 100644 index 00000000..d6b7ef32 --- /dev/null +++ b/deploy/log/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore