View on GitHub

Jenkins Workshop

by Titas Norkūnas

Download this project as a .zip file Download this project as a tar.gz file

Step 1: Download and start Jenkins

mkdir ~/jenkins
cd ~/jenkins
wget http://mirrors.jenkins-ci.org/war/latest/jenkins.war
java -jar jenkins.war

Step 2: Install Git Plugin

Go to: http://localhost:8080/pluginManager/available

Find and install a plugin called "Git Plugin"

Step 3: Creating a job (1)

Go to: http://localhost:8080/newJob

Build a freestyle project called "jenkins-workshop"

Select Git as Source Code Management and enter parameters.

Remote: git://github.com/titas/jenkins-workshop.git

Branch: origin/workshop

Select Build Trigger.

Poll SCM (every minute): * * * * *

Step 4: Creating a job (2)

Create touch ~/.jenkins/setup.rb file. This will be used to create required settings for your build.

#!/usr/bin/env ruby

db_name      = ENV["JOB_NAME"].downcase
project_path = ENV["WORKSPACE"]
database_yml = "#{project_path}/config/database.yml"

default_template = <<-EOF
test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: jenkins-workshop-test
  pool: 5
  username: <your-username>
  password: <your-password>
  socket: /tmp/mysql.sock
EOF

`touch #{database_yml}`
File.open(database_yml, 'w') { |f| f.write(default_template) }

Make this file executable.

chmod +x ~/.jenkins/setup.rb

Step 5: Creating a job (3)

Add build step - Execute Shell.

export RAILS_ENV=test        # use test environment
bundle install               # always do bundle install before running job

Add build step - Execute Shell.

export RAILS_ENV=test        # use test environment
~/.jenkins/setup.rb          # setup your job's settings

Add build step - Execute Shell.

export RAILS_ENV=test                          # use test environment
bundle exec rake db:drop db:create db:migrate  # cleanup if last build went bad

Add build step - Execute Shell.

export RAILS_ENV=test        # use test environment
bundle exec rake             # run the build

Add build step - Execute Shell.

export RAILS_ENV=test        # use test environment
bundle exec rake db:drop     # cleanup afterwards

You want to have individualized build steps because Jenkins will only read the result of the last command in the step for determining if the step failed or not.

Click Save, observe your first build: http://localhost:8080/job/jenkins-workshop/1/console

Step 6: Configure CI reporter

Lets configure ci_reporter gem in the project. This will let us see some stats about build failures.

We need to change the way we run our test:

Change your build step Execute Shell - replace bundle exec rake with bundle exec rake ci:setup:minitest test

Add a post build step - Publish JUnit test result report.

test/reports/*.xml

I will push these changes once everyone is ready. https://github.com/titas/jenkins-workshop/compare/workshop...ci-reporter

There will be no changes after this build runs http://localhost:8080/job/jenkins-workshop/2/console

Trigger one more build manually: http://localhost:8080/job/jenkins-workshop/build?delay=0sec

After this one is finished, take a look at build overview page: http://localhost:8080/job/jenkins-workshop/

Step 7: Configure email notifications

Take a look at E-mail Notification section in here: http://localhost:8080/configure

SMTP server: smtp.gmail.com

Default suffix: @gmail.com

Sender: <your-login@gmail.com>

Use authentication: true

User name: <your-login>

Password: <your-password>

Port: 465

Reply-To: noreply@gmail.com

Charset: UTF-8

Click Test Configuration to check if everything is alright.

Add a post build action to the job http://localhost:8080/job/jenkins-workshop/configure to send an email to <your@email.com>

Let me break the build: https://github.com/titas/jenkins-workshop/compare/ci-reporter...break-build

Step 8: Finished..?

Almost!

Let's fix the build: https://github.com/titas/jenkins-workshop/compare/break-build...fix-build

CI reporter did its job: http://localhost:8080/job/jenkins-workshop/

Now we're done. Thanks!