main
1require "capistrano/scm/plugin"
2
3module Capistrano
4 class S3Plugin < ::Capistrano::SCM::Plugin
5 def set_defaults
6 set :git_repo, ""
7 set :aws_profile, "default"
8 set :git_branch, ENV.fetch("GIT_BRANCH", "master")
9 set :bucket_name, ""
10 set :current_revision, ENV.fetch("BUILD_REVISION", nil)
11 end
12
13 def define_tasks
14 eval_rakefile File.expand_path("../tasks/s3.rake", __FILE__)
15 end
16
17 def register_hooks
18 after "deploy:new_release_path", "s3:create_release"
19 before "deploy:set_current_revision", "s3:set_current_revision"
20 before "deploy:check", "s3:check"
21 end
22
23 def test
24 backend.test " [ -f #{repo_path}/#{tarball} ] "
25 end
26
27 def check
28 s3 "ls s3://#{fetch(:bucket_name)}/#{fetch(:application)}/#{tarball}"
29 end
30
31 def clone
32 backend.execute(:mkdir, "-p", repo_path)
33 end
34
35 def update
36 source = "s3://#{fetch(:bucket_name)}/#{fetch(:application)}/#{tarball}"
37 destination = "#{repo_path}/#{tarball}"
38 s3 "cp #{source} #{destination}"
39 end
40
41 def release
42 path = "#{repo_path}/#{tarball}"
43 strip = "--strip-components=1"
44 backend.execute(:tar, "-xvzf", path, strip, "-C", release_path)
45 end
46
47 def s3(*args)
48 args.unshift "--profile #{fetch(:aws_profile)}"
49 args.unshift :s3
50 args.unshift :aws
51 backend.execute(*args)
52 end
53
54 def tarball
55 "#{fetch(:application)}-#{fetch(:current_revision)}.tar.gz"
56 end
57
58 def fetch_revision
59 command = "git ls-remote #{fetch(:git_repo)} #{fetch(:git_branch)}"
60 @current_revision ||= fetch(:current_revision) || `#{command}`[0...7]
61 end
62 end
63end