Ruby 在使用Capistrano部署之前标记发布

Ruby 在使用Capistrano部署之前标记发布,ruby,git,capistrano,Ruby,Git,Capistrano,我正在寻找一个很好的Capistrano方法来部署Git版本控制的站点 除了我正在添加的一些其他内容外,我的第一个任务是用当前日期标记当前版本…当该标记已经存在时(例如,一天内有多个版本),添加一封信 我已经编写了一些工作代码,并在我的production.rb(在capistrano ext中使用多级)中对其进行了测试……但我不得不认为我本可以编写得更好。首先,在实际检查标签是否存在时,会有一些巨大的重复。然而,无论我移动东西的顺序如何,这是唯一产生结果的配置 有什么想法吗?提前谢谢 befo

我正在寻找一个很好的Capistrano方法来部署Git版本控制的站点

除了我正在添加的一些其他内容外,我的第一个任务是用当前日期标记当前版本…当该标记已经存在时(例如,一天内有多个版本),添加一封信

我已经编写了一些工作代码,并在我的production.rb(在capistrano ext中使用多级)中对其进行了测试……但我不得不认为我本可以编写得更好。首先,在实际检查标签是否存在时,会有一些巨大的重复。然而,无论我移动东西的顺序如何,这是唯一产生结果的配置

有什么想法吗?提前谢谢

before 'deploy' do
  # Tag name is build_YYYYMMDD
  tag_name = "build_#{Time.now.strftime('%Y%m%d')}"
  check_tag = `git tag -l #{tag_name}`
  # If the tag exists, being appending letter suffix
  if not check_tag.empty?
    suffix = 'a'
    check_tag = `git tag -l #{tag_name}#{suffix}`
    while not check_tag.empty? do
      suffix.next!
      check_tag = `git tag -l #{tag_name}#{suffix}`
    end
    tag_name = "#{tag_name}#{suffix}"
  end
  # Tag with computed tag name
  p "Tagging #{tag_name}" # TODO How to output via Capistrano?
  system "git tag #{tag_name}"
  # Push tags to origin remote
  p "Pushing tag to origin" # TODO How to output via Capistrano?
  system "git push origin master --tags"
end

我对卡皮斯特拉诺也做过类似的事情。最简单的方法是使用Capistrano在部署期间使用的时间戳名称(即YYYYMMDDHHMMSS)进行标记,因此很难获得重复的名称

例如:

task :push_deploy_tag do
  user = `git config --get user.name`.chomp
  email = `git config --get user.email`.chomp
  puts `git tag #{stage}_#{release_name} #{current_revision} -m "Deployed by #{user} <#{email}>"`
  puts `git push --tags origin`
end
task:push\u deploy\u标记do
user=`git config--get user.name`.chomp
email=`git config--get user.email`.chomp
将'git tag#{stage}#{release_name}#{current_revision}-m“由#{user}部署”`
将`git push--tags放在原点`
结束

只需添加要从中生成的提交的短散列即可

git log -1 --format=%h

关于代码中关于如何通过Capistrano输出文本的问题。只需将p更改为puts,它就会显示良好。

感谢@289提供了一个很好的答案。我进一步尝试为capistrano复制heroku版本。我需要一个好的工具来了解服务器上已经部署了什么,并与我当前的本地分支进行比较:

  • 首先,使用@dunedain289的代码,稍微修改一下,适合我

    task :push_deploy_tag do
       user = `git config --get user.name`.chomp
       email = `git config --get user.email`.chomp
       stage = "production" unless stage # hack, stage undefined for me
       puts `git tag #{stage}_#{release_name} -m "Deployed by #{user} <#{email}>"`
       puts `git push --tags origin`
     end
    
  • 执行

    $ cap diff
    # awesome colored diff of production and local
    

  • 这个被接受答案的更新版本对我来说很有用:

    desc 'Tag the deployed revision'
    task :push_deploy_tag do
      env = fetch(:rails_env)
      timestamp = fetch(:release_timestamp)
      user = `git config --get user.name`.chomp
      email = `git config --get user.email`.chomp
    
      puts `git tag #{env}-#{timestamp} #{fetch :current_revision} -m "Deployed by #{user} <#{email}>"`
      puts `git push --tags origin`
    end
    
    desc'标记已部署的修订版'
    任务:push_deploy_标记do
    env=fetch(:rails\u env)
    timestamp=fetch(:release\u timestamp)
    user=`git config--get user.name`.chomp
    email=`git config--get user.email`.chomp
    将'git tag{env}-{timestamp}{fetch:current_revision}-m“由{user}部署”`
    将`git push--tags放在原点`
    结束
    
    好了,就这样……像往常一样,我的问题的解决方案是完全不同的方法。呵呵,干得好。但问题是……在部署之前我还能这样做吗?@Theodlinguist我不确定。这取决于何时设置了
    release\u name
    。您可能需要筛选Capistrano代码才能找到答案。这非常有用。这是一个例子。它有点长,因为cap3没有提供
    release\u name
    current\u revision
    本身。很抱歉,我写这篇文章时没有发表评论的权限。我认为作为一个答案存在总比什么都没有好。我是否应该完全删除它,并转移到一个评论,即使它有投票权?酷,np!权限允许所需的SO使用,这是多么可悲啊!我建议使用此Gem来处理已部署与即将部署之间的“git日志”和“git差异”:
    desc 'Tag the deployed revision'
    task :push_deploy_tag do
      env = fetch(:rails_env)
      timestamp = fetch(:release_timestamp)
      user = `git config --get user.name`.chomp
      email = `git config --get user.email`.chomp
    
      puts `git tag #{env}-#{timestamp} #{fetch :current_revision} -m "Deployed by #{user} <#{email}>"`
      puts `git push --tags origin`
    end