codebase4 gem过时了(仅限Capistrano 2),我想知道如何使用Capistrano 3,因为它取消了配方并改为使用任务。
答案 0 :(得分:1)
这项任务非常适合与gem结合使用:
namespace :codebase do
desc "Logs the deployment of your Codebase 4 repository"
task :log_deployment do
previous_revision = fetch :previous_revision
current_revision = fetch :current_revision
if previous_revision == current_revision
puts "\e[31m The old revision & new revision are the same - you didn't deploy anything new. Skipping logging.\e[0m"
next
end
cmd = ["cb deploy #{previous_revision or "0000000000000000000000000000000000000000"} #{current_revision}"]
if respond_to?(:environment)
set :environment, environment
elsif respond_to?(:rails_env)
set :environment, rails_env
end
branch = fetch :branch
roles = fetch :roles
stage = fetch :stage
app = fetch :application
cmd << "-s #{app}"
cmd << "-b #{branch}"
cmd << "-e #{stage}"
## get the repo and project name etc...
account, project, repo = nil, nil, nil
case fetch(:repo_url)
when /git\@codebasehq.com\:(.+)\/(.+)\/(.+)\.git\z/
account, project, repo = $1, $2, $3
when /ssh:\/\/.+\@codebasehq.com\/(.+)\/(.+)\/(.+)\.hg\z/
account, project, repo = $1, $2, $3
when /https?:\/\/.+\@(.+)\.codebasehq.com\/(.+)\/(.+)\.(?:hg|svn)\z/
account, project, repo = $1, $2, $3
when /https?:\/\/(?:.+\@)?(.+)\.svn\.codebasehq.com\/(.+?)\/(.+?)(?:\/.*)\z/
account, project, repo = $1, $2, $3
else
puts "! Repository path not supported by deployment logging"
next
end
cmd << "-r #{project}:#{repo}"
cmd << "-h #{account}.codebasehq.com"
cmd << "--protocol https"
run_locally do
execute cmd.join(' ') + "; true"
end
end
end
示例部署:
namespace :deploy do
after 'deploy:symlink:release', 'codebase:log_deployment'
end
如果您正在运行已删除先前修订版的Capistrano版本。
将此添加到您部署文件或添加为另一个自定义任务文件:
on roles(:app) do
within current_path do
revision = capture "cat #{current_path}/REVISION"
set :previous_revision, revision
end
end
然后正常修改版本。
previous_revision = fetch :previous_revision
current_revision = fetch :current_revision
我的Ruby目前有点生疏,所以如果可以巩固/改进,请告诉我,谢谢。
答案 1 :(得分:0)
codebase4
gem已更新为包含Capistrano 3版本的脚本。
以下为我工作(在Rails 4项目上测试)。
在Capfile中,添加以下内容:
require 'capistrano/git'
require 'codebase/capistrano3'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
Dir.glob('lib/capistrano/**/*.rb').each { |r| import r }
在部署脚本中的某处添加以下任务。我把它放在一个名为lib/capistrano/tasks/git.cap
的单独文件中。
namespace :git do
desc 'Determine the previous revision that was deployed'
task :set_previous_revision do
on release_roles :all do
within repo_path do
with fetch(:git_environmental_variables) do
set :previous_revision, strategy.fetch_revision
end
end
end
end
before 'git:update', 'git:set_previous_revision'
end
就是这样。
我一直试图在Rails 3项目上进行这项工作很长一段时间,但出于某种原因,到current_revision
和previous_revision
变量为fetch
时(在部署过程结束时,执行部署跟踪代码时,变量不存在;他们返回空白字符串。
相同的代码在Rails 4项目上完美运行。我不确定在部署跟踪代码运行之前是否发生了清除这两个变量的事情。