Capistrano 3 / SSHKit write to a file in custom task
Asked Answered
R

3

7

I want mark the current deployed directory with my release number.

I tried this approach:

Get locally the app version, store it into a variable, and on the remote host, store it in a file.

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

The problem is, when I run this via:

cap deploy:mark_release

the command look like this:

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

The semicolon is making trouble. and my RELEASE file is of course empty.

I think it is due to some escaping made by SSHKit.

Any clues ?

Rauch answered 3/1, 2014 at 9:5 Comment(1)
You need escape release number with quotes execute("echo \"#{release_number}\" > #{current_path}/RELEASE")Gilli
R
4

I managed it:

1) I took the release number from the repo directory on the machine

2) I wrote it with a stream to a file via the upload! method

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
Rauch answered 3/1, 2014 at 11:46 Comment(0)
S
3

Here is the solution that I came up with which doesn't require uploading a local file. It goes to the repo path to execute the git command to extract the version and then redirects the output to file. The file can then be read by the Rails app. The execute requires the different parameters to be passed in separately. https://github.com/capistrano/sshkit#the-command-map has more info about the command map and why it's needed, due to the problem of escaping and whitespace.

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
Schlegel answered 23/2, 2014 at 23:21 Comment(0)
B
0

Use SSHKit::Command

SSHKit::Command.new("echo #{release_number} > #{current_path}/RELEASE")
Bench answered 10/6, 2017 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.