1

I am trying to define an environment variable in /config/deploy/staging.rb like :

set :default_env, { 
  'environment' => 'preprodv1'
}

Then I am trying to acces this variable in my file /lib/capistrano/tasks/build.rake like :

desc "Builds the admin front-end"
    task :build_admin_front do
        on roles (:all) do |host|
            within "#{release_path}" do
                execute "cd #{release_path}/front_admin && npm install && npm run-script build --env=#{fetch(:environment)}"
            end
        end
    end
end

But I got this error : undefined local variable or method `environment'

Do you please have any idea of the reason why? Can't I access to my environment variable set in rb file from my rake file ?

Thanks a lot for your help!

Zoe Edwards
  • 12,999
  • 3
  • 24
  • 43
  • I don't think `fetch` will access the environment (although I could be wrong). To access an environment variable, `ENV['environment']` should work. In order to get `fetch(:environment)` working, you need `set :environment, ...` – Stefan Mar 04 '19 at 14:55

3 Answers3

0

Is it because you’ve set the variable as :default_env, but you’re asking for it as :environment?

Zoe Edwards
  • 12,999
  • 3
  • 24
  • 43
  • I'm not sure of the syntax, but when i read the documentation, I see that I can only set some defined variables : [documentation](https://capistranorb.com/documentation/getting-started/configuration/). But I am not sure how to access to default_env variables, do you think that I should try something like `#{fetch(:default_env.environment)}` ? I havn't found the correct syntax in the documentation – Bruno Cambianica Mar 04 '19 at 14:34
  • I’m pretty sure you’re doing it right looking at the docs. Very odd. – Zoe Edwards Mar 04 '19 at 14:41
  • I am gonna keep looking for a solution, thank you for your help @Thomas – Bruno Cambianica Mar 04 '19 at 14:49
0

Thank you for your answer, I finally got it fixed by using set :environment, "preprodv1" Instead of set :default_env, { 'environment' => 'preprodv1' } And by keeping #{fetch(:environment)} Thanks a lot for your help :)

0
fetch(:default_env)['environment']
Davinj
  • 327
  • 4
  • 16
  • I think that I was mixing two syntaxes : `set :default_env, { 'environment' => 'preprodv1' }` is accessible with : `fetch(:default_env)['environment']` and `set :environment` is accessible with : `fetch(:environment)` – Bruno Cambianica Mar 05 '19 at 11:00