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

相关文章

如何在Ubuntu 24.04上部署Zabbix 7.0对服务器进行监控

《如何在Ubuntu24.04上部署Zabbix7.0对服务器进行监控》在Ubuntu24.04上部署Zabbix7.0监控阿里云ECS服务器,需配置MariaDB数据库、开放10050/1005... 目录软硬件信息部署步骤步骤 1:安装并配置mariadb步骤 2:安装Zabbix 7.0 Server

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

pytest+allure环境搭建+自动化实践过程

《pytest+allure环境搭建+自动化实践过程》:本文主要介绍pytest+allure环境搭建+自动化实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、pytest下载安装1.1、安装pytest1.2、检测是否安装成功二、allure下载安装2.

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

Web技术与Nginx网站环境部署教程

《Web技术与Nginx网站环境部署教程》:本文主要介绍Web技术与Nginx网站环境部署教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Web基础1.域名系统DNS2.Hosts文件3.DNS4.域名注册二.网页与html1.网页概述2.HTML概述3.

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

ubuntu如何部署Dify以及安装Docker? Dify安装部署指南

《ubuntu如何部署Dify以及安装Docker?Dify安装部署指南》Dify是一个开源的大模型应用开发平台,允许用户快速构建和部署基于大语言模型的应用,ubuntu如何部署Dify呢?详细请... Dify是个不错的开源LLM应用开发平台,提供从 Agent 构建到 AI workflow 编排、RA