Skip to content

Instantly share code, notes, and snippets.

@jssjr
Created January 22, 2013 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jssjr/4597109 to your computer and use it in GitHub Desktop.
Save jssjr/4597109 to your computer and use it in GitHub Desktop.
Rake tasks for auto testing and uploading Chef cookbooks in Jenkins
require 'rubygems'
require 'chef'
require 'json'
# Load constants from rake config file.
require File.join(File.dirname(__FILE__), 'config', 'rake')
# Detect the version control system and assign to $vcs. Used by the update
# task in chef_repo.rake (below). The install task calls update, so this
# is run whenever the repo is installed.
#
# Comment out these lines to skip the update.
if File.directory?(File.join(TOPDIR, ".svn"))
$vcs = :svn
elsif File.directory?(File.join(TOPDIR, ".git"))
$vcs = :git
end
# Load common, useful tasks from Chef.
# rake -T to see the tasks this loads.
load 'chef/tasks/chef_repo.rake'
desc "Automated Test/Upload Task"
task :build, :cookbook do |t, args|
ruby_version = File.read(File.join(File.dirname(__FILE__),'.rbenv-version')).chomp
ruby_path = "/opt/rubies/#{ruby_version}/bin"
system <<-EOT
export PATH=#{ruby_path}:$PATH
bundle install --path=vendor --without development
bundle exec strainer test #{args.cookbook}
server_version=$(bundle exec knife cookbook list | grep " #{args.cookbook} " | awk '{print $2}')
local_version=$(awk '/version/{print $2}' cookbooks/#{args.cookbook}/metadata.rb | tr -d "'")
if bundle exec ruby -r solve -e "exit Solve::Version.new('$local_version') > Solve::Version.new('$server_version')" ; then
bundle exec knife spork upload #{args.cookbook}
else
echo "Skipping upload because built version (${local_version}) is not greater than server version (${server_version})"
fi
EOT
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment