【问题标题】:Tagging release before deploying with Capistrano在使用 Capistrano 部署之前标记发布
【发布时间】:2023-04-04 19:45:01
【问题描述】:

我正在寻找一个不错的 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

【问题讨论】:

    标签: ruby git capistrano


    【解决方案1】:

    我用 Capistrano 做过类似的事情。最简单的做法是使用 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
    

    【讨论】:

    • 好吧,你去...像往常一样,我的问题的解决方案是一种完全不同的方法。呵呵 干得好。问题,但是......我仍然可以在部署之前这样做吗?
    • @TheOddLinguist 我不确定。这取决于何时设置release_name。您可能必须筛选 Capistrano 代码才能找到答案。
    • 这非常有用。这是cap3 version。它有点长,因为 cap3 本身不提供 release_namecurrent_revision
    【解决方案2】:

    只需添加您正在构建的提交的简短哈希。

    git log -1 --format=%h
    

    【讨论】:

      【解决方案3】:

      感谢@dunedain289 的精彩回答。我更进一步尝试为 capistrano 复制 heroku 版本。我需要一个很好的工具来了解服务器上已经部署的内容,并与我当前的本地分支进行比较:

      1. 首先,使用@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
        
      2. 添加一个任务来过滤发布标签并区分最后一个

        task :diff do
          stage = "production" unless stage
          last_stage_tag = `git tag -l #{stage}* | tail -1`
          system("git diff #{last_stage_tag}", out: $stdout, err: :out)
        end
        
        #this preserves coloring if you have a .gitconfig with 
        [color "diff"]
          meta = yellow bold
          frag = magenta bold
          old = red bold
          new = green bold
        
      3. 执行

        $ cap diff
        # awesome colored diff of production and local
        

      【讨论】:

      【解决方案4】:

      关于您在代码中关于如何通过 Capistrano 输出文本的问题。只需将 p 更改为 puts 即可。

      【讨论】:

      • 对不起,我写这篇文章时没有权限发布 cmets。我认为作为答案存在总比无处存在要好。我是否应该完全删除它并转到评论,即使它已经投票了?
      • 酷,np!权限允许 SO 的预期用途是多么可悲!
      【解决方案5】:

      这个已接受答案的更新版本对我有用:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-27
        • 1970-01-01
        • 2010-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-01
        相关资源
        最近更新 更多