Search code examples
rubyvariablestaskcapistranoenvironment

capistrano set and get environment variables from config to rake


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!


Solution

  • 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 :)