Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
06b6042ab9 | ||
|
|
b0133c88c0 | ||
|
|
e934cead87 | ||
|
|
2dbec99914 | ||
|
|
c24bab3f37 | ||
|
|
3c6fc42623 | ||
|
|
bb520be7e3 | ||
|
|
a24c306e6b | ||
|
|
221b7246c1 | ||
|
|
b85a7a3e48 | ||
|
|
01f5e36a89 | ||
|
|
2bd711ab9c | ||
|
|
f3fb400e98 | ||
|
|
029074e3d4 | ||
|
|
c76311a2b8 | ||
|
|
c38a4dc9d5 | ||
|
|
ae3b749937 | ||
|
|
f713b5ab48 | ||
|
|
68619ccdea | ||
|
|
982dc81bd9 | ||
|
|
7f17000d0c |
94
README.md
94
README.md
@@ -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
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user