Skip to content

Instantly share code, notes, and snippets.

@wagnerjgoncalves
Last active April 19, 2023 07:27
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save wagnerjgoncalves/f80714a403b53532090b to your computer and use it in GitHub Desktop.
Save wagnerjgoncalves/f80714a403b53532090b to your computer and use it in GitHub Desktop.
Shell Script to install Jenkins at Ubuntu

Jenkins

Install

Execute the shell script: jenkins_install.sh

Configure a proxy using nginx or apache to http://ci.company.example.com.br(I'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 https://github.com/:company/:projet/
  • Click Configure services and select the Jenkins (GitHub plugin) option
  • Enter the address to your Jenkins server http://ci.example.com.br/github-webhook/, 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 http://ci.example.com.br/view/All/newJob

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

    GitHub project: fill with https://github.com/:company/:projet/

    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 git@github.com:company/project.git

    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):

    #!/bin/bash
    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
    rspec_status=$?
    
    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
  end

This is because the Jenkins shell is a pseudo TTY.

Slack Integration

To configure integration with Slack https://company.slack.com/services/new/jenkins-ci

#!/usr/bin/env bash
# This script install Jenkins in your Ubuntu System
#
# This script must be run as root:
# $ sudo ./jenkins_install.sh
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# 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 (http://phantomjs.org/build.html)
## 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://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.9
./build.sh
## 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 - 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
# 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';
FLUSH PRIVILEGES;\q"
# Create sample_database.yml
## BUIL_TAG is a String of "jenkins-${JOB_NAME}-${BUILD_NUMBER}":
## - JOB_NAME=company-branch
## - BUILD_NUMBER=99
## - 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
test:
<<: *default
database: <%= "sample-test-#{ENV['BUILD_TAG']}" %>
EOT
@suneeldudekula
Copy link

i am not understood

@cumtzhangqijie
Copy link

me too

@manu2melvin
Copy link

excellent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment