capistrano-conditional 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64ab0159258e31d984ab66b427d33caf82dfaa23
4
- data.tar.gz: 9b5fbe11ff09c04be713d8fba77b3a28c921a5bb
3
+ metadata.gz: 342478e6b86a6640f3ef7e5e736106aa7bb0891d
4
+ data.tar.gz: 9fe26b04628d6e5ca1449bdd2a82752eaa05c4d9
5
5
  SHA512:
6
- metadata.gz: 0f36f346973da57b1d9a0abaedc40a81d6ec131e93890b6c5aa07ed0fe9e2d8ecc38ec76e7b604d126a123511630e642ea8c18306508df472067924625d81bda
7
- data.tar.gz: 070d4a2781d1e1b1a85294f56339d44c1574c23afd3071b236963fe45187a096b33acada75e183f51031445cd18afc4c86d8640793bf4211944c418d0c350cc9
6
+ metadata.gz: d9d204061b4610bc21ea10ba28d48c7ffd0449db7c06ae9505594cbe0590a6c02fa02a63d5f9c89f86a1efe3eb26827f8e5b99614e0089133bb5ea70db31bb6d
7
+ data.tar.gz: 6239ed308a07eef67539cf37a1fd21b97bd21b083e3edb3d7a290e81c93518d3eb654ec7f012c4856a56bed7c45f2f78ccca21abd6e77664082ae6b2e4b1db30
data/README.md CHANGED
@@ -1,12 +1,9 @@
1
1
  # capistrano-conditional
2
2
 
3
- This gem extends capistrano v2 deployments to allow certain tasks to only be run under certain conditions -- i.e. conditionally.
3
+ This gem extends capistrano v3 deployments to allow certain tasks to only be run under certain conditions -- i.e. conditionally.
4
4
 
5
- It hasn't yet been extended to work with Capistrano 3; pull requests welcomed!
5
+ For capistrano v2 support, see version 0.1.0.
6
6
 
7
- ## BREAKING UPDATES
8
-
9
- * v. 0.1.0 pulls the branch to be deployed from capistrano multistage, rather than assuming it's whatever you currently have checked out in your working directory (among other things, this allows tweaking the deploy recipe multiple times without having to commit those changes).
10
7
 
11
8
  ## Installation
12
9
 
@@ -16,27 +13,29 @@ Add to your Gemfile:
16
13
  gem 'capistrano-conditional', :require => false # <-- This is important!
17
14
  end
18
15
 
19
- And then modify your deploy.rb to include this at the top:
16
+ And then modify your Capfile to include this line:
20
17
 
21
- require "capistrano-conditional"
18
+ require "capistrano/conditional"
22
19
 
23
20
  ## Requirements
24
21
 
25
22
  Your application must already be using capistrano for deployments, and (for now at least) you need to be using git.
26
23
 
27
- ## Usage Instructions
24
+ ## Usage
25
+
26
+ ### Overview
28
27
 
29
- <code>capistrano-conditional</code> adds logic to be run before <code>cap deploy</code> or <code>cap deploy:migrations</code> that compares the to-be-deployed code with the existing remote (currently deployed) code and lists all files that will be updated by the current deploy. It then checks the list of conditional statements that you've provided and runs any that you want run -- e.g. if you're using [whenever](https://github.com/javan/whenever) and you only want to run the <code>deploy:update_crontab</code> task if <code>config/schedule.rb</code> has been changed, you'd add a block like this to your deploy.rb:
28
+ <code>capistrano-conditional</code> adds logic to be run before <code>deploy:starting</code> that compares the to-be-deployed code with the existing remote (currently deployed) code and lists all files that will be updated by the current deploy. It then checks the list of conditional statements that you've provided and runs any that match the conditions specified, which allows you to only run certain tasks when other conditions are met. For instance, to add a logging statement to the capistrano output if any asset files have changed:
30
29
 
31
- ConditionalDeploy.register :whenever, :watchlist => 'config/schedule.rb' do
32
- after "deploy:symlink", "deploy:update_crontab"
30
+ ConditionalDeploy.configure(self) do |conditional|
31
+ conditional.register :asset_announcement, :any_match => ['app/assets'] do |c|
32
+ puts "Pointless alert: you're deploying an updated asset file!"
33
+ end
33
34
  end
34
35
 
35
- This example registers a conditional named "whenever" (names aren't programmatically important, but they're used to report what's going to be run at the beginning of each deploy). The contents of the block will be run only if the list of changed files includes a path that matches <code>config/schedule.rb</code>.
36
36
 
37
- ### Setting branches
37
+ This example registers a conditional named "asset_announcements" (names aren't programmatically important, but they're used to report what's going to be run at the beginning of each deploy). The contents of the block will be run only if the list of changed files includes a path that matches <code>add/assets</code>. For more useful tasks, keep reading.
38
38
 
39
- By default, capistrano-conditional pics up the branch-to-be-deployed from the `:branch` setting used by capistrano multistage, or defaults to `HEAD`. To specify a different branch manually: `set :git_deploying, 'some/other/branch/name'`.
40
39
 
41
40
  ### Available Conditions
42
41
 
@@ -55,75 +54,51 @@ Where file_list is either a string or an array of strings which will be <em>matc
55
54
 
56
55
  If you need more custom control, <code>:if</code> and <code>:unless</code> expect a Proc (which will be passed the list of changed files, if one argument is expected, or the list of changes and the git object itself, if two arguments are expected and you really want to dive into things yourself).
57
56
 
58
- ## Example Usage
59
57
 
60
- These snippets go in <code>config/deploy.rb</code> or any other file that gets loaded via capistrano.
58
+ ### Skipping Tasks
61
59
 
62
- ### A random collection of examples:
60
+ A major change from Capistrano 2 to Capistrano 3 is that task definitions are now additive, so defining a new task doesn't overwrite the existing definition. Often we want to replace a task with a no-op when certain conditions match, however (e.g. skip compiling assets if none have changed since the previous deploy). To help with this, CapistranoConditional adds a `skip_task` helper method on the context passed into the register block. It clears out the existing definition of that method and, by default, replaces it with a `put` statement saying it's been skipped.
63
61
 
64
- #### Using [whenever](https://github.com/javan/whenever)
62
+ It accepts a hash of options, including `:silent` (if truthy, the put statement is skipped), `:message` (to customize the message to be displayed), and `:clear_hooks`, which will clear all existing before/after hooks on the named task as well as updating the task's definition itself.
65
63
 
66
- Only run the rake task to update the crontab if the schedule has changed:
64
+ For instance, using [whenever](https://github.com/javan/whenever), to only run the rake task updating the crontab if the schedule.rb has changed:
67
65
 
68
- ConditionalDeploy.register :whenever, :watchlist => 'config/schedule.rb' do
69
- after "deploy:symlink", "deploy:update_crontab"
66
+ ConditionalDeploy.configure(self) do |conditional|
67
+ conditional.register :no_whenever, :none_match => 'config/schedule.rb' do |c|
68
+ c.skip_task 'whenever:update_crontab'
69
+ end
70
70
  end
71
71
 
72
- #### Using [thinking-sphinx](https://github.com/pat/thinking-sphinx)
73
72
 
74
- Only restart the sphinx daemon if our database or schema has changed. Otherwise, just copy the generated sphinx config from the previous release:
75
-
76
- SPHINX_WATCHLIST = ['db/schema.rb', 'db/migrate', 'sphinx.yml', 'app/indices']
77
-
78
- ConditionalDeploy.register :sphinx, :watchlist => SPHINX_WATCHLIST do
79
- before "deploy:update_code", "thinking_sphinx:stop"
80
- before "deploy:start", "thinking_sphinx:start"
81
- before "deploy:restart", "thinking_sphinx:start"
82
- end
83
-
84
- ConditionalDeploy.register :no_sphinx, :none_match => SPHINX_WATCHLIST do
85
- after "deploy:update_code", "sphinx:copy_config"
86
- end
73
+ ### Example Usage - Asset Precompilation
87
74
 
88
- namespace :sphinx do
89
- desc 'Copy the config file from previous release, if available, or else rerun configuration'
90
- task :copy_config, :roles => :app do
91
- run "([ -f #{current_path}/config/#{stage}.sphinx.conf ] && cp #{current_path}/config/#{stage}.sphinx.conf #{release_path}/config/#{stage}.sphinx.conf) || true"
92
- run "[ -f #{release_path}/config/#{stage}.sphinx.conf ] || (cd #{release_path} && bundle exec rake ts:config RAILS_ENV=#{stage})"
75
+ ConditionalDeploy.configure(self) do |conditional|
76
+ asset_paths = ['/assets', 'Gemfile.lock', 'config/environments']
77
+ conditional.register :skip_asset_precompilation, none_match: asset_paths do |c|
78
+ c.skip_task 'deploy:compile_assets'
93
79
  end
94
- end
95
-
96
-
97
- #### Using [jammit](https://github.com/documentcloud/jammit)
98
80
 
99
- For pre-asset-pipeline versions of Rails, this snippet will reprocess your assets with [jammit](https://github.com/documentcloud/jammit) only if necessary:
81
+ conditional.register :local_asset_precompilation, any_match: asset_paths do |c|
82
+ c.skip_task 'deploy:compile_assets', silent: true
100
83
 
101
- ConditionalDeploy.register :jammit, :watchlist => ['public/images/embed', 'public/stylesheets', 'public/javascripts', 'public/assets', 'config/assets.yml'] do
102
- after 'deploy:symlink', 'deploy:rebuild_assets'
84
+ task 'deploy:compile_assets' do
85
+ # Logic here to precompile locally
86
+ end
87
+ end
103
88
  end
104
89
 
105
- #### Migrations
106
90
 
107
- I've got <code>cap deploy</code> in muscle memory, and I used to find myself forgetting to run <code>cap deploy:migrations</code> until after I tested the new changes and found staging wasn't working right. I now add the following code to my apps, so I never have to worry about it again:
108
91
 
109
- if ARGV.any?{|v| v['deploy:migrations']} # If running deploy:migrations
110
- # If there weren't any changes to migrations or the schema file, then abort the deploy
111
- ConditionalDeploy.register :unneeded_migrations, :none_match => ['db/schema.rb', 'db/migrate'] do
112
- abort "You're running migrations, but it doesn't look like you need to!"
113
- end
114
- else # If NOT running deploy:migrations
115
- # If there were changes to migration files, run migrations as part of the deployment
116
- ConditionalDeploy.register :forgotten_migrations, :any_match => ['db/schema.rb', 'db/migrate'], :msg => "Forgot to run migrations? It's cool, we'll do it for you." do
117
- after "deploy:update_code", "deploy:migrate"
118
- end
119
- end
120
92
 
121
- Since I use it on every project, I've wrapped that logic up into this gem. To enable, just add `set :monitor_migrations, true`.
122
93
 
123
94
  ## Advanced Usage
124
95
 
125
96
  If you need to force a particular conditional to run, you can do so via the environment. Given the examples above, if you want to run the conditional named <code>whenever</code> even though config/schedule.rb hasn't been changed, just run <code>cap deploy RUN_WHENEVER=1</code>. Similarly, if you needed to skip the <code>whenever</code> conditional which would otherwise be run, you can use <code>cap deploy SKIP_WHENEVER=1</code>.
126
97
 
98
+ ### Setting branches
99
+
100
+ By default, capistrano-conditional pics up the branch-to-be-deployed from the `:branch` setting used by capistrano multistage, or defaults to `HEAD`. To specify a different branch manually: `set :git_deploying, 'some/other/branch/name'`.
101
+
127
102
  ## License
128
103
 
129
104
  Copyright &copy; 2014 [Deviantech, Inc.](http://www.deviantech.com) and released under the MIT license.
@@ -21,5 +21,5 @@ Gem::Specification.new do |s|
21
21
  # specify any dependencies here; for example:
22
22
  # s.add_development_dependency "rspec"
23
23
  s.add_runtime_dependency "git"
24
- s.add_runtime_dependency "capistrano", '~> 2.5'
24
+ s.add_runtime_dependency "capistrano", '~> 3'
25
25
  end
@@ -2,4 +2,5 @@ require "capistrano-conditional/version"
2
2
  require 'git'
3
3
  require "capistrano-conditional/unit"
4
4
  require "capistrano-conditional/deploy"
5
- require "capistrano-conditional/integration"
5
+
6
+ load File.expand_path("../../capistrano-conditional/tasks/integration.rake", __FILE__)
@@ -12,29 +12,27 @@ class ConditionalDeploy
12
12
  @@conditionals << Capistrano::Conditional::Unit.new(name, opts, block)
13
13
  end
14
14
 
15
- def self.configure
16
- yield self
17
- end
15
+ def skip_task(name, opts={})
16
+ method = opts[:clear_hooks] ? :clear : :clear_actions
17
+ msg = opts[:message] || "Skipping #{name} as preconditions to require it were not met"
18
18
 
19
- def self.monitor_migrations(context)
20
- if ARGV.any?{|v| v['deploy:migrations']} # If running deploy:migrations
21
- # If there weren't any changes to migrations or the schema file, then abort the deploy
22
- ConditionalDeploy.register :unneeded_migrations, :none_match => ['db/schema.rb', 'db/migrate'] do
23
- context.send :abort, "You're running migrations, but it doesn't look like you need to!"
24
- end
25
- else # If NOT running deploy:migrations
26
- # If there were changes to migration files, run migrations as part of the deployment
27
- ConditionalDeploy.register :forgotten_migrations, :any_match => ['db/schema.rb', 'db/migrate'], :msg => "Forgot to run migrations? It's cool, we'll do it for you." do
28
- context.after "deploy:update_code", "deploy:migrate"
29
- end
19
+ Rake::Task[name] && Rake::Task[name].send(method)
20
+
21
+ # Need to create stub for method in case called from
22
+ @@deploy_context.send(:task, name) do
23
+ puts msg.cyan unless opts[:silent]
30
24
  end
31
25
  end
32
26
 
33
- def initialize(current, deploying)
34
- @logger = Capistrano::Logger.new(:output => STDOUT)
35
- @logger.level = Capistrano::Logger::MAX_LEVEL
27
+ def self.configure(context)
28
+ @@deploy_context = context
29
+ yield self
30
+ end
31
+
32
+ def initialize(context, current, deploying)
33
+ @context = context
34
+ @log_method = :info # TODO: make this configurable
36
35
 
37
- @verbose = true
38
36
  @git = Git.open('.')
39
37
  @working = get_object 'HEAD'
40
38
  @current = get_object current, 'currently deployed'
@@ -65,7 +63,7 @@ class ConditionalDeploy
65
63
  set_report_header
66
64
  set_report_files
67
65
  set_report_runlist
68
- log_plan @plan
66
+ log @plan
69
67
  end
70
68
 
71
69
  def screen_conditionals
@@ -80,7 +78,7 @@ class ConditionalDeploy
80
78
 
81
79
  def run_conditionals
82
80
  @to_run.each do |job|
83
- job.block.call
81
+ job.block.call(self)
84
82
  end
85
83
  end
86
84
 
@@ -125,9 +123,9 @@ class ConditionalDeploy
125
123
  "#{c.sha} #{c.message.split("\n").first}"
126
124
  end
127
125
 
128
- def log_plan(lines = "\n", level = Capistrano::Logger::TRACE)
126
+ def log(lines = "\n")
129
127
  Array(lines).each do |line|
130
- @logger.log(level, ': ' + line, "Conditional")
128
+ @context.send @log_method, " [Conditional] #{line}"
131
129
  end
132
130
  end
133
131
 
@@ -0,0 +1,19 @@
1
+ abort "\nGit is not defined (are you in a git repository, with the Git gem installed?)\n" unless defined?(Git)
2
+
3
+ # By default, assume using multi-stage deployment setting :branch variable, and that the local branch is up to date
4
+ # (although don't require being on that branch to deploy).
5
+ set :git_deploying, -> { fetch(:branch).nil? ? 'HEAD' : "origin/#{fetch(:branch)}"}
6
+
7
+ namespace :conditional do
8
+ desc "Initializes the conditional deployment functionality"
9
+ task :apply do
10
+ on primary(:app) do
11
+ currently_deployed = capture("cat #{current_path}/REVISION").strip
12
+ @deploy = ConditionalDeploy.new(self, currently_deployed, fetch(:git_deploying))
13
+ @deploy.apply_conditions!
14
+ end
15
+ end
16
+ end
17
+
18
+ # Ensure deploys apply conditional elements before running the rest of the tasks
19
+ before 'deploy:starting', 'conditional:apply'
@@ -1,5 +1,5 @@
1
1
  module Capistrano
2
2
  module Conditional
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-conditional
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kali Donovan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-25 00:00:00.000000000 Z
11
+ date: 2014-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: git
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.5'
33
+ version: '3'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.5'
40
+ version: '3'
41
41
  description: Allows making tasks for git-based projects conditional based on the specific
42
42
  files to be deployed.
43
43
  email:
@@ -52,11 +52,11 @@ files:
52
52
  - README.md
53
53
  - Rakefile
54
54
  - capistrano-conditional.gemspec
55
- - lib/capistrano-conditional.rb
56
55
  - lib/capistrano-conditional/deploy.rb
57
- - lib/capistrano-conditional/integration.rb
56
+ - lib/capistrano-conditional/tasks/integration.rake
58
57
  - lib/capistrano-conditional/unit.rb
59
58
  - lib/capistrano-conditional/version.rb
59
+ - lib/capistrano/conditional.rb
60
60
  homepage: https://github.com/deviantech/capistrano-conditional
61
61
  licenses: []
62
62
  metadata: {}
@@ -1,28 +0,0 @@
1
- Capistrano::Configuration.instance(:must_exist).load do
2
- log_formatter([
3
- { :match => /^Conditional$/, :color => :cyan, :style => :dim, :priority => 10 }
4
- ])
5
-
6
- abort "\ncapistrano-conditional is not compatible with Capistrano 1.x\n" unless respond_to?(:namespace)
7
- abort "\nGit is not defined (are you in a git repository, with the Git gem installed?)\n" unless defined?(Git)
8
-
9
- # By default, assume using multi-stage deployment setting :branch variable, and that the local branch is up to date
10
- # (although don't require being on that branch to deploy).
11
- set :git_deploying, -> { fetch(:branch).blank? ? 'HEAD' : "origin/#{fetch(:branch)}"}
12
- set :git_currently_deployed, -> { capture("cat #{current_path}/REVISION").strip }
13
- set :monitor_migrations, -> { false }
14
-
15
- namespace :conditional do
16
- desc "Initializes the conditional deployment functionality"
17
- task :apply do
18
- ConditionalDeploy.monitor_migrations(self) if monitor_migrations
19
-
20
- @deploy = ConditionalDeploy.new(git_currently_deployed, git_deploying)
21
- @deploy.apply_conditions!
22
- end
23
- end
24
-
25
- # Ensure deploys apply conditional elements before running the rest of the tasks
26
- before 'deploy', 'conditional:apply'
27
- before 'deploy:migrations', 'conditional:apply'
28
- end