Skip to content

Instantly share code, notes, and snippets.

@tachesimazzoca
Last active February 13, 2019 08:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tachesimazzoca/95110218bc12edeec0b163300dedd8b6 to your computer and use it in GitHub Desktop.
Save tachesimazzoca/95110218bc12edeec0b163300dedd8b6 to your computer and use it in GitHub Desktop.
Capistrano Custom SCM

Capistrano Custom SCM

Overview

Capistrano 3.7.0 and newer has three SCM plugins (Git/Subversion/Mercurial).

As the link above shows, it is also possible to provide a custom SCM plugin. The implementations of the default plugins would help you know the nuts and bolts.

Capistrano::SCM::Tgz

# .tgz file to be extracted into releases (required)
set :tgz_archive, "path/to/archive.tar.gz"
#set :tgz_archive, ask("Enter the path to *.tar.gz")

# tar --strip-components option (default: 0)
set :tgz_strip_components, 0
require "capistrano/scm/plugin"
require "digest/sha1"
class Capistrano::SCM::Tgz < Capistrano::SCM::Plugin
def set_defaults
end
def register_hooks
after "deploy:new_release_path", "tgz:create_release"
before "deploy:set_current_revision", "tgz:set_current_revision"
end
def define_tasks
this_plugin = self
namespace :tgz do
desc "Extract .tgz archive to releases"
task :create_release do
tgz_archive = this_plugin.as_tgz_archive(fetch(:tgz_archive))
n = fetch(:tgz_strip_components, 0)
on release_roles :all do
execute :mkdir, "-p", release_path
tmp_file = capture("mktemp")
upload!(tgz_archive, tmp_file)
execute :tar, "--strip-components=#{n}", "-xzf", tmp_file, "-C", release_path
execute :rm, tmp_file
end
end
task :set_current_revision do
tgz_archive = this_plugin.as_tgz_archive(fetch(:tgz_archive))
set :current_revision, Digest::SHA1.file(tgz_archive).hexdigest
end
end
end
def as_tgz_archive(path)
raise ":tgz_archive is not a regular file." unless File.exists? path
path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment