当前位置: 首页 > 工具软件 > Capistrano > 使用案例 >

rails capistrano 自动化部署

尤研
2023-12-01

1. 在本地生存ssh key ,如果有,可以跳过,如果没有,参考

https://blog.csdn.net/tang05709/article/details/78470258

2.在服务器创建deploy用户并给与sudo权限

adduser deploy --ingroup sudo

3.在服务器安装nodejs,yarn, ruby,bundle, rails,mysql及相关依赖

bundler要手动在服务器上安装好

4.服务器也生成ssh key, 并在.ssh,即id_rsd.pub 下同级目录下新建authorized_keys文件,把本地的id_rsd.pub里的东西复制进authorized_keys,这样可以免密登录

5.修改Gemfile,在development下添加capistrano

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  gem 'capistrano', '~> 3.11'
  gem 'capistrano-rvm', '~> 0.1.2'
  gem 'capistrano-rails', '~> 1.4'
  gem 'capistrano3-puma', '~> 3.1', '>= 3.1.1'
end

然后运行bundle install, 

docker : docker-compose build web

安装好后运行

bundle exec cap install

这样会在根目录下生成Capfile,在config目录下生成deploy.rb和deploy目录

6.修改Capfile,添加require

require "capistrano/rvm"
require 'capistrano/rails'
require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require 'capistrano/puma'
require 'capistrano/puma/nginx'
install_plugin Capistrano::Puma
install_plugin Capistrano::Puma::Nginx

rvm和rbenv看各自选择,只能选其一

7. 修改config/deploy.rb

`ssh-add`
# config valid for current version and patch releases of Capistrano
lock "~> 3.11.0"


set :application, "jizan"
#set :repo_url, "git@example.com:me/my_repo.git"
#username github的用户名 demo git项目
set :repo_url, "git@github.com:username/demo.git"

set :rvm_type, :user
set :rvm_ruby_version,  File.read('.ruby-version').strip

# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

# Default deploy_to directory is /var/www/my_app_name
# set :deploy_to, "/var/www/my_app_name"
set :deploy_to, "/home/deploy/xxx"

# Default value for :format is :airbrussh.
# set :format, :airbrussh

# puma
set :puma_init_active_record, true
set :puma_threads, [0, 8]
set :puma_workers, 2

# 前端
# set :compile_client, ask('compile client? (yes: 编译, no: 跳过)', 'no')

# 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", "config/application.yml", "config/master.key", "public/robots.txt"

# Default value for linked_dirs is []
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/uploads", "public/uwrite", "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

新版本没有secret.yml和application.yml,但多了master.key

8.修改config/deploy/production.rb

server "0.0.0.0", user: "deploy", roles: %w{app db web}

0.0.0.0 服务器ip

9.修改bin/setup

#!/usr/bin/env ruby
require 'fileutils'
include FileUtils

# path to your application root.
APP_ROOT = File.expand_path('..', __dir__)

def system!(*args)
  system(*args) || abort("\n== Command #{args} failed ==")
end

chdir APP_ROOT do
  # This script is a starting point to setup your application.
  # Add necessary setup steps to this file.

  puts '== Installing dependencies =='
  system! 'gem install bundler --conservative'
  system('bundle check') || system!('bundle install')

  # Install JavaScript dependencies if using Yarn
  system!('bin/yarn')

  puts "\n== Copying sample files =="
  unless File.exist?('config/database.yml')
    cp 'config/database.example.yml', 'config/database.yml'
  end
  unless File.exist?('config/application.yml')
    cp 'config/application.example.yml', 'config/application.yml'
  end

  puts "\n== Preparing database =="
  system! 'bin/rails db:setup'

  puts "\n== Removing old logs and tempfiles =="
  system! 'bin/rails log:clear tmp:clear'

  puts "\n== Restarting application server =="
  system! "rm -f tmp/pids/server.pid"
  system! 'bin/rails restart'
end

我使用了yarn,所以去掉了yarn的注释

因为我增加了application.yml,所以也需要相应增加判断

10.运行 cap production deploy:check查看问题并处理问题

11. 运行cap production deploy 部署

 

我踩过的坑

1.set :repo_url, "git@github.username/demo.git"

username是github的用户名

2.rake stderr: /usr/share/yarn/lib/cli.js:45726

Sprockets::FileNotFound: couldn't find file 'jquery' with type 'application/javascript'

这个是nodejs版本过低,和yarn版本相差太大造成的,升级nodejs

3.部署好后样式获取不到

修改config/envionments/production.rb

config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

改为

config.public_file_server.enabled = true

 

 类似资料: