Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Shell Script to install Jenkins at Ubuntu



Execute the shell script:

Configure a proxy using nginx or apache to'll use this URL as example).

Configure credentials

Configure plugins:

Configure Shell section setting Shell executable

Configure GitHub Webhooks

  • Navigate to Webhooks & Services under Settings on the GitHub repository page
  • Click Configure services and select the Jenkins (GitHub plugin) option
  • Enter the address to your Jenkins server, check Active and update your settings.
  • Try it out by pushing a commit to GitHub and verifying that a new build is triggered in Jenkins

Create a new build configuration

  • Access

  • Add new item (Build a free-style multi-branch project)

    GitHub project: fill with

    Execute concurrent builds if necessary check this options

    Source Code Management:

    Sync Branches Schedule: H/2 * * * * (every two minutes)

    Git check this option

    Repository URL: fill with

    Credentials: select company-credentials or the name specified at Configure credentialsstep

    Include branches: fill with * to include all branchs

    Build Triggers:

    Check Build when a change is pushed to GitHub

    Build Environment:

    Check Color ANSI Console Output and select xterm from ANSI color map

    Build (Execute shell):

    cp /var/lib/jenkins/sample_database.yml config/database.yml
    function clean_up {
      RAILS_ENV=test bundle exec rake db:drop
      exit $1
    rvm use 2.2.1 --install
    rvm info
    ruby --version
    gem install bundler --no-rdoc --no-ri
    bundle install
    RAILS_ENV=test bundle exec rake db:create:all
    RAILS_ENV=test bundle exec rake db:migrate
    RAILS_ENV=test bundle exec rake db:test:prepare
    trap clean_up SIGHUP SIGINT SIGTERM
    SPEC_OPTS="--no-drb --format documentation"
    bundle exec rspec spec
    # Capture value returnd by last command
    clean_up $rspec_status

Rspec configuration

To get RSpec colours working in Jenkins you have to specify the following in the RSpec configuration block:

  RSpec.configure do |config|
   config.tty = true

This is because the Jenkins shell is a pseudo TTY.

Slack Integration

To configure integration with Slack

#!/usr/bin/env bash
# This script install Jenkins in your Ubuntu System
# This script must be run as root:
# $ sudo ./
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
# Install the necessary packages to prepare the environment
sudo apt-get install autoconf bison build-essential libffi-dev libssl-dev
sudo apt-get install libyaml-dev libreadline6 libreadline6-dev zlib1g zlib1g-dev curl git vim
# Install PhantomJS (
## First install the necessary packages
sudo apt-get install g++ flex gperf ruby perl libsqlite3-dev libfontconfig1-dev
sudo apt-get install libicu-dev libfreetype6 libssl-dev libpng-dev libjpeg-dev
## Then build PhantomJS
cd /usr/local/share
git clone git://
cd phantomjs
git checkout 1.9
## Then provide phantomjs to system
## to check the version of phantomjs user: $ phantomjs --version
sudo ln -sf /usr/local/share/phantomjs/bin/phantomjs /usr/local/bin
# Install Jenkins
## Before install is necessary to add Jenkins to trusted keys and source list
wget -q -O - | sudo apt-key add -
sudo sh -c 'echo deb binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
# Install and Configure Mysql to Jenkins
## Install the necessary packages (I used password: root)
sudo apt-get install mysql-client libmysqlclient-dev mysql-server
## Add user to jenkins
## You can check if user was created using: SELECT User FROM mysql.user;
mysql --user=root --password=root -e \
"CREATE USER 'jenkins'@'localhost' IDENTIFIED BY 'jenkins';
GRANT ALL PRIVILEGES ON * . * TO 'jenkins'@'localhost';
# Create sample_database.yml
## BUIL_TAG is a String of "jenkins-${JOB_NAME}-${BUILD_NUMBER}":
## - JOB_NAME=company-branch
## - BUIL_TAG=company-branch-99
sudo touch /var/lib/jenkins/sample_database.yml
sudo chmod 755 /var/lib/jenkins/sample_database.yml
cat <<EOT >> /var/lib/jenkins/sample_database.yml
default: &default
adapter: mysql2
username: 'jenkins'
password: 'jenkins'
host: 'localhost'
pool: 100
encoding: utf8
reconnect: true
<<: *default
database: <%= "sample-test-#{ENV['BUILD_TAG']}" %>

This comment has been minimized.

Copy link

commented Aug 2, 2018

i am not understood

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.