Deployment 使用Capistrano 3在部署时更新codebasehq.com

Deployment 使用Capistrano 3在部署时更新codebasehq.com,deployment,capistrano3,Deployment,Capistrano3,codebase4 gem已经过时(仅限Capistrano 2),我想知道如何让它与Capistrano 3配合使用,因为它不再使用配方,而是使用任务。此任务与gem配合使用时效果非常好: namespace :codebase do desc "Logs the deployment of your Codebase 4 repository" task :log_deployment do previous_revision = fetch :previous_revis

codebase4 gem已经过时(仅限Capistrano 2),我想知道如何让它与Capistrano 3配合使用,因为它不再使用配方,而是使用任务。

此任务与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
如果您运行的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目前有点生锈,因此如果可以整合/改进,请告诉我,谢谢。

已更新
codebase4
gem,以包含Capistrano 3版本的脚本

以下内容对我很有用(在Rails4项目上进行了测试)

在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项目上完成这项工作,但由于某种原因,当
当前版本
上一版本
变量被
提取时(在部署过程结束时,当执行部署跟踪代码时),变量不存在;它们返回空白字符串


同样的代码在Rails4项目上完美地工作。我不确定在部署跟踪代码运行之前是否发生了清除这两个变量的事件。

这真的有效吗?对我来说,
fetch:previous_revision
fetch:current_revision
返回空字符串。是的,我们在所有网站上都使用了此选项,没有问题。它可能是您正在使用的capistrano版本?目前我们锁定在3.2.1。这可能是问题所在。在较新版本的Capistrano中,已删除以前的修订标签功能。谢谢你的更新。如果你弄明白了,请在这里发布。如果没有,我今天晚些时候会尝试看这个,然后做同样的事情。请看上面的编辑,我刚刚测试了它,它在3.4.0上记录了部署。
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