rails capistrano 自动化部署

2024-03-12 20:08

本文主要是介绍rails capistrano 自动化部署,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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/springgem '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 ==")
endchdir 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 Yarnsystem!('bin/yarn')puts "\n== Copying sample files =="unless File.exist?('config/database.yml')cp 'config/database.example.yml', 'config/database.yml'endunless File.exist?('config/application.yml')cp 'config/application.example.yml', 'config/application.yml'endputs "\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

 

这篇关于rails capistrano 自动化部署的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/802424

相关文章

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

Linux流媒体服务器部署流程

《Linux流媒体服务器部署流程》文章详细介绍了流媒体服务器的部署步骤,包括更新系统、安装依赖组件、编译安装Nginx和RTMP模块、配置Nginx和FFmpeg,以及测试流媒体服务器的搭建... 目录流媒体服务器部署部署安装1.更新系统2.安装依赖组件3.解压4.编译安装(添加RTMP和openssl模块

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

nginx部署https网站的实现步骤(亲测)

《nginx部署https网站的实现步骤(亲测)》本文详细介绍了使用Nginx在保持与http服务兼容的情况下部署HTTPS,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录步骤 1:安装 Nginx步骤 2:获取 SSL 证书步骤 3:手动配置 Nginx步骤 4:测

Tomcat高效部署与性能优化方式

《Tomcat高效部署与性能优化方式》本文介绍了如何高效部署Tomcat并进行性能优化,以确保Web应用的稳定运行和高效响应,高效部署包括环境准备、安装Tomcat、配置Tomcat、部署应用和启动T... 目录Tomcat高效部署与性能优化一、引言二、Tomcat高效部署三、Tomcat性能优化总结Tom

如何在本地部署 DeepSeek Janus Pro 文生图大模型

《如何在本地部署DeepSeekJanusPro文生图大模型》DeepSeekJanusPro模型在本地成功部署,支持图片理解和文生图功能,通过Gradio界面进行交互,展示了其强大的多模态处... 目录什么是 Janus Pro1. 安装 conda2. 创建 python 虚拟环境3. 克隆 janus

本地私有化部署DeepSeek模型的详细教程

《本地私有化部署DeepSeek模型的详细教程》DeepSeek模型是一种强大的语言模型,本地私有化部署可以让用户在自己的环境中安全、高效地使用该模型,避免数据传输到外部带来的安全风险,同时也能根据自... 目录一、引言二、环境准备(一)硬件要求(二)软件要求(三)创建虚拟环境三、安装依赖库四、获取 Dee