Permalink
Browse files

Run a build script in Copy deploy strategy

Use the :build_script configuration variable

The build script will be run before compression/exclusion of files,
if a :copy_cache is used, the build will run in the cache directory.
  • Loading branch information...
brugidou committed Aug 16, 2011
1 parent 7120c4a commit b9d3940e6054e0dbb032a6bb9290773d85465285
Showing with 40 additions and 0 deletions.
  1. +26 −0 lib/capistrano/recipes/deploy/strategy/copy.rb
  2. +14 −0 test/deploy/strategy/copy_test.rb
@@ -38,6 +38,16 @@ module Strategy
# :copy_compression, which must be one of :gzip, :bz2, or
# :zip, and which specifies how the source should be compressed for
# transmission to each host.
+ #
+ # There is a possibility to pass a build command that will get
+ # executed if your code needs to be compiled or something needs to be
+ # done before the code is ready to run.
+ #
+ # set :build_script, "make all"
+ #
+ # Note that if you use :copy_cache, the :build_script is used on the
+ # cache and thus you get faster compilation if your script does not
+ # recompile everything.
class Copy < Base
# Obtains a copy of the source code locally (via the #command method),
# compresses it to a single file, copies that file to all target
@@ -58,6 +68,8 @@ def deploy!
raise Capistrano::Error, "shell command failed with return code #{$?}"
end
+ build(copy_cache)
+
FileUtils.mkdir_p(destination)
logger.debug "copying cache to deployment staging area #{destination}"
@@ -84,6 +96,8 @@ def deploy!
logger.debug "getting (via #{copy_strategy}) revision #{revision} to #{destination}"
system(command)
+ build(destination)
+
if copy_exclude.any?
logger.debug "processing exclusions..."
if copy_exclude.any?
@@ -108,6 +122,18 @@ def deploy!
FileUtils.rm_rf destination rescue nil
end
+ def build(directory)
+ return unless configuration[:build_script]
+
+ Dir.chdir(directory) do
+ self.system(configuration[:build_script])
+ # Check the return code of last system command and rollback if not 0
+ unless $? == 0
+ raise Capistrano::Error, "shell command failed with return code #{$?}"
+ end
+ end
+ end
+
def check!
super.check do |d|
d.local.command(source.local.command) if source.local.command
@@ -288,6 +288,20 @@ def test_with_copy_cache_with_excludes_should_not_copy_excluded_files
@strategy.deploy!
end
+ def test_with_build_script_should_run_script
+ @config[:build_script] = "mkdir bin"
+
+ Dir.expects(:tmpdir).returns("/temp/dir")
+ @source.expects(:checkout).with("154", "/temp/dir/1234567890").returns(:local_checkout)
+ @strategy.expects(:system).with(:local_checkout)
+
+ Dir.expects(:chdir).with("/temp/dir/1234567890").yields
+ @strategy.expects(:system).with("mkdir bin")
+
+ prepare_standard_compress_and_copy!
+ @strategy.deploy!
+ end
+
private
def prepare_directory_tree!(cache, exclude=false)

0 comments on commit b9d3940

Please sign in to comment.