1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00

Add FAQ entry about how to lazily load configuration variables from a stage file in deploy.rb

This commit is contained in:
John Manuel 2014-06-29 15:21:20 -07:00
parent ca46cdff57
commit 36a4552eaf
2 changed files with 37 additions and 1 deletions

View file

@ -41,7 +41,8 @@
<h5>FAQ</h5>
<li><a href="/documentation/faq/why-does-something-work-in-my-ssh-session-but-not-in-capistrano/">Why Does Something Work In An SSH Session, But Not In Capistrano?</a></li>
<li><a href="/documentation/faq/how-can-i-get-capistrano-to-prompt-for-a-password/">How can I get Capistrano to prompt for a password?</a></li>
<!--<li><a href="/documentation/faq/should-i-use-capistrano-to-provision-my-servers/">Should I Use Capistrano To Provision My Servers?</a></li>-->
<li><a href="/documentation/faq/how-can-i-access-stage-configuration-variables/">How can I access stage configuration variables?</a></li>
<!--<li><a href="/documentation/faq/should-i-use-capistrano-to-provision-my-servers/">Should I Use Capistrano To Provision My Servers?</a></li>-->
<li><a href="http://lee.hambley.name/2013/06/11/using-capistrano-v3-with-chef.html">Should I Use Capistrano To Provision My Servers?</a></li>
<!--<li class="divider"></li> -->
<!--<h5>Power Use-Cases</h5> -->

View file

@ -0,0 +1,35 @@
---
title: How can I access stage configuration variables?
layout: default
---
Configuration variables are access with the fetch method, like so:
{% highlight ruby %}
local = fetch(:configuration_variable, _default_value_)
{% endhighlight %}
This works fine when accessing configuration variables defined within the same file. For example accessing a previously set configuration variable defined in deploy.rb or accessing a set configuration variable in a stage file.
The deploy.rb configuration is executed first and then the stage file(s) from config/deploy/*.rb are executed next. This means that the configuration variables set in deploy.rb are available to the stage files, but configuration variables created in a stage file are not available in deploy.rb. To access them they must be lazily loaded in deploy.rb. This works because all configuration variables (from both deploy.rb and the current stage file) have been defined by the time the tasks run and access the variables.
For example, let's create a configuration variable in the production and staging files and access the current one from deploy.rb.
config/deploy/production.rb
{% highlight ruby %}
set :app_domain, "www.my_application.com"
{% endhighlight %}
config/deploy/staging.rb
{% highlight ruby %}
set :app_domain, "stage.application_test.com"
{% endhighlight %}
These variables are not available in deploy.rb using fetch(:nginx_port) or fetch(:app_domain) because they are not defined when deploy.rb is executed. They can, however, be lazily loaded using a lambda in deploy.rb like this:
config/deploy.rb
{% highlight ruby %}
set :nginx_server_name, ->{ fetch(:app_domain) }
set :puma_bind, ->{ "unix:/tmp/#{fetch(:app_domain)}.sock" }
{% endhighlight %}
Now the `:nginx_server_name` and `:puma_bind` variables will be lazily assigned the values set in which ever stage file was used to deploy.