1. 程式人生 > >gitlab + jenkins + salt持續整合

gitlab + jenkins + salt持續整合

目的:
主要是瞭解持續整合方面的知識,分析gitlab的對於svn的優勢,瞭解gitlab和jenkins的原理,再配合指令碼和salt做一個簡易的持續整合。

環境
我使用三臺機器做測試:
三臺機器系統都是centos7,selinux disabled,iptables disabled
192.168.46.66 jenkins  salt-master
192.168.46.82 gitlab 
192.168.46.105 nginx    salt-minion
該測試大致過程
通過提交程式碼到gitlab觸發jenkins構建,jenkins呼叫指令碼打包程式,通過salt分發包,並通過salt部署包

安裝


1.1 安裝jenkins
官網安裝文件:https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins#InstallingJenkins-Unix%2FLinuxInstallation

我裝的穩定版本:
1.1.1 新增Jenkins的源(repository):
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
yum install jenkins

1.1.2 安裝/檢查JDK
[
[email protected]
jenkins]# yum install java -y [[email protected] jenkins]# java -version openjdk version "1.8.0_71" OpenJDK Runtime Environment (build 1.8.0_71-b15) OpenJDK 64-Bit Server VM (build 25.71-b15, mixed mode) 1.1.3 安裝Jenkins: [[email protected] jenkins]#yum install jenkins 遇到錯誤 Delta RPMs disabled because /usr/bin/applydeltarpm not installed. jenkins-1.653-1.1.noarch.rpm FAILED http://pkg.jenkins-ci.org/redhat-stable/jenkins-1.653-1.1.noarch.rpm: [Errno 14] HTTP Error 404 - Not Found ] 0.0 B/s | 0 B --:--:-- ETA Trying other mirror. [
[email protected]
~]# yum install -y deltarpm 繼續安裝jenkins [[email protected] jenkins]#yum install jenkins 如果有防火牆 firewall-cmd --zone=public --add-port=8080/tcp --permanent firewall-cmd --zone=public --add-service=http --permanent firewall-cmd --reload firewall-cmd --list-all /usr/lib/jenkins/:jenkins安裝目錄,WAR包會放在這裡。 /etc/sysconfig/jenkins:jenkins配置檔案,“埠”,“JENKINS_HOME”等都可以在這裡配置 /var/lib/jenkins/:預設的JENKINS_HOME。 /var/log/jenkins/jenkins.log:Jenkins日誌檔案。 啟動Jenkins [[email protected] jenkins]# /sbin/chkconfig jenkins on [[email protected] jenkins]# systemctl start jenkins 1.1.4 安裝salt-master 1.1.4.1 匯入key rpm --import https://repo.saltstack.com/yum/redhat/7/x86_64/latest/SALTSTACK-GPG-KEY.pub 1.1.4.2 編輯源/etc/yum.repos.d/saltstack.repo [saltstack-repo] name=SaltStack repo for RHEL/CentOS $releasever baseurl=https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest enabled=1 gpgcheck=1 gpgkey=https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest/SALTSTACK-GPG-KEY.pub 1.1.4.3 安裝master yum install salt-master -y 1.1.4.4 啟動 systemctl enable salt-master systemctl start salt-master
訪問 192.168.46.66:8080 
jenkins.png
1.2 gitlab安裝
官網安裝文件:https://www.elastic.co/guide/index.html

1.2.1 安裝相關軟體
yum install curl policycoreutils openssh-server openssh-clients
systemctl enable sshd
systemctl start sshd
yum install postfix
systemctl enable postfix
systemctl start postfix
firewall-cmd --permanent --add-service=http
systemctl reload firewalld

1.2.2 下載安裝gitlab包
curl -LJO https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-8.5.5-ce.0.el7.x86_64.rpm/download
rpm -i  gitlab-ce-8.5.5-ce.0.el7.x86_64.rpm

1.2.3 配置啟動gitlab
gitlab-ctl reconfigure
gitlab-ctl --help 檢視幫助  
[[email protected] ~]# gitlab-ctl service-list
gitlab-workhorse*
logrotate*
nginx*
postgresql*
redis*
sidekiq*
unicorn*

1.2.4 登陸 192.168.46.82
Username: root 
Password: 5iveL!fe
第一次登陸會讓你修改密碼 
gitlab.png
1.3 nginx安裝
1.3.1 安裝nginx
    yum install -y nginx
    啟動nginx
    nginx
1.3.2 安裝salt-minion
  1.3.2.1 匯入key
    rpm --import https://repo.saltstack.com/yum/redhat/7/x86_64/latest/SALTSTACK-GPG-KEY.pub
  1.3.2.2 編輯源/etc/yum.repos.d/saltstack.repo
    [saltstack-repo]
    name=SaltStack repo for RHEL/CentOS $releasever
    baseurl=https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest
    enabled=1
    gpgcheck=1
    gpgkey=https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest/SALTSTACK-GPG-KEY.pub
  1.3.2.3 安裝master
    yum install salt-master -y
  1.3.2.4 啟動
    systemctl enable salt-minion
    systemctl start salt-minion

配置


2.1 配置46.105 nginx
2.1.1 配置nginx 
    我的程式碼會發布到/usr/share/nginx/html/demo目錄中
    [[email protected] ~]# vim /etc/nginx/nginx.conf
     server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html/demo;
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    location / {
    }
    error_page 404 /404.html;
        location = /40x.html {
    }
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
    }
重啟nginx
nginx -s reload
2.1.2 配置salt-minion
    [[email protected] ~]# vim /etc/salt/minion
    master: 192.168.46.66
    id: nginx
    重啟minion
    systemctl restart salt-minion 

    在46.66 salt-master 加入nginx
    salt-key -a nginx
2.2 配置46.82 gitlab​
2.2.1 建立一個demo專案
gitlab2.png
2.2.2 在46.66上建立ssh key  
把公鑰拷貝過來,私鑰會拷到jenkins上,就可以無密碼通訊了
gitlab3.png
2.2.3 配置gitlab的web hooks
當有push時會觸發jenkins構建
url為: http://192.168.46.66:8080/git/[email protected]:root/demo.git
/git/notifyCommit?url=前為jenkins的地址和埠
/git/notifyCommit?url=後為git專案地址
2.2.4 配置salt master
vim /etc/salt/master
file_roots:
  base:
    - /srv/salt
重啟salt-master
systemctl restart salt-master
gitlab4.png
2.2 配置46.66 jenkins
2.2.1 配置jenkins 
系統設定

jenkins2.png
jenkins3.png
建立專案
jenkins5.png
新增私鑰
jenkins6.png
jenkins7.png
jenkins8.png
指令碼放在/project
[[email protected] project]# ls
code  config  deploy.sh  package
code 程式碼目錄
config 配置檔案目錄
package 程式碼打包後存放目錄

deploy.sh  jenkins構建時呼叫指令碼
[[email protected] project]# cat deploy.sh 
#!/usr/bin/bash
###########[size=16]#[/size]
#author:CM
#mail:[email protected]
#########[size=16]#[/size]
[size=16]提前建立相應的目錄[/size]
LOCK_FILE="/project/lock"
CODE_PATH="/project/code"
DEMO_PATH="/project/code/demo"
PACKAGE_PATH="/srv/salt/package"
SALT_PACKAGE_PATH="package"
CONFIG_PATH="/project/config"
LOG_FILE="/project/deploy.log"
DEST_PROJECT="/project"
CTIME=$(date "+%Y-%m-%d-%H-%M")


Usage(){
  echo "Usage: $0 ( deploy | list | rollback edition)"
}


Lock_judge(){
  if [ -f $LOCK_FILE ];then
    echo "warnning !!! someone else must deploy new version or rollback old version! please wait."
    exit 2
  else
    touch -f $LOCK_FILE
  fi    
}

Delete_lock_file(){
  if [ -f $LOCK_FILE ];then
    rm -f  $LOCK_FILE
  fi
}

Deploy_code(){
  Lock_judge
  tag=$?
  echo $tag
  if [ $tag == 0 ];then
    cd $DEMO_PATH
    git pull
    Commit=$(git log|head -n 1|cut -d ' ' -f 2)
    Com_ID=$(echo ${Commit:0:6})
    cp -r $DEMO_PATH ${PACKAGE_PATH}/demo_${Com_ID}_${CTIME}
    cp -r ${CONFIG_PATH}/* ${PACKAGE_PATH}/demo_${Com_ID}_${CTIME}/
    cd $PACKAGE_PATH
    tar -zcf demo_${Com_ID}_${CTIME}.tar.gz demo_${Com_ID}_${CTIME}
    salt "nginx" cp.get_file  salt://${SALT_PACKAGE_PATH}/demo_${Com_ID}_${CTIME}.tar.gz ${DEST_PROJECT}/demo_${Com_ID}_${CTIME}.tar.gz
    salt "nginx" cmd.run "cd ${DEST_PROJECT} && tar -zxf demo_${Com_ID}_${CTIME}.tar.gz && rm -f /usr/share/nginx/html/demo && ln -s ${DEST_PROJECT}/demo_${Com_ID}_${CTIME} /usr/share/nginx/html/demo && nginx -s reload"
  fi
  Delete_lock_file
}

Edition_list(){
  salt 'nginx' cmd.run "ls -l ${DEST_PROJECT}|grep ^d"
}

Rollback_code(){
  Lock_judge
  salt 'nginx' cmd.run "rm -f /usr/share/nginx/html/demo && ln -s ${DEST_PROJECT}/$1 /usr/share/nginx/html/demo"
  Delete_lock_file
}

main(){
  case $1 in
    deploy)
      Deploy_code
      ;;
    list)
      Edition_list
      ;;
    rollback)
      Rollback_code $2
      ;;
    *)
      echo $1
      Usage
      ;;
  esac
}

main $1 $2

#指令碼沒有完善,沒有寫日誌

測試


我之前已經推上去程式碼
jenkins9.png
nginx1.png
vim index.html
<h1>Adam Jensen</h1>
  <h2>今晚上吃</h2>
  <h2>西紅柿雞</h2>
  <h2>西紅牛腩</h2>
git add index.html 
git commit -m "牛腩"
git push origin master
jenkins10.png
nginx2.png
回滾
[[email protected] project]# ls
code  config  deploy.sh  package
[[email protected] project]# sh deploy.sh list
nginx:
    drwxr-xr-x 7 root root     129 Mar 23 09:28 demo_205d03_2016-03-23-09-28
    drwxr-xr-x 7 root root     129 Mar 23 17:04 demo_3c2a63_2016-03-23-17-04
    drwxr-xr-x 7 root root     129 Mar 22 09:21 demo_43fabe_2016-03-22-09-21
    drwxr-xr-x 7 root root     151 Mar 21 17:41 demo_4c5be1_2016-03-21-17-41
    drwxr-xr-x 7 root root     151 Mar 21 17:48 demo_4c5be1_2016-03-21-17-48
    drwxr-xr-x 7 root root     119 Mar 21 14:46 demo_574cdd_2016-03-21-14-46
    drwxr-xr-x 7 root root     119 Mar 21 14:48 demo_574cdd_2016-03-21-14-48
    drwxr-xr-x 7 root root     119 Mar 21 14:49 demo_574cdd_2016-03-21-14-49
    drwxr-xr-x 7 root root     119 Mar 21 14:56 demo_574cdd_2016-03-21-14-56
    drwxr-xr-x 7 root root     129 Mar 21 15:59 demo_8c7d37_2016-03-21-15-59
    drwxr-xr-x 7 root root     129 Mar 23 17:22 demo_8cc9b4_2016-03-23-17-22
    drwxr-xr-x 7 root root     129 Mar 21 15:41 demo_a2333d_2016-03-21-15-41
    drwxr-xr-x 7 root root     129 Mar 23 17:03 demo_d5f4f3_2016-03-23-17-03

[[email protected] project]# sh deploy.sh rollback  demo_3c2a63_2016-03-23-17-04

nginx3.jpg

轉至:http://www.openskill.cn/article/306