未加星标

Setting Up Jenkins on Ubuntu 16.04 for Ruby on Rails CI

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

When looking for a hosted continuous integration platform, one problem is that most services don’t support self hosted repositories. I was setting up CI for a team that has git repos on self hosted Gitlab. Jenkins is a great option for such cases. In this article we’ll set up the following:

Jenkins 2.45 on Ubuntu 16.04 Nginx as reverse proxy for Jenkins Get an SSL certificate from Letsencrypt Install Ruby with RVM and Postgres 9.5 for Rails Run the Rails project’s tests on git push

I’m using a Digital Ocean VPS with 2GB RAM for this setup. Spin up a VPS with Ubuntu 16.04 and make sure you’re able to SSH as root before continuing.

Install Jenkins

First of all, we will use apt-get to install Jenkins.

wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | apt-key add - echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list apt update apt install -y jenkins

Go to http://your-ip-address:8080/ to get the Jenkins page. You will be asked for admin password to unlock Jenkins, which you can get here:

cat /var/lib/jenkins/secrets/initialAdminPassword

Once you have unlocked Jenkins, install the suggested plugins in the “getting started” wizard, and then create an admin account.

Install Ruby

Next, we will set up Ruby 2.3.3 on the server. Replace 2.3.3 below with whichever version you’re using.

apt install -y build-essential git-core curl nodejs curl -L https://get.rvm.io | bash -s stable --ruby=2.3.3 echo 'source "/var/lib/jenkins/.rvm/scripts/rvm"' >> /var/lib/jenkins/.bashrc chmod 755 /var/lib/jenkins/.bashrc su jenkins # switch to jenkins user rvm install 2.3.3 gem install bundler

Install Postgres

In this tutorial, we will be installing Postgres 9.5.

apt install -y libpq-dev postgresql postgresql-client

Make the following change in the pg_hba.conf file. You can find its location by running find / -name "pg_hba.conf" , but in our case its most likely location is /etc/postgresql/9.5/main/pg_hba.conf .

- local all postgres peer + local all postgres md5

Restart the postgres service with service postgresql restart . You might want to set up the database user for your project.

$ su - postgres $ psql postgres=# create user foobar with password 'mysecretpassword';

Install Nginx

Although we can access Jenkins on port 8080, it’s always a good idea to enable SSL on a site. The easiest way to do this is to set up Nginx as a reverse proxy to Jenkins, and use an SSL certificate from Let’s Encrypt. Let’s install Nginx:

apt install nginx

SSL with Letsencrypt

We can get a free SSL certificate from Lets Encrypt . It is as easy as running the following two commands.

$ apt install letsencrypt $ letsencrypt certonly --webroot -w /var/www/html -d ci.example.com - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/ci.example.com/fullchain.pem. Your cert will expire on 2017-05-11. To obtain a new version of the certificate in the future, simply run Let's Encrypt again.

Here ci.example.com should be changed to whatever subdomain you’re using. You can read more about setting up LetsEncrypt SSL certificates here .

Configure Nginx

Digital Ocean has an excellent tutorial on setting up Nginx as a reverse proxy for Jenkins . My Nginx conf is based on that tutorial, so please refer to that tutorial if you need to learn more about this config. Change /etc/nginx/sites-enabled/default to something like below. Replace ci.example.com with the subdomain you’ll be using.

upstream jenkins { server 127.0.0.1:8080 fail_timeout=0; } server { listen 80; return 301 https://$host$request_uri; } server { listen 443; server_name ci.example.com; ssl_certificate /etc/letsencrypt/live/ci.example.com/cert.pem; ssl_certificate_key /etc/letsencrypt/live/ci.example.com/privkey.pem; ssl on; ssl_session_cache builtin:1000 shared:SSL:10m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4; ssl_prefer_server_ciphers on; access_log /var/log/nginx/jenkins.access.log; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://jenkins; proxy_read_timeout 90; proxy_redirect http://jenkins https://ci.example.com; } } }

Now you should be able to access Jenkins on the subdomain using https.

Add public key

Before we set up Jenkins to run our tests, we need to make sure that it has access to our git repo. Create an SSH key pair and copy the public key. You will need to paste it as a deploy key for your project on Github, Gitlab or whichever host you’re using.

su jenkins cd ~ ssh-keygen -t rsa -C "nithin@example.com"

At this point, you should be able to git clone the repo when logged in as Jenkins.

Jenkins project configuration

Login to Jenkins dashboard and create a new freestyle project. You will be redirected to the project configuration page. Set the repository URL in the source code management section. In the build section, add a new build step and paste the following:

#!/bin/bash export RAILS_ENV=test source $HOME/.bashrc cd . # Force RVM to load the correct Ruby version bundle install bundle exec rails db:create db:schema:load db:migrate bundle exec rails test

Save the project, and check that the build is working by clicking the “build now” link.

Now that you have the build working, you need to make sure that a build is triggered whenever someone pushes code or creates a pull request. Our repo is on a self hosted Gitlab installation, so I needed to install the Gitlab plugin on Jenkins and enable the “Build when a change is pushed to GitLab” option. There are plugins available for other services as well.

One minor drawback of Jenkins is cluttered UI. Blue Ocean is a project aiming to bring a modern UI to Jenkins, and will eventually replace the existing UI. I highly recommend trying out the beta version, which is available as a Jenkins plugin.

Links Jenkins home page Blue Ocean - Modern UI for Jenkins How To Configure Nginx with SSL as a Reverse Proxy for Jenkins

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: RubyUbuntuNginxRuby on RailsRuby on RailGitVPSHIG2G
tags: Jenkins,jenkins,install,ci,proxy,example,set
分页:12
转载请注明
本文标题:Setting Up Jenkins on Ubuntu 16.04 for Ruby on Rails CI
本站链接:http://www.codesec.net/view/532749.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(69)