Thursday, December 20, 2012

Rake task to update build version No. with latest tag name

The following code will help us to automate the process of updating build version No. by considering latest tag name available in git repo.

Assuming, you are deploying your Rails app using Capistrano.

1) In your deploy.rb add the following hook,

after "deploy", "installed_version:update"

And add a task(installed_version:update)

namespace :installed_version do
 task :update, :roles => :app, :except => { :no_release => true } do
   run "cd #{current_path} && RAILS_ENV=#{rails_env} rake update:version"
 end
end


2) Create a new rake task file in your rails app(lib/tasks/update_build_version.rake)

Add below lines,

namespace "update" do
 desc "This will update the installed version no. in DB from the latest tag name"
 task :version => :environment do
    tag = `git rev-list --tags --max-count=1`
    tag_name = `git describe --tags #{tag}`.chomp
    puts "Latest Tag No.#{tag_name}"
    attributes = {:version_no => tag_name, :build_dt => Time.now}
   Version.create(attributes) 
# assuming you have versions tbl   
    puts "Build updated with Version No.\"#{tag_name}\""
 end
end



That's all, we are done !!!

Note: Before every deployment you need to tag your code with some version No.(ex: 1.2.1) and then run cap deploy. The above code will read the latest tag name(1.2.1) and update into versions table.

This post would be really helpful when you want to automate your Build Management process.

Tuesday, December 11, 2012

Rake Task with Arguments

The following example shows how to pass an argument to Rake Task. Here version No(:no) gets passed to task and the same gets saved into versions table. This example can be incorporated with your build management script to update the current version No. for each release.

namespace "build" do
 desc "This will update the version No."
 task :version, [:no] => :environment do |t, args|
   raise "Message: Version Number Required to build !!!" if args.no.nil?
   version = Version.first # assume you have a table named versions(:id, :version_no, :updated_at...)
   version.update_attributes(:version_no => args.no)
   puts "Build updated with Version No.#{args.no}"
 end
end


Usage:  rake "build:version[1.3.2]"

Tuesday, November 27, 2012

Git Tag

Tag  (people use this functionality to mark release points)

git tag  -- Listing Your Tags

git tag -l ’v1.4.2.*’  -- wildcard Tag listing

git tag v1.4    -- Creates Lightweight Tag

git tag -a v1.4 -m ’my version 1.4’  -- Creates Annotated Tag

git tag -s v1.5 -m ’my signed 1.5 tag’ -- sign your tags with GPG, assuming you have a private key.

git show v1.4  -- Shows the last commit of a Tag

git tag -a v1.2 9fceb02  -- Tagging later (if you forgot to Tag a release point)

git push origin v1.2  -- Sharing Tags (pushing to remote origin)

git push origin --tags --  Transfer all of your tags to the remote server that are not already there

git tag -d v1.2 -- Delete a Tag

git checkout -b v1.4-fixes v1.4  -- Creates a branch(v1.4-fixes) from Tag(v1.4)

git diff v1.1 v1.2 --stat  --  Shows no of files changed between two tags

git log --pretty=oneline v1.1..v1.2   -- 
Shows no of commits between two tags