gitlab與jenkins的自動化部署(經過webhook與ansible)

gitlab與jenkins的自動化部署(經過webhook與ansilble)java


一、部署介紹nginx

gitlab服務器:192.168.1.49:80
jenkins服務器:192.168.1.49:8180
ps:因爲兩個服務運行在同一臺服務器上面,所以,爲了保證jenkins服務器能拉取到 gitlab的代碼,把root的用戶的公鑰分別上傳到gitlab admin用戶跟jenkins admin用戶當中去。jenkins我沒有使用默認的端口8080,主要是我公司的gerrit架構會用到這個8080端口,因此爲了防止衝突,改成8180端口git


 

二、gitlab安裝 web

a) 第一步:安裝相應的在依賴包(postfix或者sendmail),其中選擇internet site這種網絡smtp郵件服務器這種方式來發送郵件,其餘一些依賴包:curl \openssh-server\ ca-certificates\ postfix

If you install Postfix to send email please select 'Internet Site' during setup. Instead of using Postfix you can also use Sendmail or configure a custom SMTP server . If you wish to use Exim, please configure it as an SMTP server.
On Centos 6 and 7, the commands below will also open HTTP and SSH access in the system firewall.

操做命令:
sudo apt-get install curl openssh-server ca-certificates postfix


b) 第二步:Add the GitLab package server and install the package. 下載gitlab安裝包,而且安裝
方法一、增長yum源連接,使用sduo apt-get install gitlab-ee
Apt-get安裝Curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh|sudo bash
操做命令:
sudo apt-get install gitlab-ce

If you are not comfortable installing the repository through a piped script, you can find the entire script here .
Alternatively you can select and download the package manually and install using
方法二、deb包安裝下載deb包,而後安裝

下載: sudo wget https://packages.gitlab.com/gitlab/gitlab-ee
安裝: sudo dpkg -i gitlab-ce_8.1.2-ee.0_amd64.deb

建議通常下載後再裝

c)第三步,啓用配置,而且啓動 GitLab
操做命令:
sudo gitlab-ctl reconfigure

d) 第四步:打開網頁,進行註冊,使用默認密碼
http://192.168.0.177:80
Username: rootPassword: 5iveL!fe

e) 第五步:2.1.6. Upload your license(EE版本須要認證,CE) 這步我操做的時候是沒有的
Go toAdmin > Licenseand upload your.gitlab-licensefile.
Don't have a license yet? Get a subscription.shell

其實只要下載到gitlab ce包(ce是社區版,不用驗證的),直接安裝就好了,啓動命令爲gitlab-ctl reconfigure安全


 

三、jenkins安裝 bash

a) Install Jenkins
wget -q -O - https://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

b) Starting Jenkins服務器


nohup java -Dorg.jenkinsci.plugins.gitclient.Git.timeOut=60 -Dfile.encoding=UTF-8 -Dhudson.DNSMultiCast.disabled=true -jar /usr/share/jenkins/jenkins.war --httpPort=8180 > /var/log/jenkins_log 2>&1 &

(注意--httpPort=8180放在後面)

c) 開啓 jenkins頁面爲:192.168.0.179:8180
http://xxxxx:8180/

d) 插件安裝
Git Plugin
SSH Plugin
URL Trigger Plugin
Gerrit Trigger Plugin
Gitlab Hook
Gitlab Merge Request Builder
GitLab Plugin

e) 註冊用戶
先開啓用戶註冊按鈕,用戶註冊完畢之後,關閉用戶註冊網絡


 

四、配置jenkins拉取gitlab項目的帳號的sshkey架構

因爲兩個服務運行在同一臺服務器上面,所以,爲了保證jenkins服務器能拉取到 gitlab的代碼,把root的用戶的公鑰分別上傳到gitlab admin用戶跟jenkins admin用戶當中去。步驟我就不寫了!

 


 

五、配置jenkins項目

  一、登陸192.168.1.49:8180
點擊新建:

 



依次填寫相關信息:(注意,項目安全這塊是在平常管理中才會用到,若是簡單測試能夠忽略)



源碼管理:

 


觸發器:

我這裏選擇得比較多,有push觸發,也有merge觸發

構建:(經過shell來構建)


我這裏是經過簡單的ansible來控制web服務器的壓縮包scp,tar解壓,還有指定軟聯接
#變量WORKSPACE、GIT_URL是jenkins默認內置的變量

#!/bin/bash 

#變量WORKSPACE,GIT_URL是jenkins默認提供的變量
cd $WORKSPACE

#截取giturl中的產品
product=`echo $GIT_URL| awk -F"/" '{print $NF}'|sed 's/\.git//g'`

#截取代碼當前的前七位sha值
sha=`cat .git/HEAD|cut -b 1-7`

#指定日期
d=`date +"%Y%m%d"`

#指定壓縮包的路徑與包全稱,包名(不帶後綴)
p_pathname=/tmp/"$product"_"$d"_"$sha".tar.gz
p_name="$product"_"$d"_"$sha".tar.gz
p_productname="$product"_"$d"_"$sha"

#本地壓縮
tar -cf $p_pathname  $WORKSPACE/*

#指定web服務器的包的存放位置
p_store=/var/prod/"$product"/"$d"

#指定web服務器的包解壓位置
p_target=$p_store/$p_productname

#指定nginx網頁代碼位置
www=/mnt/www/launcher.szprize.cn

#dev表明web服務器,確保包解壓路徑存在
ansible dev  -m shell -a "mkdir -p $p_target "
#dev表明web服務器,ansible copy模塊遠程複製
ansible dev -m copy -a "src=$p_pathname dest=$p_store/"
#ansible 遠程解壓
ansible dev -m shell -a "tar -xf $p_store/$p_name -C $p_target"
#ansible 軟聯接
ansible dev -m file -a "src=$p_target dest=$www state=link"#!/bin/bash 

jenkins這邊就配置完了

可是注意:jenkins跟gitlab怎麼通訊觸發呢?

你們請留意觸發器那塊有一句話:
Build when a change is pushed to GitLab. GitLab CI Service URL: http://192.168.1.49:8180/project/launcher
它的意思的,gitlab ci接口能夠經過http://192.168.1.49:8180/project/launcher來鏈接二者的有關係。


 


六、gitlab設置webhook

在對應的gitlab項目設置當中,設置webhooks

固然,這裏也能夠設置triger事件:

保存後會在底部,生成webhook,點擊test,看jenkins界面是否觸發了!





登陸jenkins界面:192.168.1.49:8180

而後大功告成!

相關文章
相關標籤/搜索