好吧,事实证明有很多关于如何为此目的使用 Capistrano 的好信息(包括Prakash 的参考资料),但似乎没有一个是完全全面的。
经过几个小时的挑选指南,论坛之后,堆栈溢出问题之后,我设法实现了我的大部分目标。为了为其他人节省时间,我将尝试提供我在原始问题的上下文中找到的答案和信息。
更新:
事实证明,我一直在寻找Jenkins:它是我之前使用的 CruiseControl 构建服务器应用程序的完美(甚至是改进)模拟。Jenkins 是一个 Web 应用程序,它可以按计划启动构建,或者使用插件启动提交事件。它不包括实际部署我的 Rails 应用程序的功能,因此 Capistrano 介入其中。使用 Jenkins 的“shell 执行”构建任务来触发 Capistrano 部署,我能够实现上述所有目标。
请查看本指南以获取更多详细信息。
原帖:
首先,Capistrano 可以用于构建系统,但是,它与 CruiseControl 完全不同。
巡航控制是:
- 一个 Web 应用程序,在用作“构建服务器”的机器上运行
- 任何用户都可以访问网络应用程序来运行构建;用户无需对其进行任何设置或配置……它只是一个网页。
- 该接口还提供了日志、责备、计划构建等功能...
卡皮斯特拉诺是:
- 不是应用程序或服务,而是像
rake
; 它具有类似于 CruiseControl 用于部署目的的 Ant 和 Nant 脚本的功能。
- Capistrano 在用户的本地计算机(通常是 Rails 应用程序文件夹)上运行,并且需要在其端进行配置才能运行
- 它通过 SSH 直接从用户的机器连接到远程服务器以执行部署(如果您不想授予用户对远程服务器的 SSH 访问权限,这可能不是可取的)
- 命令通过控制台运行,即
cap deploy
(这与“一键式”部署一样接近。)
- 它确实产生日志,执行回滚等。
- 它不会自行启动预定的构建(建议使用 cron)
关于我原来的要求...
将最新提交从我的 GitHub 存储库中的“主”分支部署到我的登台服务器。
配置 Capistrano 的 deploy.rb 文件将完成此操作。
一键按需构建:我想随时点击按钮或链接强制部署(或重新部署)
所有 Capistrano 部署都是通过“强制”完成的:您在控制台中手动运行“cap deploy”
作为部署的一部分,能够在登台服务器上运行自定义命令(即“捆绑安装”、重新启动 Apache 等...)
配置 Capistrano 的 deploy.rb 文件将完成此操作。这些命令中的大多数都是开箱即用的。
每天或在 GitHub 上提交后自动部署(可选)
我还没有弄清楚这个...... cron 工作可能是做到这一点的最佳方式。
设置 Capistrano
首先从GitHub 上的本教程开始。在您的 Rails App 文件夹中,您应该得到一个Capfile
andconfig/deploy.rb
文件。
为了节省您一些时间,请复制并粘贴这些文件,并根据您的需要调整设置。以下文件配置用于:
- 部署到测试环境
- GitHub
- 基于 RVM 的 Rails
- 使用 Ruby 和 gemset 的显式版本
- 包括未跟踪的文件(即
database.yml
您未提交给 SCM 的文件)
- 使用 Rails 资产管道
- 在每次部署时运行迁移和种子
- 每次部署都重新启动基于 Apache 的乘客
头文件
# Set this if you use a particular version of Ruby or Gemset
set :rvm_ruby_string, 'ruby-1.9.3-p286@global'
#set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"") # Read from local system
require "bundler/capistrano"
# Uncomment this if you're using RVM
require "rvm/capistrano"
load 'deploy'
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
load 'config/deploy' # remove this line to skip loading any of the default tasks
配置/部署.rb
# BEGIN RUBY CONFIG
# You can manually override path variables here
# set :default_environment, {
# 'PATH' => "/usr/local/bin:/bin:/usr/bin:/bin:/<ruby-dir>/bin",
# 'GEM_HOME' => '<ruby-dir>/lib/ruby/gems/1.8',
# 'GEM_PATH' => '<ruby-dir>lib/ruby/gems/1.8',
# 'BUNDLE_PATH' => '<ruby-dir>/lib/ruby/gems/1.8/gems'
# }
# This changes the default RVM bin path
# set :rvm_bin_path, "~/bin"
# If your remote server doesn't have a ~/.rvm directory, but is installed
# at the /usr/local/rvm path instead, use this line
set :rvm_type, :system
# END RUBY CONFIG
default_run_options[:pty] = true # Must be set for the password prompt
# from git to work
# BEGIN MULTIPLE ENVIRONMENT DEPLOYS
# Read the following URL if you need to deploy to different environments (test, development, etc)
# https://github.com/capistrano/capistrano/wiki/2.x-Multistage-Extension
# set :stages, %w(production test)
# set :default_stage, "test"
# require 'capistrano/ext/multistage'
# END MULTIPLE ENVIRONMENT DEPLOYS
# BEGIN APPLICATION VARS
set :application, "yourapp"
set :rails_env, 'test'
# END APPLICATION VARS
# BEGIN PATH DEFINITIONS
set(:releases_path) { File.join(deploy_to, version_dir) }
set(:shared_path) { File.join(deploy_to, shared_dir) }
set(:current_path) { File.join(deploy_to, current_dir) }
set(:release_path) { File.join(releases_path, release_name) }
# END PATH DEFINITIONS
# BEGIN SCM VARS
set :repository, "git@github.com:yourgithubuser/yourrepository.git" # Your clone URL
set :scm, "git"
set :scm_username, "yourgithubuser"
set :scm_password, proc{Capistrano::CLI.password_prompt('GitHub password:')} # The deploy user's password
set :branch, "master"
# END SCM VARS
# BEGIN SERVER VARS
set :user, "ubuntu" # The server's user for deploys
role :web, "dev.#{application}" # The location of your web server i.e. dev.myapp.com
role :app, "dev.#{application}" # The location of your app server i.e. dev.myapp.com
role :db, "dev.#{application}", :primary => true # The location of your DB server i.e. dev.myapp.com
set :deploy_to, "/home/#{user}/www/#{application}"
set :deploy_via, :remote_cache
# Uncomment this if you want to store your Git SSH keys locally and forward
# Else, it uses keys in the remote server's .ssh directory
# ssh_options[:forward_agent] = true
# END SERVER VARS
# BEGIN ADDITIONAL TASKS
before "deploy:start" do
deploy.migrate
deploy.seed
end
before "deploy:restart" do
deploy.migrate
deploy.seed
end
# Some files that the Rails app needs are too sensitive to store in SCM
# Instead, manually upload these files to your <Rails app>/shared folder
# then the following code can be used to generate symbolic links to them,
# so that your rails app may use them.
# In this example below, I link 'shared/config/database.yml' and 'shared/db/seed_data/moderators.csv'
before "deploy:assets:precompile" do
run ["ln -nfs #{shared_path}/config/settings.yml #{release_path}/config/settings.yml",
"ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml",
"mkdir -p #{release_path}/db/seed_data",
"ln -nfs #{shared_path}/db/seed_data/moderators.csv #{release_path}/db/seed_data/moderators.csv",
"ln -fs #{shared_path}/uploads #{release_path}/uploads"
].join(" && ")
end
namespace :deploy do
# Define seed task (call 'cap deploy:seed')
desc "Reload the database with seed data"
task :seed do
run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}"
end
# If you are using Passenger mod_rails uncomment this:
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
# END ADDITIONAL TASKS