Compare commits
43 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
561331b263 | ||
|
|
57efd74341 | ||
|
|
5d31bd7df6 | ||
|
|
1a8635a98b | ||
|
|
1cff76230d | ||
|
|
eeb75048ba | ||
|
|
1ac8d2f9c4 | ||
|
|
36f2405a31 | ||
|
|
e9ba5e715c | ||
|
|
11eabe5b7c | ||
|
|
7eaf0a83f8 | ||
|
|
95b1cc15b9 | ||
|
|
9f9a0c3d6a | ||
|
|
536214fd03 | ||
|
|
964c57d21f | ||
|
|
675361e5d9 | ||
|
|
4fa2026b00 | ||
|
|
0945596b2f | ||
|
|
8ed22009d7 | ||
|
|
7f0a7e516b | ||
|
|
0ad8061fbb | ||
|
|
0cdc527259 | ||
|
|
1e2687ce83 | ||
|
|
ce07273ce1 | ||
|
|
2c0cb6098a | ||
|
|
e3abc74e5b | ||
|
|
7bbf24343e | ||
|
|
7c50957282 | ||
|
|
2015eb534e | ||
|
|
207023eb42 | ||
|
|
f10e66b69a | ||
|
|
989665c52a | ||
|
|
b187533157 | ||
|
|
90177a7ff5 | ||
|
|
257b59c37d | ||
|
|
b5b1cae948 | ||
|
|
72224b2d96 | ||
|
|
d5a6d036c9 | ||
|
|
c247629356 | ||
|
|
b8b4ffce03 | ||
|
|
bcecbcc885 | ||
|
|
85a7fcd2f4 | ||
|
|
368ec7e2a2 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,4 @@
|
||||
.rbenv-version
|
||||
.bundle
|
||||
/bin
|
||||
Gemfile.lock
|
||||
|
||||
8
Gemfile
8
Gemfile
@@ -1,9 +1,3 @@
|
||||
source "https://rubygems.org"
|
||||
|
||||
group :test do
|
||||
gem 'rake'
|
||||
gem 'rspec'
|
||||
gem 'capistrano'
|
||||
gem 'capistrano-ext'
|
||||
gem 'capistrano-spec', :git => 'git://github.com/mydrive/capistrano-spec.git'
|
||||
end
|
||||
gemspec
|
||||
|
||||
45
Gemfile.lock
45
Gemfile.lock
@@ -1,45 +0,0 @@
|
||||
GIT
|
||||
remote: git://github.com/mydrive/capistrano-spec.git
|
||||
revision: dcc0908dc00872272b1d495bcce70e82240fa8f7
|
||||
specs:
|
||||
capistrano-spec (0.1.0)
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
capistrano (2.11.2)
|
||||
highline
|
||||
net-scp (>= 1.0.0)
|
||||
net-sftp (>= 2.0.0)
|
||||
net-ssh (>= 2.0.14)
|
||||
net-ssh-gateway (>= 1.1.0)
|
||||
capistrano-ext (1.2.1)
|
||||
capistrano (>= 1.0.0)
|
||||
diff-lcs (1.1.3)
|
||||
highline (1.6.11)
|
||||
net-scp (1.0.4)
|
||||
net-ssh (>= 1.99.1)
|
||||
net-sftp (2.0.5)
|
||||
net-ssh (>= 2.0.9)
|
||||
net-ssh (2.3.0)
|
||||
net-ssh-gateway (1.1.0)
|
||||
net-ssh (>= 1.99.1)
|
||||
rake (0.9.2.2)
|
||||
rspec (2.8.0)
|
||||
rspec-core (~> 2.8.0)
|
||||
rspec-expectations (~> 2.8.0)
|
||||
rspec-mocks (~> 2.8.0)
|
||||
rspec-core (2.8.0)
|
||||
rspec-expectations (2.8.0)
|
||||
diff-lcs (~> 1.1.2)
|
||||
rspec-mocks (2.8.0)
|
||||
|
||||
PLATFORMS
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
capistrano
|
||||
capistrano-ext
|
||||
capistrano-spec!
|
||||
rake
|
||||
rspec
|
||||
150
README.md
150
README.md
@@ -1,13 +1,20 @@
|
||||
Capistrano Deployment Tags
|
||||
==========================
|
||||
This plugin to Capistrano will add a timestamped Git tag
|
||||
at each deployment, automatically. It is intended to be used with
|
||||
the multistage recipe and will tag each release by environment.
|
||||
You can, however, use it without multistage simply by setting :branch
|
||||
and :stage in your recipe.
|
||||
[](http://badge.fury.io/rb/capistrano-deploytags)
|
||||
|
||||
## Capistrano Deployment Tags
|
||||
|
||||
This plugin for Capistrano 3 will add a timestamped Git tag
|
||||
at each deployment, automatically. It requires :branch and :stage to be set,
|
||||
but as Capistrano 3 is multistage by default (unlike Cap 2) :stage should
|
||||
already be set, but you can override the variable if you want to change the
|
||||
name of the tag.
|
||||
|
||||
### Requires Capistrano 3
|
||||
|
||||
As of version 1.0.0, this plugin requires Cap 3. If you need a Capistrano
|
||||
2 compatible version, then use `gem 'capistrano-deploytags', '~> 0.9.2'`
|
||||
|
||||
### What It Does
|
||||
|
||||
What It Does
|
||||
------------
|
||||
Simply: it makes it so you can track your deployments from Git.
|
||||
If I were to issue the command:
|
||||
|
||||
@@ -16,28 +23,36 @@ If I were to issue the command:
|
||||
This would result in one new git tag with the environment and
|
||||
timestamp:
|
||||
|
||||
`production-2012.04.02-203155`
|
||||
`production-2012.04.02-203155-utc`
|
||||
|
||||
These tags can be used for any number of useful things including
|
||||
generating statistics about deployments per day/week/year, tracking
|
||||
code size over a period of time, detecting Rails migrations, and
|
||||
probably a thousand other things I haven't thought of.
|
||||
|
||||
Usage
|
||||
-----
|
||||
### Usage
|
||||
|
||||
capistrano-deploytags is available on
|
||||
[rubygems.org](https://rubygems.org/gems/capistrano-deploytags).
|
||||
You can install it from there with:
|
||||
In keeping with the pattern used by Capistrano itself and other plugins, add it
|
||||
to the `development` group of your Gemfile with `require: false`:
|
||||
|
||||
`gem install capistrano-deploytags`
|
||||
```ruby
|
||||
# Gemfile
|
||||
group :development do
|
||||
gem 'capistrano-deploytags', '~> 1.0.0', require: false
|
||||
end
|
||||
```
|
||||
|
||||
If you use Bundler, be sure to add the gem to your Gemfile.
|
||||
In your Capistrano `config/deploy.rb` you should add:
|
||||
Then require `capistrano/deploytags` in your Capfile
|
||||
|
||||
`require 'capistrano-deploytags'`
|
||||
```
|
||||
# Capfile
|
||||
require 'capistrano/deploytags'
|
||||
```
|
||||
|
||||
This will create two tasks, one that runs before deployment and one
|
||||
that runs after.
|
||||
This will create two tasks, one that runs before the `deploy` task, and one
|
||||
that runs after the `cleanup` task.
|
||||
|
||||
*NOTE:* You will be creating and pushing tags from the version of the code in the
|
||||
current checkout. This plugin needs to be run from a clean checkout of your
|
||||
@@ -50,15 +65,14 @@ system that will actually be deployed before checking the tree for changes.
|
||||
Know this ahead of time as this may affect how you deal with your deployment
|
||||
branches.
|
||||
|
||||
Setting the Remote
|
||||
------------------
|
||||
By default, Capistrano Deploytags will use the first remote in the list returned
|
||||
by `git remote`. If you prefer to use a different remote, then you may change the
|
||||
`:git_remote` setting from your `deploy.rb`, the stage, or on the command line with
|
||||
`-S git_remote=your-remote`.
|
||||
### Setting the Remote
|
||||
|
||||
By default, Capistrano Deploytags will use the remote names `origin`. If you
|
||||
use a different remote name, then you may change the `:git_remote` setting
|
||||
from your `deploy.rb` or the stage.
|
||||
|
||||
### Working on Your Deployment Scripts
|
||||
|
||||
Working on Your Deployment Scripts
|
||||
----------------------------------
|
||||
Because you must have a clean tree to deploy, working on your deployment
|
||||
scripts themselves can be a bit frustrating unless you know how to make it
|
||||
work. The easiest way around this problem is to simply commit your changes
|
||||
@@ -68,17 +82,18 @@ happily carry on deploying without complaint.
|
||||
Alternatively, you could disable the plugin temporarily with one of the
|
||||
methods described below.
|
||||
|
||||
Disabling Tagging for a Stage
|
||||
-----------------------------
|
||||
### Disabling Tagging for a Stage
|
||||
|
||||
Sometimes you do not want to enable deployment tagging for a particular
|
||||
stage. In that event, you can simply disable tagging by setting `no_deploytags`
|
||||
lik so:
|
||||
like so:
|
||||
|
||||
```ruby
|
||||
set :no_deploytags, true
|
||||
```
|
||||
|
||||
You can also set this from the command line at any time with `-S no_deploytags=true`.
|
||||
You can also set this from the command line at any time with an environment
|
||||
variable `cap stage deploy NO_DEPLOYTAGS=true`.
|
||||
|
||||
*NOTE:* this will disable the use of the plugin's functionality entirely for
|
||||
that stage. The tasks will run, but will do nothing. This means that tasks that
|
||||
@@ -86,8 +101,30 @@ are hooked to the Capistrano Deploytags tasks will also still run, but they may
|
||||
find their expectations are not met with regards to the cleanliness of the git
|
||||
tree.
|
||||
|
||||
Viewing Deployment History
|
||||
--------------------------
|
||||
### Customizing the Tag Format
|
||||
|
||||
You may override the time format in `deploy.rb` or your stage:
|
||||
|
||||
```ruby
|
||||
set :deploytag_time_format, "%Y.%m.%d-%H%M%S-utc"
|
||||
```
|
||||
|
||||
To use your local time and not UTC (so that ```Time.now``` and not ```Time.now.utc``` is used internally):
|
||||
|
||||
```ruby
|
||||
set :deploytag_utc, false
|
||||
```
|
||||
|
||||
### Customizing the Tag Commit Message
|
||||
|
||||
By default, Capistrano Deploytags will create a tag with a message that indicates
|
||||
the local user name on the box where the deployment is done, and the hash of the
|
||||
tagged commit. If you prefer to have a more detailed commit message you may override
|
||||
the `:deploytag_commit_message` setting from your `deploy.rb`, e.g.
|
||||
`set :deploytag_commit_message, 'This is my commit message for the deployed tag'`
|
||||
|
||||
### Viewing Deployment History
|
||||
|
||||
It's trivial to view the deployment history for a repo. From a checkout
|
||||
of the repo, type `git tag -l -n1`. The output looks something like:
|
||||
|
||||
@@ -96,17 +133,16 @@ dev-2013.07.22-105130 baz deployed a4d522d9d to dev
|
||||
dev-2013.07.22-113207 karl deployed 4c43f8464 to dev
|
||||
dev-2013.07.22-114437 gavin deployed 776e15414 to dev
|
||||
dev-2013.07.22-115103 karl deployed 619ff5724 to dev
|
||||
dev-2013.07.22-144121 joshmyers deployed cf1ed1a02 to dev
|
||||
dev-2013.07.22-144121 josh deployed cf1ed1a02 to dev
|
||||
```
|
||||
|
||||
A little use of `grep` and you can easily get the history for a
|
||||
particular (e.g. `git tag -l -n1 | grep dev`).
|
||||
|
||||
It should be noted that the names used when tags are created are the
|
||||
local user name on the box where the deployment is done.
|
||||
|
||||
Helpful Git Config
|
||||
------------------
|
||||
### Helpful Git Config
|
||||
|
||||
You might find it useful to add this to your ~/.gitconfig in order
|
||||
to get a nice history view of the commits and tags.
|
||||
|
||||
@@ -118,21 +154,43 @@ to get a nice history view of the commits and tags.
|
||||
You can then view the list by typing `git lol` from the checked out
|
||||
code path.
|
||||
|
||||
Deploying a Previous Commit
|
||||
---------------------------
|
||||
### Deploying a Previous Commit
|
||||
|
||||
Because you have to actually be on the head of the branch you are
|
||||
deploying in order for tagging to work properly, deploying a previous
|
||||
commit doesn't work as you might expect. The simple solution is to
|
||||
create a new branch from the previous commit you wish to deploy and
|
||||
supplying `-S branch=<new branch>` as arguments to Capistrano.
|
||||
commit doesn't work as you might expect.
|
||||
|
||||
One simple solution is to configure your `config.rb` to accept an ENV var
|
||||
override. Then if you need to deploy a previous commit you can check out that
|
||||
commit (SHA or branch), and supply the var on the command line. e.g. with this
|
||||
in your `config.rb`:
|
||||
|
||||
```ruby
|
||||
set :branch, ENV["REVISION"] || ENV["BRANCH_NAME"] || "master"
|
||||
```
|
||||
|
||||
you can deploy a previous commit with
|
||||
|
||||
```shell
|
||||
git checkout <previous-commit>
|
||||
cap <stage> deploy REVISION=<previous-commit>
|
||||
```
|
||||
|
||||
### Running from Jenkins
|
||||
|
||||
Because Jenkins will check out the code with the current revision
|
||||
number you will be in a detached state. This causes the plugin to be
|
||||
unhappy about the git tree. The solution is to add `-S branch=$GIT_COMMIT`
|
||||
to the cap deploy line called from your Jenkins build. This will cause
|
||||
the diffs and comparisons done by the deploytags gem to be correct.
|
||||
|
||||
### Credits
|
||||
|
||||
Credits
|
||||
-------
|
||||
This software was written by [Karl Matthias](https://github.com/relistan)
|
||||
with help from [Gavin Heavyside](https://github.com/gavinheavyside) and the
|
||||
support of [MyDrive Solutions Limited](http://mydrivesolutions.com).
|
||||
|
||||
License
|
||||
-------
|
||||
### License
|
||||
|
||||
This plugin is released under the BSD two clause license which is
|
||||
available in both the Ruby Gem and the source repository.
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
Gem::Specification.new do |s|
|
||||
s.name = 'capistrano-deploytags'
|
||||
s.version = '0.8.0'
|
||||
s.date = '2013-08-27'
|
||||
s.summary = "Add dated, environment-specific tags to your git repo at each deployment."
|
||||
s.description = <<-EOS
|
||||
Capistrano Deploytags is a simple plugin to Capistrano that works with your deployment framework to track your code releases. All you have to do is require capistrano-deploytags and each deployment will add a new tag for that deployment, pointing to the latest commit. This lets you easily see which code is deployed on each environment, and allows you to figure out which code was running in an environment at any time in the past.
|
||||
s.license = 'BSD-2-Clause'
|
||||
s.version = '1.0.4'
|
||||
s.date = '2015-10-18'
|
||||
s.summary = 'Add dated, environment-specific tags to your git repo at each deployment.'
|
||||
s.description = <<-EOS
|
||||
Capistrano Deploytags is a simple plugin to Capistrano 3 that works with your deployment framework to track your code releases. All you have to do is require capistrano-deploytags/capistrano and each deployment will add a new tag for that deployment, pointing to the latest commit. This lets you easily see which code is deployed on each environment, and allows you to figure out which code was running in an environment at any time in the past.
|
||||
EOS
|
||||
s.authors = ["Karl Matthias"]
|
||||
s.email = 'relistan@gmail.com'
|
||||
s.files = Dir.glob("lib/**/*") + %w{ README.md LICENSE }
|
||||
s.authors = ['Karl Matthias', 'Gavin Heavyside']
|
||||
s.email = ['relistan@gmail.com', 'gavin.heavyside@mydrivesolutions.com']
|
||||
s.files = `git ls-files lib`.split(/\n/) + %w{ README.md LICENSE }
|
||||
s.homepage = 'http://github.com/mydrive/capistrano-deploytags'
|
||||
s.add_dependency 'capistrano'
|
||||
s.add_dependency 'capistrano-ext'
|
||||
s.add_dependency 'capistrano', '>= 3.2.0'
|
||||
# s.add_development_dependency 'capistrano-spec'
|
||||
s.add_development_dependency 'rake'
|
||||
s.add_development_dependency 'rspec', '~> 3.0.0'
|
||||
s.require_path = 'lib'
|
||||
end
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
Dir[File.join(File.dirname(__FILE__), 'capistrano', '*')].each do |file|
|
||||
require file
|
||||
end
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
module Capistrano
|
||||
module DeployTags
|
||||
def pending_git_changes?
|
||||
# Do we have any changes vs HEAD on deployment branch?
|
||||
!(`git fetch && git diff #{branch} --shortstat`.strip.empty?)
|
||||
end
|
||||
|
||||
def git_tag_for(stage)
|
||||
"#{stage}-#{Time.now.strftime("%Y.%m.%d-%H%M%S")}"
|
||||
end
|
||||
|
||||
def safe_run(*args)
|
||||
raise "#{args.join(" ")} failed!" unless system(*args)
|
||||
end
|
||||
|
||||
def validate_git_vars
|
||||
unless exists?(:branch) && exists?(:stage)
|
||||
logger.log Capistrano::Logger::IMPORTANT, 'Capistrano Deploytags requires that :branch and :stage be defined.'
|
||||
raise 'define :branch and :stage'
|
||||
end
|
||||
end
|
||||
|
||||
def git_tag?(tag)
|
||||
!`git tag -l #{tag}`.strip.empty?
|
||||
end
|
||||
|
||||
def has_remote?
|
||||
!`git remote`.strip.empty?
|
||||
end
|
||||
|
||||
def remote
|
||||
exists?(:git_remote) ? git_remote : `git remote`.strip.split(/\n/).first
|
||||
end
|
||||
|
||||
def self.load_into(configuration)
|
||||
configuration.load do
|
||||
before 'deploy', 'git:prepare_tree'
|
||||
before 'deploy:migrations', 'git:prepare_tree'
|
||||
after 'deploy', 'git:tagdeploy'
|
||||
after 'deploy:migrations', 'git:tagdeploy'
|
||||
|
||||
desc 'prepare git tree so we can tag on successful deployment'
|
||||
namespace :git do
|
||||
task :prepare_tree, :except => { :no_release => true } do
|
||||
next if fetch(:no_deploytags, false)
|
||||
|
||||
cdt.validate_git_vars
|
||||
|
||||
logger.log Capistrano::Logger::IMPORTANT, "Preparing to deploy HEAD from branch '#{branch}' to '#{stage}'"
|
||||
|
||||
if cdt.pending_git_changes?
|
||||
logger.log Capistrano::Logger::IMPORTANT, "Whoa there, partner. Dirty trees can't deploy. Git yerself clean first."
|
||||
raise 'Dirty git tree'
|
||||
end
|
||||
|
||||
cdt.safe_run 'git', 'checkout', branch
|
||||
logger.log Capistrano::Logger::IMPORTANT, "Pulling from #{branch}"
|
||||
cdt.safe_run 'git', 'pull', cdt.remote, branch if cdt.has_remote?
|
||||
end
|
||||
|
||||
desc 'add git tags for each successful deployment'
|
||||
task :tagdeploy, :except => { :no_release => true } do
|
||||
next if fetch(:no_deploytags, false)
|
||||
|
||||
cdt.validate_git_vars
|
||||
|
||||
current_sha = `git rev-parse #{branch} HEAD`.strip[0..8]
|
||||
logger.log Capistrano::Logger::INFO, "Tagging #{current_sha} for deployment"
|
||||
|
||||
tag_user = (ENV['USER'] || ENV['USERNAME']).strip
|
||||
cdt.safe_run 'git', 'tag', '-a', cdt.git_tag_for(stage), '-m', "#{tag_user} deployed #{current_sha} to #{stage}"
|
||||
cdt.safe_run 'git', 'push', '--tags' if cdt.has_remote?
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Capistrano.plugin :cdt, Capistrano::DeployTags
|
||||
|
||||
if Capistrano::Configuration.instance
|
||||
Capistrano::DeployTags.load_into(Capistrano::Configuration.instance(:must_exist))
|
||||
end
|
||||
32
lib/capistrano/deploytags.rb
Normal file
32
lib/capistrano/deploytags.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
# Ensure deploy tasks are loaded before we run
|
||||
require 'capistrano/deploy'
|
||||
|
||||
# Load extra tasks into the deploy namespace
|
||||
load File.expand_path("../tasks/deploytags.rake", __FILE__)
|
||||
|
||||
module CapistranoDeploytags
|
||||
class Helper
|
||||
def self.git_tag_for(stage)
|
||||
"#{stage}-#{formatted_time}"
|
||||
end
|
||||
|
||||
def self.formatted_time
|
||||
now = if fetch(:deploytag_utc, true)
|
||||
Time.now.utc
|
||||
else
|
||||
Time.now
|
||||
end
|
||||
|
||||
now.strftime(fetch(:deploytag_time_format, "%Y.%m.%d-%H%M%S-#{now.zone.downcase}"))
|
||||
end
|
||||
|
||||
def self.commit_message(current_sha, stage)
|
||||
if fetch(:deploytag_commit_message, false)
|
||||
deploytag_commit_message
|
||||
else
|
||||
tag_user = (ENV['USER'] || ENV['USERNAME'] || 'deployer').strip
|
||||
"#{tag_user} deployed #{current_sha} to #{stage}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
51
lib/capistrano/tasks/deploytags.rake
Normal file
51
lib/capistrano/tasks/deploytags.rake
Normal file
@@ -0,0 +1,51 @@
|
||||
namespace :deploy do
|
||||
desc 'prepare git tree so we can tag on successful deployment'
|
||||
before :deploy, :prepare_tree do
|
||||
run_locally do
|
||||
if ENV['NO_DEPLOYTAGS'] || fetch(:no_deploytags, false)
|
||||
info "[deploytags] Skipping deploytags"
|
||||
else
|
||||
branch = fetch(:branch, false)
|
||||
stage = fetch(:stage, false)
|
||||
|
||||
unless branch && stage
|
||||
error 'capistrano-deploytags requires that :branch and :stage be defined'
|
||||
raise 'define :branch and :stage'
|
||||
end
|
||||
|
||||
strategy.git "fetch #{fetch(:git_remote, 'origin')}"
|
||||
|
||||
diff_output = capture :git, "diff #{branch} --shortstat"
|
||||
|
||||
unless diff_output.empty?
|
||||
error "Whoa there, partner. Dirty trees can't deploy. Git yerself clean first"
|
||||
raise 'Dirty git tree'
|
||||
end
|
||||
|
||||
strategy.git "checkout #{branch}"
|
||||
info "Pulling from #{branch}"
|
||||
strategy.git "pull #{fetch(:git_remote, 'origin')} #{branch}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc 'add git tags for each successful deployment'
|
||||
after :cleanup, :tagdeploy do
|
||||
run_locally do
|
||||
if ENV['NO_DEPLOYTAGS'] || fetch(:no_deploytags, false)
|
||||
info "[deploytags] Skipping deploytags"
|
||||
else
|
||||
tag_name = ENV['CUSTOM_DEPLOYTAG'] || fetch(:custom_deploytag) || CapistranoDeploytags::Helper.git_tag_for(fetch(:stage))
|
||||
latest_revision = fetch(:current_revision)
|
||||
commit_message = CapistranoDeploytags::Helper.commit_message(latest_revision, fetch(:stage))
|
||||
|
||||
unless fetch(:sshkit_backend) == SSHKit::Backend::Printer # unless --dry-run flag present
|
||||
strategy.git "tag -a #{tag_name} -m \"#{commit_message}\" #{latest_revision}"
|
||||
strategy.git "push #{fetch(:git_remote, 'origin')} #{tag_name}"
|
||||
end
|
||||
|
||||
info "[cap-deploy-tagger] Tagged #{latest_revision} with #{tag_name}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,115 +1,5 @@
|
||||
require 'capistrano'
|
||||
require 'capistrano-spec'
|
||||
require 'fileutils'
|
||||
mypath = File.expand_path(File.dirname(__FILE__))
|
||||
require File.expand_path(File.join(mypath, '..', 'lib', 'capistrano', 'deploy_tags'))
|
||||
require 'rspec'
|
||||
|
||||
describe Capistrano::DeployTags do
|
||||
let(:configuration) { Capistrano::Configuration.new }
|
||||
let(:tmpdir) { "/tmp/#{$$}" }
|
||||
let(:mypath) { mypath }
|
||||
|
||||
before :each do
|
||||
Capistrano::DeployTags.load_into(configuration)
|
||||
end
|
||||
|
||||
def with_clean_repo(&block)
|
||||
FileUtils.rm_rf tmpdir
|
||||
FileUtils.mkdir tmpdir
|
||||
FileUtils.chdir tmpdir
|
||||
raise unless system("/usr/bin/tar xzf #{File.join(mypath, 'fixtures', 'git-fixture.tar.gz')}")
|
||||
FileUtils.chdir "#{tmpdir}/git-fixture"
|
||||
yield
|
||||
FileUtils.rm_rf tmpdir
|
||||
end
|
||||
|
||||
context "prepare_tree" do
|
||||
it "raises an error when not in a git tree" do
|
||||
FileUtils.chdir '/tmp'
|
||||
configuration.set(:branch, 'master')
|
||||
configuration.set(:stage, 'test')
|
||||
expect { configuration.find_and_execute_task('git:prepare_tree') }.to raise_error('git checkout master failed!')
|
||||
end
|
||||
|
||||
context "with a clean git tree" do
|
||||
before :each do
|
||||
configuration.set(:branch, 'master')
|
||||
configuration.set(:stage, 'test')
|
||||
end
|
||||
|
||||
it "raises an error if :stage or :branch are undefined" do
|
||||
with_clean_repo do
|
||||
configuration.unset(:branch)
|
||||
configuration.unset(:stage)
|
||||
expect { configuration.find_and_execute_task('git:prepare_tree') }.to raise_error('define :branch and :stage')
|
||||
end
|
||||
end
|
||||
|
||||
it "does not raise an error when run from a clean tree" do
|
||||
with_clean_repo do
|
||||
expect { configuration.find_and_execute_task('git:prepare_tree') }.to_not raise_error
|
||||
end
|
||||
end
|
||||
|
||||
it "does not run when :no_deploytags is defined by (i.e. by the stage)" do
|
||||
with_clean_repo do
|
||||
configuration.set(:no_deploytags, true)
|
||||
configuration.cdt.should_not_receive(:validate_git_vars)
|
||||
configuration.find_and_execute_task('git:prepare_tree')
|
||||
end
|
||||
end
|
||||
|
||||
it "uses a different remote when one is defined" do
|
||||
with_clean_repo do
|
||||
system('git remote add nowhere git@example.com:nowhere')
|
||||
configuration.set(:git_remote, 'nowhere')
|
||||
configuration.cdt.should_receive(:safe_run).with('git', 'checkout', 'master')
|
||||
configuration.cdt.should_receive(:safe_run).with('git', 'pull', 'nowhere', 'master')
|
||||
configuration.find_and_execute_task('git:prepare_tree')
|
||||
end
|
||||
end
|
||||
|
||||
it "uses the first remote when one is not specified" do
|
||||
with_clean_repo do
|
||||
system('git remote add somewhere git@example.com:somewhere')
|
||||
configuration.cdt.should_receive(:safe_run).with('git', 'checkout', 'master')
|
||||
configuration.cdt.should_receive(:safe_run).with('git', 'pull', 'somewhere', 'master')
|
||||
configuration.find_and_execute_task('git:prepare_tree')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "tagdeploy" do
|
||||
before :each do
|
||||
configuration.set(:branch, 'master')
|
||||
configuration.set(:stage, 'test')
|
||||
end
|
||||
|
||||
it "does not raise an error when run from a clean tree" do
|
||||
with_clean_repo do
|
||||
expect { configuration.find_and_execute_task('git:tagdeploy') }.to_not raise_error
|
||||
end
|
||||
end
|
||||
|
||||
it "adds appropriate git tags" do
|
||||
with_clean_repo do
|
||||
configuration.find_and_execute_task('git:tagdeploy')
|
||||
|
||||
tags = `git tag -l`.split(/\n/)
|
||||
tags.should have(1).items
|
||||
tags.first.should =~ /^test-\d{4}\.\d{2}\.\d{2}/
|
||||
end
|
||||
end
|
||||
|
||||
it "does not run when :no_deploytags is defined by (i.e. by the stage)" do
|
||||
with_clean_repo do
|
||||
configuration.set(:branch, 'master')
|
||||
configuration.set(:stage, 'test')
|
||||
configuration.set(:no_deploytags, true)
|
||||
configuration.cdt.should_not_receive(:validate_git_vars)
|
||||
expect { configuration.find_and_execute_task('git:prepare_tree') }.to_not raise_error
|
||||
end
|
||||
end
|
||||
end
|
||||
describe 'Capistrano::Deploytags' do
|
||||
pending "capistrano-spec doesn't support Capistrano 3"
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user