Capistrano 3 / SSHKit write to a file in custom task

3.7k views Asked by At

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 ?

3

There are 3 answers

0
astropanic On

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
0
Shaun McDonald On

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
0
Pratik Kapasi On

Use SSHKit::Command

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