Variables set in "deploy.rb" can't be fetched in "staging files" and "task files" #1519

Closed
Oliboy50 opened this Issue Oct 29, 2015 · 3 comments

Projects

None yet

2 participants

@Oliboy50
Contributor

Everything is in the title.

Since, I didn't find anything about this huge problem, I presume this is due to a mistake on my side.

Here is my whole configuration:

Ubuntu 14.04
foo@bar:~$ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
foo@bar:~$ cap -v
Capistrano Version: 3.4.0 (Rake Version: 10.4.2)

Capfile

set :deploy_config_path, 'capistrano/deploy.rb'
set :stage_config_path, 'capistrano/environments'

require 'capistrano/setup'

require 'capistrano/deploy'

Dir.glob('capistrano/tasks/*.cap').each { |r| import r }

capistrano/deploy.rb

lock '3.4.0'

set :application, 'FooBarProject'
set :keep_releases, 3

set :scm, :git
set :repo_url, 'git@github.com:Something/FooBarProject.git'

set :linked_dirs, fetch(:linked_dirs, []).push('build', 'app/logs', 'web/uploads')
set :linked_files, fetch(:linked_files, []).push('app/config/parameters.yml')

capistrano/environments/staging.rb

set :stage, :staging

set :ssh_user, 'oliboy25'
set :tmp_dir, -> { "/home/#{fetch(:ssh_user)}/tmp/capistrano" }

set :branch, 'master'
set :deploy_to, -> { "/var/www/html/#{fetch(:application)}" }

server 'my-own-domain.com',
  roles: %w{web app db},
  user: fetch(:ssh_user),
  ssh_options: {
    auth_methods: %w(publickey),
    keys: %w(staging/id_rsa)
  }

capistrano/tasks/check.cap

desc "Check that we can access everything"
task :check_write_permissions do
  on roles(:all) do |host|
    if test("[ -w #{fetch(:deploy_to)} ]")
      info "#{fetch(:deploy_to)} is writable on #{host}"
    else
      error "#{fetch(:deploy_to)} is not writable on #{host}"
    end
  end
end

Output (when using task with this configuration)

foo@bar:~$ cap staging check_write_permissions
DEBUG [dba57f80] Running /usr/bin/env [ -w /var/www/html/ ] as oliboy25@my-own-domain.com
DEBUG [dba57f80] Command: [ -w /var/www/html/ ]
DEBUG [dba57f80] Finished in 0.598 seconds with exit status 0 (successful).
INFO /var/www/html/ is writable on my-own-domain.com

Output (when I put everything in the staging file "capistrano/environments/staging.rb")

foo@bar:~$ cap staging check_write_permissions
DEBUG [dba57f80] Running /usr/bin/env [ -w /var/www/html/FooBarProject ] as oliboy25@my-own-domain.com
DEBUG [dba57f80] Command: [ -w /var/www/html/FooBarProject ]
DEBUG [dba57f80] Finished in 0.543 seconds with exit status 0 (successful).
INFO /var/www/html/ is writable on my-own-domain.com

I wonder if this happen because I changed the deploy_config_path... anyway, every variables I set in my deploy.rb file seems to be completely ignored...

Any ideas?

@mattbrictson
Member

Yes, this is a bit tricky.

The problem is that :deploy_config_path must be an absolute path. Otherwise it will search in Ruby's default load path. So when you write this:

set :deploy_config_path, 'capistrano/deploy.rb'

You are actually instructing capistrano to load its own deploy.rb. In other words, it's as if you had written:

require "capistrano/deploy"

The solution is to pass an absolute path, which you can do in your Capfile like this:

set :deploy_config_path, File.expand_path('capistrano/deploy.rb')
@Oliboy50
Contributor

Great, it works just fine now. Thank you @mattbrictson

This should be in the doc :)

I keep this open until it's done. Feel free to close it :)

@mattbrictson
Member

Could you open an issue for the missing documentation in the capistrano/documentation repository here?: https://github.com/capistrano/documentation/issues (this is the repo for the capistranorb.com site)

If you want to submit a PR to add the docs that would also help us a lot! 😃

@Oliboy50 Oliboy50 referenced this issue in capistrano/documentation Oct 31, 2015
Merged

Add FAQ "how to change config paths?" #141

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment