Skip to content
This repository
Browse code

Finished writing about variables, there wasn't much to say\!

  • Loading branch information...
commit 2fa2a3c06dc149b20fb1cbfa383d172bcef1ccbe 1 parent feaecd5
Lee Hambley authored

Showing 1 changed file with 69 additions and 2 deletions. Show diff stats Hide diff stats

  1. 71  index.markdown
71  index.markdown
Source Rendered
@@ -305,14 +305,16 @@ The Multi-Stage Extension is designed for deploying the same application to mult
305 305
 
306 306
 The Multi-Stage Extension may be implementing something like the following internally:
307 307
 
  308
+    set :application, 'example-website'
  309
+
308 310
     task :production do
309  
-      set :deploy_to, "/u/apps/#{application}_production/"
  311
+      set :deploy_to, "/u/apps/#{application}-production/"
310 312
       set :deploy_via, :remote_cache
311 313
       after('deploy:symlink', 'cache:clear')
312 314
     end
313 315
     
314 316
     task :staging do
315  
-      set :deploy_to, "/u/apps/#{application}_staging/"
  317
+      set :deploy_to, "/u/apps/#{application}-staging/"
316 318
       set :deploy_via, :copy
317 319
       after('deploy:symlink', 'cruise_control:build')
318 320
     end
@@ -354,3 +356,68 @@ Before `deploy:symlink` is run, the only thing required to roll-back the changes
354 356
 In the `deploy:update_code` example, only one step is needed to undo the *damage* done by the failed task, for `deploy:symlink` there is a little more to it, and in this example this is implemented using the `do..end` block syntax also using a [heredoc](http://en.wikipedia.org/wiki/Heredoc#Ruby) to pass a multi-line string to the run() command, in this instance, as you can see it removes the `current` symlink and replaces it with one to the `previous_release`.
355 357
 
356 358
 If your roll-back logic was any more complicated than that, you may consider including a rake task with your application with some kind of rollback task that you can invoke to keep the deployment simple.
  359
+
  360
+#### Variables
  361
+
  362
+Capistrano has its own variable mechanism built in, you will not in the default `deploy.rb` that `capify` generates most of the variable assignment is done in the following manner:
  363
+
  364
+    set :foo, 'bar'
  365
+
  366
+As [`set`](http://wiki.capify.org/index.php/Set) is quite a complex function, we will only brush the surface here.
  367
+
  368
+Here are a few things to note:
  369
+
  370
+    set :username, 'Capistrano Wizard'
  371
+    
  372
+    task :say_username do
  373
+      puts "Hello #{username}"
  374
+    end
  375
+
  376
+Note that we have a *real* ruby variable to use in our string interpolation, having used the Capistrano specific `set` method to declare, and assign to it.
  377
+
  378
+One of the key benefits to using the `set` method is that it makes the resulting variable available anywhere inside the Capistrano environment, as well as being able to assign complex objects such as Procs to variables for delayed processing.
  379
+
  380
+Set has a partner function [`fetch`](http://wiki.capify.org/index.php/Set) that functions similarly except that it is for retrieving previously `set` variables.
  381
+
  382
+In addition, there is [`exists?`](http://wiki.capify.org/index.php/Exists%3F) which can be used to check whether a variable exists at all; this might be used to implement a solution to the *missing stage* problem we left unresolved in the **Tasks** section:
  383
+
  384
+    before :deploy do
  385
+      unless exists?(:deploy_to)
  386
+        raise "Please invoke me like `cap stage deploy` where stage is production/staging"
  387
+      end
  388
+    end
  389
+
  390
+For convenience Capistrano's internals use a method called `_cset` which is designed to non-destructively set variables, it is implemented using `exists?` and `set`, take a look:
  391
+
  392
+    def _cset(name, *args, &block)
  393
+      unless exists?(name)
  394
+        set(name, *args, &block)
  395
+      end
  396
+    end
  397
+
  398
+This can be used without you having to redefine it to set a variable, only in the event that it hasn't already been set. If you need to change the value of a variable, please just use `set`.
  399
+
  400
+Part of the argument list to `set` is a `&block`, these can be used to lazy-set a variable, or compute it at runtime... take a look:
  401
+
  402
+    set :application, 'example-website'
  403
+    set :deploy_to, { "/u/apps/#{application}-#{stage}" }
  404
+    
  405
+    task :production do
  406
+      set :stage, 'production'
  407
+    end
  408
+    
  409
+    task :staging do
  410
+      set :stage, 'staging'
  411
+    end
  412
+
  413
+Note that on the second line of the example the `stage` variable doesn't exist, and were Capistrano to evaluate this inline, an exception would be raised.
  414
+
  415
+However, as the `deploy_to` variable isn't used until further through the deployment process, in `deploy:update`, which we know when invoked with `cap production deploy` will run after the `production` task has defined the `stage` variable, the block that is assigned to `:deploy_to` won't be evaluated until then; this is often used by people who wish to have Capistrano ask for their passwords at deploy-time, rather than commit them to the source repository, for example:
  416
+
  417
+    set(:user) do
  418
+       Capistrano::CLI.ui.ask "Give me a ssh user: "
  419
+    end
  420
+
  421
+This prompt won't be displayed until the variable is actually required, which of course depending on the configuration of your callbacks, may be never at all, this is a very valuable feature that can help ensure your low-level staff or colleagues don't have access to sensitive passwords for production environments that you may wish to keep a secret.
  422
+
  423
+**Note:** The curly-brace, and do..end syntaxes are purely a matter of taste and readability, choose whichever suits you better, this is Ruby syntax sugar, and you may use it as you please.

0 notes on commit 2fa2a3c

Please sign in to comment.
Something went wrong with that request. Please try again.