FacebookLinkedInShare

Intro

Currently we’re working on new continues integration process which will be responsible for deploying all the environment in our new micro services architecture, when the  will be done it will be described in a blog.

There are a lot of posts that described how to do a specified link in the chain of CI process,
In this post we will describe the whole process, and the steps to build a simple CI process for the following infrastructure:

  • Ruby 2.1.2
  • Rails 4.1.4
  • rspec-rails 3.1.0
  • Jenkins
  • Github repository

Summary

The process will monitor with Jenkins every minute if there is a new commit, than create a build and run the unit tests with coverage report, at the end it will notify by mail about the success or failure of the process.

Step 1: Install Jenkins plugins

For installing new plugins in Jenkins : Jenkins -> Manage Jenkins -> Plugin Manager -> Available

Select the following plugins and click “Download without restart”

Step2: Create Git integration branch

Integration branch will be monitored by the Jenkins, and will execute the unit test sequence once there is a new commit.

$ git checkout -b integration
$ git push origin integration

Step 3: Ruby application configuration

Add the following Gems to your Gemfile under ‘test’ and ‘development’ group:

group :development, :test do
  gem 'rspec-rails' # Rails unit tests framework
  gem 'simplecov' # Ruby unit tests coverage framework
  gem 'simplecov-rcov' # Allows to inspect simplecov output in Jenkins
  gem 'rspec_junit_formatter' # Allows to inspect rspec output as junit output in Jenkins
end

Unit test coverage configuration the add the following code to rails_helper.rb:

require 'simplecov'
require 'simplecov-rcov'
SimpleCov.formatters = [
    SimpleCov::Formatter::RcovFormatter, # Output for Jenkins
    SimpleCov::Formatter::HTMLFormatter # Output for development environment
]
SimpleCov.start do
  add_filter 'db'
  add_filter 'config'
  add_filter 'script'
  add_filter 'bin'
  add_filter 'lib/tasks'
  add_filter 'spec'
end

We ignore those directories because there is not real rails code in there that we test in the our units test.

Add the following rake task for version management ‘lib/tasks/version’, also this rake will create a git tag for each successful build.

namespace :ci do

  desc "Increases the minor version, and commits the changes. version x.y.z will be x.y.z+1"
  task :bump_build => :environment do
    version = get_splitted_version
    version[1] = (version[1].to_i + 1).to_s
    bump_version('v' + version.join('.'))
  end

  def get_splitted_version
    version = File.read("config/version.yml")
    version = version[7..-1]
    version.split('.')
  end

  def bump_version(version)

    write_version_yml(version)
    add_version_to_release_notes(version)

    exec_command("git add config/version.yml")
    exec_command("git add doc/release_notes.md")
    exec_command("git commit -m 'bump version #{version}' config/version.yml doc/release_notes.md")

    tag_version(version)
  end

  def write_version_yml(version)
    File.open("config/version.yml", 'w') { |f| f.write("name: #{version}") }
  end

  def add_version_to_release_notes(version)
    version_addition = "# version #{version}"
    version_addition += "\n#### #{Time.now.to_formatted_s(:long)}"
    version_addition = "\n\n" + version_addition + "\n" + "-" * version_addition.length + "\n\n"
  end

  def tag_version(version)
    exec_command("git tag #{version} -a  -m '#{version}'")
    exec_command("git push origin #{version}")
  end

  def exec_command(cmd)
    puts "Executing - '#{cmd}'..."
    success = system(cmd)
    raise "'#{cmd}' exited with code:#{$?.exitstatus}." if !success
  end
end

Step 4: Jenkins configuration

Now when all the application and the git configuration are ready we need to define the job in Jenkins.

We will start by adding ‘New item‘ -> ‘Build a free-style software project’ 

And define basic configuration such as :

  • Project name
  • Log rotation – how many build history to keep.
  • Git repository

Screen Shot 2015-03-17 at 10.57.02 PM

Git configuration is an actually a cron job that set to run every minute and monitor the git repository for changes:Screen Shot 2015-03-17 at 11.03.08 PM

Setting the build configuration, should set the ruby version and the script that run the tests:

Screen Shot 2015-03-17 at 11.09.14 PM

The next step is to configure the unit tests and the coverage reports:

Screen Shot 2015-03-17 at 11.04.23 PM

 

The final step is to configure email notification:

Screen Shot 2015-03-17 at 11.20.13 PM

Summary

This posts describes how to setup the application, Jenkins and Git for simple continues integration process, that monitors git and executes unit test each commit and creates git tag for that build.

  • Ajeet Khan

    To add a rake task, lib/tasks/version here version is a folder or the name of the task?