21 Commits
0.6.0 ... 0.8.0

Author SHA1 Message Date
Karl Matthias
06b6042ab9 Rev for release 0.8.0. 2013-08-27 15:24:18 +01:00
Karl Matthias
b0133c88c0 Use a configurable remote, or default to the first in git remote list.
Also, don't use double quotes where we don't need them.
2013-08-27 15:22:53 +01:00
Karl Matthias
e934cead87 Make it explicit that the plugin will do a git pull. 2013-08-27 14:28:24 +01:00
Karl Matthias
2dbec99914 Documenation bug fix. 2013-08-27 12:23:15 +01:00
Karl Matthias
c24bab3f37 Further documentation improvements. 2013-08-27 12:22:28 +01:00
Karl Matthias
3c6fc42623 Added explanation of how to disable tagging in the README. 2013-08-27 12:20:42 +01:00
Karl Matthias
bb520be7e3 Add ability to disable all tagging/git checking based on a setting. 2013-08-27 12:15:34 +01:00
Karl Matthias
a24c306e6b Updated README to talk about editing deployment scripts. 2013-08-27 11:53:38 +01:00
Karl Matthias
221b7246c1 Added clarification on deployment history. 2013-07-29 12:08:14 +01:00
Karl Matthias
b85a7a3e48 Added info to README on how to show your deployment history. 2013-07-29 12:06:33 +01:00
Karl Matthias
01f5e36a89 Update on deploying a previous commit. 2013-05-15 09:09:47 +01:00
Karl Matthias
2bd711ab9c Rev for release 0.7.0. 2012-10-02 20:49:46 +01:00
relistan
f3fb400e98 Merge pull request #7 from pusewicz/master
Fix tagging on deploy:migrations
2012-10-02 12:40:32 -07:00
Piotr Usewicz
029074e3d4 Fix deploy:migrations tagging 2012-10-02 11:45:01 +01:00
Karl Matthias
c76311a2b8 Use more modern rspec syntax for lambda 2012-09-28 20:26:21 +01:00
Karl Matthias
c38a4dc9d5 Fix test broken by one-line pul request! 2012-09-28 20:05:14 +01:00
relistan
ae3b749937 Merge pull request #4 from mydrive/gh3_inconsistent_validation_vs_exception
Issue 3: bring exception message inline with code
2012-09-28 12:00:19 -07:00
Barrie Bremner
f713b5ab48 GH issue 3: bring exception message inline with code, :branch _and_ :stage must be defined. 2012-09-26 11:27:57 +01:00
Karl Matthias
68619ccdea Added helpful git config. 2012-08-30 20:50:13 +01:00
Gavin Heavyside
982dc81bd9 Update README.md 2012-07-29 13:17:21 +02:00
Karl Matthias
7f17000d0c Update description for rubygems. 2012-05-30 23:00:35 +01:00
4 changed files with 163 additions and 31 deletions

View File

@@ -39,17 +39,97 @@ In your Capistrano `config/deploy.rb` you should add:
This will create two tasks, one that runs before deployment and one
that runs after.
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 codebase. You should be deploying from a
clean checkout anyway, so in most cases this is not a restriction
on how you already do things. The plugin will check if your code
is clean and complain if it is not.
*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
codebase. You should be deploying from a clean checkout anyway, so in most
cases this is not a restriction on how you already do things. The plugin will
check if your code is clean and complain if it is not.
*ALSO:* The plugin will do a pull to make sure you have the code on your local
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`.
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
before you deploy. You do not have to push them. The plugin will then
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
-----------------------------
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:
```ruby
set :no_deploytags, true
```
You can also set this from the command line at any time with `-S 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
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
--------------------------
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:
```
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
```
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
------------------
You might find it useful to add this to your ~/.gitconfig in order
to get a nice history view of the commits and tags.
```ini
[alias]
lol = log --pretty=oneline --abbrev-commit --graph --decorate
```
You can then view the list by typing `git lol` from the checked out
code path.
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.
Credits
-------
This software was written by [Karl Matthias](https://github.com/relistan)
with help from [Gavin Heavyside](https://github.com/hgavin) and the
with help from [Gavin Heavyside](https://github.com/gavinheavyside) and the
support of [MyDrive Solutions Limited](http://mydrivesolutions.com).
License

View File

@@ -1,10 +1,10 @@
Gem::Specification.new do |s|
s.name = 'capistrano-deploytags'
s.version = '0.6.0'
s.date = '2012-05-30'
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, and will move the *-latest tag 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.
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.
EOS
s.authors = ["Karl Matthias"]
s.email = 'relistan@gmail.com'

View File

@@ -4,19 +4,19 @@ module Capistrano
# 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 or :stage'
logger.log Capistrano::Logger::IMPORTANT, 'Capistrano Deploytags requires that :branch and :stage be defined.'
raise 'define :branch and :stage'
end
end
@@ -28,38 +28,48 @@ module Capistrano
!`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'
after :deploy, 'git:tagdeploy'
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
cdt.safe_run "git", "pull", "origin", branch if cdt.has_remote?
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?
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

View File

@@ -28,21 +28,53 @@ describe Capistrano::DeployTags do
FileUtils.chdir '/tmp'
configuration.set(:branch, 'master')
configuration.set(:stage, 'test')
lambda { configuration.find_and_execute_task('git:prepare_tree') }.should raise_error('git checkout master failed!')
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
lambda { configuration.find_and_execute_task('git:prepare_tree') }.should raise_error('define :branch or :stage')
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
configuration.set(:branch, 'master')
configuration.set(:stage, 'test')
lambda { configuration.find_and_execute_task('git:prepare_tree') }.should_not raise_error
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
@@ -56,7 +88,7 @@ describe Capistrano::DeployTags do
it "does not raise an error when run from a clean tree" do
with_clean_repo do
lambda { configuration.find_and_execute_task('git:tagdeploy') }.should_not raise_error
expect { configuration.find_and_execute_task('git:tagdeploy') }.to_not raise_error
end
end
@@ -69,5 +101,15 @@ describe Capistrano::DeployTags do
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
end