1. 程式人生 > >CentOS7搭建CI(GitLab + Gerrit + Jenkins)系統

CentOS7搭建CI(GitLab + Gerrit + Jenkins)系統

1、安裝GitLab:

①、安裝需要的擴充套件/應用:

yum -y install curl policycoreutils openssh-server openssh-clients postfix cronie
yum -y install readline readline-devel ncurses-devel gdbm-devel glibc-devel tcl-devel openssl-devel expat-devel db4-devel byacc sqlite-devel libyaml libyaml-devel libffi libffi-devel libxml2 libxml2-devel
libxslt libxslt-devel libicu libicu-devel system-config-firewall-tui crontabs logwatch logrotate perl-Time-HiRes git cmake libcom_err-devel libcom_err-devel git patch gcc-c++ zlib-devel make autoconf automake libtool bison python python-docutils wget ruby sudo systemctl enable sshd sudo systemctl start sshd sudo systemctl enable postfix sudo systemctl start postfix sudo chkconfig postfix on

②、更換Ruby源:

gem sources -r https://rubygems.org/
gem sources -a https://ruby.taobao.org/

③、新增yum源,使用yum源安裝:

Ⅰ、這種是官方的yum源,在國內速度可能會很慢:

curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo yum -y install gitlab-ce

Ⅱ、使用國內的yum源:
PS.具體源內容可以參考官方說明:
修改使用GitLab國內源


首先新增原始檔

sudo vi /etc/yum.repos.d/gitlab_gitlab-ce.repo

修改原始檔內容:

[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key

然後執行如下命令:

sudo yum makecache
sudo yum -y install gitlab-ce

④、用預設配置配置GitLab:

sudo gitlab-ctl reconfigure

⑤、按個人需求修改GitLab配置:

PS.因為這裡要用已有的Nginx作為GitLab的伺服器,所以要先進行配置,GitLab的配置檔案位置:

/etc/gitlab/gitlab.rb

我這裡因為提前配置了gitlab的域名是http://git.xxx.com,接下來修改/etc/gitlab/gitlab.rb以下內容,在修改內容裡要注意一點就是web_server設定的值是你nginx的配置,注意每行前面不要有空格,需要特別注意 的是因為GitLab使用的是Ruby,預設開啟埠是8080:

# note the 'https' below
external_url 'http://git.xxx.com'

# Set the web server
web_server['external_users'] = ['www']

# Disable the built-in nginx
nginx['enable'] = false

上面是gitlab配置檔案需要修改的地方,重新生成配置:

sudo gitlab-ctl reconfigure

接下來配置Nginx虛擬主機,

⑥、新增nginx虛擬主機,支援gitlab:

新增gitlab.conf:

vi /usr/local/nginx/conf/vhost/gitlab.conf

在gitlab.conf中新增以下模板內容:

## GitLab
##
## Modified from nginx http version
## Modified from http://blog.phusion.nl/2012/04/21/tutorial-setting-up-gitlab-on-debian-6/
## Modified from https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html
##
## Lines starting with two hashes (##) are comments with information.
## Lines starting with one hash (#) are configuration parameters that can be uncommented.
##
##################################
##        CONTRIBUTING          ##
##################################
##
## If you change this file in a Merge Request, please also create
## a Merge Request on https://gitlab.com/gitlab-org/omnibus-gitlab/merge_requests
##
###################################
##         configuration         ##
###################################
##
## See installation.md#using-https for additional HTTPS configuration details.

upstream gitlab-workhorse {
  server unix:/var/opt/gitlab/gitlab-workhorse/socket fail_timeout=0;
}

## HTTPS host
server {
  listen  80;
  server_name git.xxx.com; ## Replace this with something like gitlab.example.com
  server_tokens off; ## Don't show the nginx version number, a security best practice
  root /opt/gitlab/embedded/service/gitlab-rails/public;
  include pagespeed.conf;
  ## Individual nginx logs for this GitLab vhost
  access_log  /data/log/nginx/gitlab.access.log;
  error_log   /data/log/nginx/gitlab.error.log;
  location / {
    client_max_body_size 0;
    gzip on;

    ## https://github.com/gitlabhq/gitlabhq/issues/694
    ## Some requests take more than 30 seconds.
    proxy_read_timeout      300;
    proxy_connect_timeout   300;
    proxy_redirect          off;

    proxy_http_version 1.1;

    proxy_set_header    Host                $http_host;
    proxy_set_header    X-Real-IP           $remote_addr;
    proxy_set_header    X-Forwarded-Ssl     off;
    proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    proxy_set_header    X-Forwarded-Proto   $scheme;
    proxy_set_header    Upgrade $http_upgrade;
    proxy_set_header    Connection "upgrade";
    proxy_pass http://gitlab-workhorse;
  }
}

⑦、修改防火牆設定:

#為public域開放tcp協議的8080埠
firewall-cmd --zone=public --add-port=8080/tcp --permanent
#為public域新增http服務
firewall-cmd --zone=public --add-service=http --permanent
#重啟firewall服務
firewall-cmd --reload

⑧、啟動:

啟動GitLab:

sudo gitlab-ctl start/stop/restart

重啟Nginx:

nginx -s reload
nginx -s reopen

接下來就可以訪問git.xxx.com操作gitlab了。

⑨、修改GitLab預設影象路徑:

由於GitLab使用的是Gravatar的頭像,但Gravatar服務在國內不能正常訪問http的影象資訊導致Gitlab的頭像全都裂了,可以採用下面的方法來解決這個問題:
修改/etc/gitlab/gitlab.rb檔案中gravatar_plain_url的值,開啟檔案,搜尋gravatar_plain_url,如果想要在原來的基礎上改,就去掉該行前面的#,然後修改gravatar_plain_url值為

http://gravatar.duoshuo.com/avatar/%{hash}?s=%{size}&d=identicon

然後執行下面的命令:

sudo gitlab-ctl reconfigure 
sudo gitlab-rake cache:clear RAILS_ENV=production
sudo gitlab-ctl restart

完成之後GitLab使用的就是國內多說的頭像了,如果你的網站採用的是https模式,可以修改gitlab_rails['gravatar_plain_url']值為如下內容:

gitlab_rails['gravatar_plain_url'] = 'https://secure.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon'

然後執行上面的重新配置/重啟命令。

2、安裝Jenkins:

①、安裝yum源:

cd ~&&  wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key

②、安裝jenkins並修改啟動埠:

sudo yum -y install java jenkins

修改/etc/sysconfig/jenkins配置:

JENKINS_PORT="8081"

③、啟動jenkins,設定開機啟動:

sudo service jenkins start/stop/restart
sudo chkconfig jenkins on

④、修改防火牆設定:

firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --reload

⑤、使用Nginx作為Web伺服器:

安裝完成之後,訪問http://localhost:8080/就可以看到jenkins的主頁面了,然後根據提示輸入隨機生成的密碼,登入jenkins,如果想使用已有的Web伺服器訪問jenkins,可以進行如下配置(我這裡直接使用Nginx,Apache的配置官方有說明),

vi /etc/nginxhost/jenkins.conf 

在配置檔案中寫入如下配置:

server {
  listen          80;       # Listen on port 80 for IPv4 requests
  server_name     jenkins.xxx.com;
  server_tokens   off;
  #this is the jenkins web root directory (mentioned in the /etc/default/jenkins file)
  root            /var/run/jenkins/war/;
  include pagespeed.conf;
  include proxy.conf;
  access_log      /data/log/nginx/jenkins.access.log;
  error_log       /data/log/nginx/jenkins.error.log;

  location ~ "^/static/[0-9a-fA-F]{8}\/(.*)$" {

    #rewrite all static files into requests to the root
    #E.g /static/12345678/css/something.css will become /css/something.css
    rewrite "^/static/[0-9a-fA-F]{8}\/(.*)" /$1 last;
  }

  location /userContent {
        #have nginx handle all the static requests to the userContent folder files
        #note : This is the $JENKINS_HOME dir
        root /var/lib/jenkins/;
        if (!-f $request_filename){
           #this file does not exist, might be a directory or a /**view** url
           rewrite (.*) /$1 last;
           break;
        }
        sendfile on;
  }

  location @jenkins {
      sendfile off;
      proxy_pass         http://127.0.0.1:8081;
      proxy_redirect     default;
  }
  location / {

     # Optional configuration to detect and redirect iPhones
      if ($http_user_agent ~* '(iPhone|iPod)') {
          rewrite ^/$ /view/iphone/ redirect;
      }

      try_files $uri @jenkins;
   }
}

重啟Nginx,訪問http://jenkins.xxx.com就可以訪問jenkins了,安裝完成更新外掛時會發現外掛更新比較慢,可以設定新的更新站點,不用官方的更新點:
①、首先測試更新資料是否可用:
訪問http://mirror.xmission.com/jenkins/updates/current/update-center.json,如果得到結果就說明更新點可用,如果不可訪問,就直接使用官方預設的更新源;
②、測試可用之後在“Manage Jenkins”——>“Manage Plugins”——>“Advanced”中設定“Update Site”為如下地址:

http://mirror.xmission.com/jenkins/updates/current/update-center.json

3、安裝Gerrit:

①、下載gerrit2.12.2:

cd ~ && wget https://gerrit-releases.storage.googleapis.com/gerrit-2.12.2.war

②、安裝前準備:
Gerrit支援MySQL、PostgreSQL等,我這裡在使用MySQL時發現有一個建立資料表報錯的提示,也不願意再去折騰,就改用PostgreSQL,PostgreSQL的安裝使用,請參考文章:Centos7安裝PostgreSQL9.5,安裝完成之後,為Gerrit建立資料庫reviewdb

createdb -h 127.0.0.1 -p 5432 -U postgres reviewdb

輸入postgres的密碼後就建立了資料庫reviewdb
③、安裝gerrit至/var/opt/gerrit

cd ~ && java -jar gerrit-2.12.2.war init -d /var/opt/gerrit 

操作及顯示結果如下:

*** Gerrit Code Review 2.12.2
*** 

Create '/var/opt/gerrit'       [Y/n]? y

*** Git Repositories
*** 

Location of Git repositories   [git]: 

*** SQL Database
*** 

Database server type           [h2]: postgresql
Server hostname                [localhost]: 
Server port                    [(postgresql default)]: 
Database name                  [reviewdb]: 
Database username              [root]: postgres
postgres's password            : 
              confirm password : 

*** Index
*** 

Type                           [LUCENE/?]: 

*** User Authentication
*** 

Authentication method          [OPENID/?]: http
Get username from custom HTTP header [y/N]? 
SSO logout URL                 : 
Enable signed push support     [y/N]? 

*** Review Labels
*** 

Install Verified label         [y/N]? 

*** Email Delivery
*** 

SMTP server hostname           [localhost]: 
SMTP server port               [(default)]: 
SMTP encryption                [NONE/?]: 
SMTP username                  : [email protected]
[email protected]'s password : 
              confirm password : 

*** Container Process
*** 

Run as                         [root]: 
Java runtime                   [/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.101-2.6.6.1.el7_2.x86_64/jre]: 
Copy gerrit-2.12.2.war to /var/opt/gerrit/bin/gerrit.war [Y/n]? 
Copying gerrit-2.12.2.war to /var/opt/gerrit/bin/gerrit.war

*** SSH Daemon
*** 

Listen on address              [*]: 
Listen on port                 [29418]: 

Gerrit Code Review is not shipped with Bouncy Castle Crypto SSL v152
  If available, Gerrit can take advantage of features
  in the library, but will also function without it.
Download and install it now [Y/n]? 
Downloading http://www.bouncycastle.org/download/bcpkix-jdk15on-152.jar ... Failed to clean up lib: /var/opt/gerrit/lib/bcpkix-jdk15on-152.jar
 !! FAIL !!


error: http://www.bouncycastle.org/download/bcpkix-jdk15on-152.jar: 302 Found
Please download:

  http://www.bouncycastle.org/download/bcpkix-jdk15on-152.jar

and save as:

  /var/opt/gerrit/lib/bcpkix-jdk15on-152.jar

Press enter to continue 
Continue without this library  [Y/n]? 
Generating SSH host key ... rsa(simple)... done

*** HTTP Daemon
*** 

Behind reverse proxy           [y/N]? y
Proxy uses SSL (https://)      [y/N]? 
Subdirectory on proxy server   [/]: 
Listen on address              [*]:    
Listen on port                 [8081]: 8082
Canonical URL                  [http://localhost/]: http://g.example.com

*** Plugins
*** 

Installing plugins.
Install plugin singleusergroup version v2.12.2 [y/N]? y
Install plugin commit-message-length-validator version v2.12.2 [y/N]? y
Install plugin reviewnotes version v2.12.2 [y/N]? y
Install plugin replication version v2.12.2 [y/N]? y
Install plugin download-commands version v2.12.2 [y/N]? y
Initializing plugins.
No plugins found with init steps.

Initialized /var/opt/gerrit
Executing /var/opt/gerrit/bin/gerrit.sh start
Starting Gerrit Code Review: OK
Waiting for server on g.maidoumi.cn:80 ... OK
Opening http://g.example.com/#/admin/projects/ ...FAILED
Open Gerrit with a JavaScript capable browser:
  http://g.example.com/#/admin/projects/

④、安裝完成,設定Gerrit:
新增檔案gerritcodereview

vi /etc/default/gerritcodereview

檔案內容如下:

GERRIT_SITE=/var/opt/gerrit

⑤、啟動Gerrit,並設定Gerrit自啟動:
建立啟動軟鏈:

ln -sf /var/opt/gerrit/bin/gerrit.sh /usr/bin/gerrit

啟動Gerrit:

gerrit start

設定自啟動:

echo -e 'gerrit start\n' >> /etc/rc.local

⑥、設定Gerrit認證:
使用htpasswd命令為管理雲使用者生成http認證配置檔案,如果沒有htpasswd檔案需要安裝apache2-utils包。
新增admin使用者的驗證檔案,放在/var/opt/gerrit下,並將admin作為管理員:

cd /var/opt/gerrit && htpasswd -d htpasswd.conf admin

輸入兩次密碼之後就添加了admin使用者,以後新增gerrit使用者時,同樣需要先為其配置http認證,然後使用者登入後gerrit會為其自動生成使用者帳號,名字與http認證名字一致,第一次登入成功之後就會要求你設定基本驗證。
⑦、設定Nginx代理:

vi /etc/nginxhost/gerrit.conf

檔案內容如下:

server {
        listen       80;
        server_name  g.example.com;
        ## Don't show the nginx version number, a security best practice
        server_tokens off;
        include proxy.conf;
        include pagespeed.conf;
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        access_log  /data/log/nginx/gerrit.access.log  main;
        error_log   /data/log/nginx/gerrit.error.log;
        location / {
                auth_basic              "Gerrit2 Code Review";
                auth_basic_user_file    /var/opt/gerrit/htpasswd.conf;
                proxy_pass              http://127.0.0.1:8082;
                proxy_set_header        X-Forwarded-For $remote_addr;
                proxy_set_header        Host $host;
        }
        location /login/ {
                proxy_pass              http://127.0.0.1:8082;
                proxy_set_header        X-Forwarded-For $remote_addr;
                proxy_set_header        Host $host;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires       1d;
        }
        location ~ .*\.(js|css|html)?$
        {
                expires       1d;
        }
}

PS.配置檔案中的proxy.conf;pagespeed.conf具體寫法請參考我之前的文章。
之後重啟Nginx和Gerrit:

nginx -s reload
nginx -s reopen
gerrit restart

至此CI系統所需的三個產品都完成了安裝,接下來就是配合使用了。

相關推薦

CentOS7搭建CIGitLab + Gerrit + Jenkins系統

1、安裝GitLab: ①、安裝需要的擴充套件/應用: yum -y install curl policycoreutils openssh-server openssh-clients postfix cronie yum -y install

簡單Gitlab伺服器的搭建Gitlab+GerRit+Jenkins

目前程式碼版本控制分為兩種:集中式管理SVN和分散式管理Git,微軟近期以75億美元收購了Github平臺,而GitLab也頗受企業喜愛,因為Github平臺只能提供一個私有倉庫,而Gitlab伺服器可以企業自己搭建。下面就是簡單Gitlab伺服器的搭建過程。 伺服器硬體配

簡單GerRit伺服器的搭建Gitlab+GerRit+Jenkins

程式碼稽核作為一個開源的程式碼稽核工具,已經被許多公司投入使用,GerRit自帶一個程式碼儲存倉庫,但是無法做到程式碼庫版本管理。我們一般使用GerRit作為程式碼稽核工具,程式碼版本使用Gitlab或Github。在程式碼提交到GerRit倉庫後,我們使用Replicati

[原創]CI持續集成系統環境--Gitlab+Gerrit+Jenkins完整對接https://www.cnblogs.com/kevingrace/p/5651447.html

https nor ont ott AD www. 原創 back pad [原創]CI持續集成系統環境--Gitlab+Gerrit+Jenkins完整對接https://www.cnblogs.com/kevingrace/p/5651447.

基於CentOS7搭建mongodb3.6.6版本

inux ruby listen b數 單表 ocs seve more 高性能 基於CentOS7搭建mongodb(3.6.6版本) mongodb簡介 Mongodb,分布式文檔存儲數據庫,由C++語言編寫,旨在為WEB應用提供可擴展的高性能數據存儲解決方案。Mon

CI持續整合系統環境--Gitlab+Gerrit+Jenkins完整對接

原文地址https://www.cnblogs.com/kevingrace/p/5651447.html 近年來,由於開源專案、社群的活躍熱度大增,進而引來持續整合(CI)系統的誕生,也越發的聽到更多的人在說協同開發、敏捷開發、迭代開發、持續整合和單元測試這些拉風的術語。然而,大都是僅僅聽到在說而已,國內

Centos7.4 搭建Harbordocker映象倉庫

1、安裝docker,docker-compose yum -y install docker yum -y install docker-compose 2、配置go語言環境,網上百度一下很多的

CentOS7搭建FTP虛擬使用者

採用的是虛擬使用者和firewall防火牆yum install -y vsftpd ftp firewall-cmd --permanent --zone=public --add-port=20/tcp firewall-cmd --permanent --zone=public --add-port=2

eclipse(luna)搭建SSHstruts2+spring4+hibernate4

eclipse enc cti ppi 文件中 releases path ram cat 準備工作: java環境 tomcat環境(這裏使用tomcat8.0) eclipse(已添加了server runtime插件) ========================

使用Jekins自動構建項目GitLab+Java Maven

str view rdquo add scrip hist jekins 分享圖片 周期 1. 登錄Jekins 前提: Jekins 已經部署完成並能登錄版本:Jenkins2.60.2 http://IP:端口/jenkins/view/all/newJob 2.

Hadoop集群搭建Linux虛擬機

AI .org vmware 完成 devices 所有 版本選擇 windows 點擊 搭建Linux虛擬機 VMware虛擬機安裝Linux 系統 1、首先打開網易開源鏡像站: http://mirrors.163.com/ 當然,大家也可以使用阿裏開源鏡像站

Java項目框架搭建系列Java學習路線

Java 編程語言 前言:已經工作4年,真是時間飛逝。其實當你在一間公司工作一兩年之後,公司用到的開發框架的基本使用你應該都會了。你會根據一個現有項目A復制一下搭建出另外一個類似框架的項目B,然後在項目B上進行業務邏輯開發。如果你更努力一點,你可能有去摸索一些配置的作用,一些問題的排查會更有經驗和自己

LNMP環境搭建記錄源碼方式

des all 英文 pass ml2 內核 evel 語言 highlight   LNMP指的是在Linux下用PHP+MYSQL+NGIX搭建網站的環境。   Nginx是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代

Linux詳細搭建httpd網站搭建服務阿帕奇

日誌 enc 9.1 RKE virtual 新版本 util open 依賴 1.httpd簡介當今主流的開源Web服務器軟件有httpd(Apache),lighttpd,nginx,thttpd等,而httpd是迄今為止使用量多的web服務器,據統計目前httpd的全

快速搭建WebAPIOdata+Code-First附Odata條件查詢表~

Odata是什麼? 開放資料協議(Open Data Protocol,縮寫OData)是一種描述如何建立和訪問Restful服務的OASIS標準。該標準由微軟發起,前三個版本1.0、2.0、3.0都是微軟開放標準,遵循 [1]  。第四個版本4.0於2014年3月17日在OA

Windows下Python環境搭建圖解3.6.2

Windows下Python(pip)環境搭建(3.6)圖解 ----------------小白一隻,學習整理中。。。。轉載請標明,謝謝------------------------------------------- 1:下載最新的Python安裝:3.6.2

搭建ERP獲取部門列表

1、所有的資料採用JSON+AJAX的方式實現提交和返回,在struts.xml檔案中不採用result進行頁面跳轉 <package name="default" namespace="/" extends="struts-default">     &nbs

centos7純淨版最小安裝部署docker-ce詳情

一、安裝完純淨版centos7之後檢視本機ip #ip addr 至於為什麼用ip addr 而不用ifconfig請看我之前的文章有所介紹 https://mp.csdn.net/mdeditor/82736254 二、因為個人習慣原因,我使用xshell登入本

JAVA小白啟蒙篇:第一個SSM框架搭建示例附原始碼下載

  HelloWorld工程原始碼下載 http://download.csdn.net/detail/baidu_31657889/9682606 有償連結(0.1rmb):https://www.fageka.com/Home/Index/sell?id=ctLT7f4

我的android studio開發環境搭建歷程一部辛酸史

android開發愈演愈烈,本人也想湊下熱鬧,首先要過的就是要搭建android開發環境這一關,本來我用eclipse挺順的,不過參考網上的評論,感覺還是親兒子好,所以決定切換到android studio,簡稱as。 其實網上有很多文章可以參考,但是自己動起手來才發現沒有那麼簡單啊,簡直是虐心啊。。。 第