Capistrano creating task to change current symlink

André Figueira picture André Figueira · Feb 17, 2014 · Viewed 8.5k times · Source

I am trying to setup Capistrano deployment for my website, I have got the deploy working, all authentication fine, but I host with Media temple so the default symlink will not work as apache won't have access to the path specified for current to the latest release.

I am trying to make a task that will unlink the current symlink, then recreate it with a relative path, however the Capistrano documentation is severely lacking and I cannot find any more information anywhere of how exactly to set this up.

I have tried using sh, but that seems to run the commands on my computer rather than on the server, run command is not found, and I tried execute but cannot find the right format to do things like rm, or ln, etc...

Currently I am trying the following:

namespace :deploy do

  desc "Change HTML Symlink to relative path"
  task :create_symlink do
    #latest_release_relative = relative_path(deploy_to, release_path + '/html')
    #sh "rm -f #{current_path} && ln -s #{latest_release_relative} #{current_path}"
    #sh "echo #{File.basename release_path}"
    info "echo Modifying symlink to be relative"
    #run "rm -d #{current_path}"
    #run "ln releases/#{File.basename release_path} #{current_path}"
    #execute :rm, '-d', #{current_path}
  end

  desc "Create environment file"
  task :create_env_conf
  file 'env.conf' do |t|
    sh "touch env.conf"
  end

end

after :deploy, "deploy:create_symlink", "deploy:create_env_conf"

Answer

André Figueira picture André Figueira · Feb 17, 2014

After a huge amount of trial and error, I found that the issue was that;

  1. Need to use execute in cap v3

  2. Need to cd then chain command with && for running directory specific commands

  3. Capistrano needs more documentation...

This is how I got mine working, I think there is a better way of doing it with Capistrano 3 but I could not find adequate documentation describing how anywhere.

#config valid only for Capistrano 3.1
lock '3.1.0'

set :application, 'prism-credentials'
set :repo_url, 'REPO URL'

# Default deploy_to directory is /var/www/my_app
set :deploy_to, 'DEPLOY FOLDER'

# Default value for keep_releases is 5
set :keep_releases, 5

set :branch, "master"
if ENV['branch']
    set :branch, ENV['branch']
end

namespace :deploy do

  desc "Change HTML Symlink to relative path"
  task :create_symlink do
    on roles(:app) do

        #execute "ls -l"
        info "Modifying symlink to be relative"
        execute "rm -d #{current_path}"

        info "Deleted current symlink"
        execute "cd ../DEPLOY FOLDER && ln -s ./releases/#{File.basename release_path} current"
        info "Created relative current symlink"

        execute "cd ~/../DEPLOY FOLDER && touch env.conf && echo 'live' >> env.conf"
        info "Created environment file"

    end
  end

end

after :deploy, "deploy:create_symlink"