Do you love Continuous Integration? You like you environment cheap, but flexible and professional. Then you should go for:
This Gisthub shows you how to set up your environment from the scratch.
Set up EC2
Register an #EC2 instance. Trying to save cost, go for a free tier instance. In this tutorial I chose for an Ubuntu server.
In the security group give access to (inbound) port 8080, which will be used by Jenkins. Download the private key and put it in a file called security.pem. Call:
chmod 600 security.pem
From that folder log in to your EC2 instance through SSH by creating a script called 'connect':
#!/bin/bash ssh -i security.pem ubuntu@ec2-54-XXX-YYY-ZZZ.compute-1.amazonaws.com
Welcome! You're logged in. To find out your #Linux version, use:
I get the following outpout:
Linux domU-12-31-39-05-21-CD 3.8.0-19-generic #29-Ubuntu SMP Wed Apr 17 18:16:28 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Then install Jenkins:
wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list' sudo apt-get update sudo apt-get install jenkins
Add then new user 'jenkins' to the sudoers (this is important for the Jenkins jobs you will create later)
#create password for jenkins user sudo passwd jenkins #add to sudoers sudo adduser jenkins sudo #change to user jenkins for following tasks sudo su jenkins
Test if your Jenkins installation is working. Go to your browser and type in the following URL:
sudo apt-get install git
Install curl (perhaps):
sudo apt-get install curl
Install Ruby Version Manager:
curl -L https://get.rvm.io | bash -s stable --rails source /var/lib/jenkins/.rvm/scripts/rvm
Install MySQL Server:
sudo apt-get install mysql-server
Choose root password for MySQL. Finally restart your Jenkins service:
sudo service jenkins restart
Connect with GitHub
Create public/private key pair:
ssh-keygen -t rsa
At the first prompt press 'Enter' and don't use a password.
Open the private key and copy it to your clipboard:
Watch out to copy exactly the whole file. After this ssh into GitHub once (this is important, otherwise you won't be able to connect to GitHub later).
Log in to GitHub, go to Settings -> SSH Keys -> Add SSH key and copy the content of you clipboard to key.
Now log on one time:
ssh -T firstname.lastname@example.org
In GitHub go to Account settings -> Applications -> Register new application. In Main URL enter the path to your EC2 instance. In Callback URL enter the the path to your EC2 instance, appended by securityRealm/finishLogin.
Open Jenkins in your browser:
Install GitHub OAuth plugin in Jenkins under Manage Jenkins -> Manage Plugins -> Available
After this go to Manage Jenkins -> Manage Plugins -> Configure Global Security and enter Client ID and a Client Secret from GitHub Application registration process.
Under Configure Global Security -> Authorization -> GitHub Authorization Settings enter these GitHub users who should be granted admin rights.
Under Manage Jenkins -> Manage Plugins -> Available install GitHub plugin and Git plugin
Create RoR project
So it's time to create your Ruby on Rails app. On your local machine type:
rails new your_app
Now it's time to use Ruby Version Manager for your project:
# cd in your project cd your_app # Create .rvmrc file vim .rvmrc
In the .rvmrc file, define the Ruby version you want to use:
rvm use 2.0.0
Then create your repository in GitHub from GitHub's website. Next, upload your new local project to GitHub:
git init git add . git commit -m "first commit" git remote add origin https://github.com/username/your_app.git git push -u origin master
In the Gemfile you need to uncomment the following line, so that the build doesn't fail with Jenkins:
gem 'therubyracer', :platforms => :ruby
Set up New Job
First, go to 'Manage Plugins' and under 'Available' install the 'Git Plugin' and 'GitHub Plugin' and restart Jenkins.
Now in the Dashboard click on New Job and choose Build a free-style software project. Fill fields GitHub project according to helping comments and at Source Code Management choose for Git and enter the path to you GitHub project as follows:
At Build Triggers tick the box at Build when a change is pushed to GitHub and at Build choose for Execute shell and enter:
bundle install rake db:migrate rake
Now click on Save.
Now go to Manage Jenkins -> Configure System and below GitHub Web Hook click on the question mark next to Manually manage hook URLs and copy the server address in your clipboard. Next go to you GitHub project you want to connect with Jenkins and under Settings -> Service Hooks -> WebHook URLs paste the URL and click on Update Settings.
Finally you have to insert you GitHub 'user.name' and 'user.email' at 'Manage Jenkins' -> 'Configure System' and fill in the fields provided below 'Git plugin'. Now try it out and push your code to the GitHub repository with:
It works? If not write your error message in the comments!
Set up E-mail notifications with Gmail
Go to Manage Jenkins and Configure System at E-mail notification
- SMTP server: smtp.gmail.com
- Default user e-mail suffix: @email.com
- Use SMTP Authentication: yes
- User Name: Your full Gmail address (e.g. - email@example.com)
- Password: Your Gmail password
- Use SSL: yes
- SMTP Port: 465
- Reply-To Address: e.g. firstname.lastname@example.org
- Charset: UTF-8
Try to send a test E-mail. Then configure your job again and at Post-build Actions choose E-mail Notification and enter Recipients and choose Send e-mail for every unstable build
Install the HipChat Plugin under Manage Jenkins and Manage Plugins and and at Post-build Actions choose HipChat Notification. At Manage Jenkins -> Configure System enter API Token, Room, and Jenkins URL. These information will be provided by your HipChat account at Group Admin -> API. You might need to restart your Jenkins server:
sudo service jenkins restart
Using JUnit Test results
In order to get RCov to work you need to implement the ci_reporter Gem, so:
#edit your Gemfile vim Gemfile # Add the Gem gem 'ci_reporter', group: :test
Next, you need to edit your Rakefile and add these lines, according your test needs:
# edit Rakefile vim Rakefile # Add one of these codelines require 'ci/reporter/rake/rspec' # use this if you're using RSpec require 'ci/reporter/rake/cucumber' # use this if you're using Cucumber require 'ci/reporter/rake/spinach' # use this if you're using Spinach require 'ci/reporter/rake/test_unit' # use this if you're using Test::Unit require 'ci/reporter/rake/minitest' # use this if you're using Ruby 1.9 or minitest
Now ci_reporter is able to generate xml files, but you have to these rake tasks in the Execute Shell section of your job.
# Generate xml files rake ci:setup:testunit test rake ci:setup:minitest test rake ci:setup:rspec spec rake ci:setup:cucumber features rake ci:setup:spinach features
Under Post-build Actions, click Add post-build action and select Publish JUnit test result report. Fill in 'features/reports/*.xml,spec/reports/*.xml' in Test report XMLs. Then select Publish Rails stats report in the same action list.
Install Jenkins ruby metrics plugin on the plugin list under Jenkins -> Manage Jenkins -> Manage Plugins. Then extend your Gemfile:
# Edit Gemfile vim Gemfile # Add these lines gem 'simplecov', :require => false, :group => :test gem 'simplecov-rcov'
Add the following to the top of spec/spec_helper.rb or for rspec before RSpec.configure block.
require 'simplecov' require 'simplecov-rcov' SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter SimpleCov.start 'rails'
Select Publish Rcov report and fill coverage/rcov in Rcov report directory and leave the rest setting as is.
Deploy to Heroku after succesful build
First, create a Heroku application at Heroku's site. Then, install Heroku toolbelt on your Jenkins server
wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh
Log in to Heroku and add your SSH key. You need to specify the path to that ssh-key of the jenkins-user, in my case: /var/lib/jenkins/.ssh/id_rsa.pub
heroku login # Customize ssh-key path heroku keys:add /var/lib/jenkins/.ssh/id_rsa.pub
Now deploy your project to Heroku's Git repository from your local machine:
# First login to heroku heroku login # Enter your credentials git remote add production email@example.com:yourpoject.git git push production master
Set up the Heroku repository in Jenkins under the project 'Configure' -> 'Source Code Management'. Type in the path to your Heroku repository and type in a name for your production system under 'Name'.
Now, activate the Git Publisher under 'Add post-build actions' -> 'Git Publisher' and define which branch to push to your production system:
Going Headless (Optional)
Now that Jenkins is installed, we want to get a headless display configured for our browser based tests. First go to Manage Jenkins -> Manage Plugins -> Available and install the Hudson Xvnc plugin.
With Jenkins configured we need to ensure the required software is installed on the server:
sudo apt-get install vnc4server
vncserver requires a password to be set before it can be used, this needs to be set before Jenkins can make use of the vncserver. For this we need to switch to the jenkins user and set a password.
Enter a password, and verify it. Then kill the vncserver process:
vncserver -kill :1 # or whichever display the vncserver output mentioned
You have to install imagemagick with to see the screenshot or running browser via Xvnc
sudo apt-get install imagemagick
You might download VNC Viewer and then connect with your server. Find out the port number by (remember to edit EC2's firewall):
sudo netstat -tulpn