tag:ryankinderman.net,2005:/tag/rakekinderman.net : Tag rake, everything about rakecode code code2007-09-22T08:43:49-07:00Typotag:ryankinderman.net,2005:Article/312006-10-17T22:17:37-07:002007-09-22T08:43:49-07:00ryanTag-based Deployment with Capistrano<p>I've discovered that Capistrano is a useful deployment utility out-of-the-box if your needs are relatively simple. By simple, I mean that there is a single SCM that is accessible by all machines that you might need to do deployment to. This isn't always the case, especially when developing applications for large organizations. In such situations, you won't always get the access that you need to make deployment a straight-forward process. We've recently completed the deployment process for the project that I am currently working on, and it was one of those not-so-straight-forward scenarios.</p>
<p>
Our source code is stored in an SVN repos that is seperate from the client's repos, and only used by the dev team (for CI and such). However, the client servers do not connect to our dev repos, and they want deployment to be possible from their own repos. To do this, we've developed a deployment policy, along with supporting logic in the deployment process, around making periodic deployments to the client SCM.</p>
<p>
Basically, we tag the revision that we want to deploy on the dev repos, then do an export from the dev repos and import it to the client repos into a subdirectory that has the same name as the tagged version on the dev repos. We then communicate to the client that " ver X.Y.Z is now available for deployment". After that, either they or we can log into a machine on their network (which has access to their own SCM), do a checkout of the tagged deployment, and run the deployment process. The rest is your usual Capistrano stuff.</p>
<p>
The first step is to add a Rake task to support the tag/build part of the process:
<div class="CodeRay"><pre><notextile><span class="CodeRay">task <span class="sy">:tag_build</span> <span class="r">do</span>
local_repository = <span class="s"><span class="dl">"</span><span class="k">svn+ssh://local/repos</span><span class="dl">"</span></span>
remote_repository = <span class="s"><span class="dl">"</span><span class="k">svn+ssh://remote/repos</span><span class="dl">"</span></span>
build = <span class="co">TagBuild</span>.new(local_repository, remote_repository)
build.tag(<span class="co">ENV</span>[<span class="s"><span class="dl">'</span><span class="k">tag</span><span class="dl">'</span></span>], <span class="co">ENV</span>[<span class="s"><span class="dl">'</span><span class="k">rev</span><span class="dl">'</span></span>])
<span class="r">end</span></span></notextile></pre></div>
This allows the user to tag/build from the command line like this:
<div class="CodeRay"><pre><notextile><span class="CodeRay">rake tag_build tag=x.y.z rev=<span class="i">123</span></span></notextile></pre></div>
In the above example, the "tag" argument is the tag name to apply to both the local and remote repositories. The "rev" argument is the revision number that you want to take from the local repository. The TagBuild class contains the logic to tag the local repository and then check that tagged version into the remote repository. I leave that code as an exercise for the reader.</p>
<p>
The next step in this process is to add tag-based deployment capabilities to your Capistrano script. This first requires a modification of the "remote:deploy" Capistrano Rake task that passes a given tag to Capistrano. Here is an example of that modification (from lib/tasks/capistrano.rake):
<div class="CodeRay"><pre><notextile><span class="CodeRay">task(<span class="sy">:deploy</span>) { cap <span class="sy">:deploy</span>, <span class="s"><span class="dl">"</span><span class="k">-Stag=</span><span class="il"><span class="idl">#{</span><span class="co">ENV</span>[<span class="s"><span class="dl">'</span><span class="k">tag</span><span class="dl">'</span></span>]<span class="idl">}</span></span><span class="dl">"</span></span> }</span></notextile></pre></div>
This allows the user to invoke the deployment from the command-line like:
<div class="CodeRay"><pre><notextile><span class="CodeRay">rake deploy tag=<span class="co">X</span>.<span class="co">Y</span>.<span class="co">Z</span></span></notextile></pre></div></p>
<p>
And last but not least, a modification to the deploy.rb file must be made to consider the new "tag" value.
<div class="CodeRay"><pre><notextile><span class="CodeRay"> <span class="r">if</span> symbol_defined? <span class="s"><span class="dl">"</span><span class="k">tag</span><span class="dl">"</span></span>
set <span class="sy">:repository</span>, <span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>base_repository<span class="idl">}</span></span><span class="k">/tags/</span><span class="il"><span class="idl">#{</span>tag<span class="idl">}</span></span><span class="dl">"</span></span>
<span class="r">else</span>
set <span class="sy">:repository</span>, <span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>base_repository<span class="idl">}</span></span><span class="k">/trunk</span><span class="dl">"</span></span>
<span class="r">end</span></span></notextile></pre></div>
</p>