【问题标题】:Capistrano 3 / SSHKit write to a file in custom taskCapistrano 3 / SSHKit 在自定义任务中写入文件
【发布时间】:2014-01-20 21:48:04
【问题描述】:

我想用我的版本号标记当前部署的目录。

我试过这种方法:

在本地获取应用版本,将其存储到变量中,然后在远程主机上,将其存储在文件中。

namespace :deploy do
  desc "Set a release number as the app version"
  task :mark_release do
    release_number = `git describe`
      on roles(:web) do
        execute("echo #{release_number} > #{current_path}/RELEASE")
      end
    end
  end

问题是,当我通过以下方式运行它时:

cap deploy:mark_release

命令如下所示:

echo v9.3.0-254-g178d1f8; > /foo/bar/current/RELEASE

分号惹麻烦了。我的 RELEASE 文件当然是空的。

我认为这是由于 SSHKit 进行了一些转义。

有什么线索吗?

【问题讨论】:

  • 你需要用引号转义版本号 execute("echo \"#{release_number}\" > #{current_path}/RELEASE")

标签: ruby git deployment capistrano3 sshkit


【解决方案1】:

我做到了:

1) 我从机器上的 repo 目录中获取了版本号

2)我通过上传将它用流写入文件!方法

namespace :deploy do
 desc "Set a release number as the app version"
 task :mark_release do
   on roles(:web) do
     within "/foo/bar/repo/" do
       upload! StringIO.new(capture(:git, "describe")), "#{current_path}/RELEASE"
     end
   end
 end
end

【讨论】:

    【解决方案2】:

    这是我提出的不需要上传本地文件的解决方案。它转到 repo 路径以执行 git 命令以提取版本,然后将输出重定向到文件。然后Rails 应用程序可以读取该文件。执行需要分别传入不同的参数。 https://github.com/capistrano/sshkit#the-command-map 有更多关于命令映射的信息以及为什么需要它,因为转义和空格的问题。

    namespace :deploy do
      before :restart, :add_revision_file
      task :add_revision_file do
        on roles(:app) do
          within repo_path do
            execute(:git, :'rev-parse', :'--short', :'HEAD', ">#{release_path}/REVISION")
          end
        end
      end
    end
    

    【讨论】:

      【解决方案3】:

      使用SSHKit::Command

      SSHKit::Command.new("echo #{release_number} > #{current_path}/RELEASE")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-12
        • 1970-01-01
        • 1970-01-01
        • 2011-05-24
        • 2016-11-08
        • 1970-01-01
        • 2014-03-30
        相关资源
        最近更新 更多