日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

gitlab与jenkins的自动化部署(通过webhook与ansible)

發(fā)布時(shí)間:2025/7/14 98 豆豆
生活随笔 收集整理的這篇文章主要介紹了 gitlab与jenkins的自动化部署(通过webhook与ansible) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

gitlab與jenkins的自動(dòng)化部署(通過webhook與ansilble)


1、部署介紹

gitlab服務(wù)器:192.168.1.49:80
jenkins服務(wù)器:192.168.1.49:8180
ps:由于兩個(gè)服務(wù)運(yùn)行在同一臺(tái)服務(wù)器上面,因此,為了保證jenkins服務(wù)器能拉取到 gitlab的代碼,把root的用戶的公鑰分別上傳到gitlab admin用戶跟jenkins admin用戶當(dāng)中去。jenkins我沒有使用默認(rèn)的端口8080,主要是我公司的gerrit架構(gòu)會(huì)用到這個(gè)8080端口,所以為了防止沖突,改為8180端口


?

2、gitlab安裝?

a) 第一步:安裝相應(yīng)的在依賴包(postfix或者sendmail),其中選擇internet site這種網(wǎng)絡(luò)smtp郵件服務(wù)器這種方式來發(fā)送郵件,其他一些依賴包: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安裝包,并且安裝
方法1、增加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
方法2、deb包安裝下載deb包,然后安裝

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

建議一般下載后再裝

c)第三步,啟用配置,并且啟動(dòng) GitLab
操作命令:
sudo gitlab-ctl reconfigure

d) 第四步:打開網(wǎng)頁,進(jìn)行注冊(cè),使用默認(rèn)密碼
http://192.168.0.177:80
Username: rootPassword: 5iveL!fe

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

其實(shí)只要下載到gitlab ce包(ce是社區(qū)版,不用驗(yàn)證的),直接安裝就行了,啟動(dòng)命令為gitlab-ctl reconfigure


?

3、jenkins安裝?

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) 注冊(cè)用戶
先開啟用戶注冊(cè)按鈕,用戶注冊(cè)完畢以后,關(guān)閉用戶注冊(cè)


?

4、配置jenkins拉取gitlab項(xiàng)目的賬號(hào)的sshkey

由于兩個(gè)服務(wù)運(yùn)行在同一臺(tái)服務(wù)器上面,因此,為了保證jenkins服務(wù)器能拉取到 gitlab的代碼,把root的用戶的公鑰分別上傳到gitlab admin用戶跟jenkins admin用戶當(dāng)中去。步驟我就不寫了!

?


?

5、配置jenkins項(xiàng)目

? 1、登錄192.168.1.49:8180
點(diǎn)擊新建:

?



依次填寫相關(guān)信息:(注意,項(xiàng)目安全這塊是在日常管理中才會(huì)用到,如果簡單測(cè)試可以忽略)



源碼管理:

?


觸發(fā)器:

我這里選擇得比較多,有push觸發(fā),也有merge觸發(fā)

構(gòu)建:(通過shell來構(gòu)建)


我這里是通過簡單的ansible來控制web服務(wù)器的壓縮包scp,tar解壓,還有指定軟聯(lián)接
#變量WORKSPACE、GIT_URL是jenkins默認(rèn)內(nèi)置的變量

#!/bin/bash #變量WORKSPACE,GIT_URL是jenkins默認(rèn)提供的變量 cd $WORKSPACE#截取giturl中的產(chǎn)品 product=`echo $GIT_URL| awk -F"/" '{print $NF}'|sed 's/\.git//g'`#截取代碼當(dāng)前的前七位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服務(wù)器的包的存放位置 p_store=/var/prod/"$product"/"$d"#指定web服務(wù)器的包解壓位置 p_target=$p_store/$p_productname#指定nginx網(wǎng)頁代碼位置 www=/mnt/www/launcher.szprize.cn#dev代表web服務(wù)器,確保包解壓路徑存在 ansible dev -m shell -a "mkdir -p $p_target " #dev代表web服務(wù)器,ansible copy模塊遠(yuǎn)程復(fù)制 ansible dev -m copy -a "src=$p_pathname dest=$p_store/" #ansible 遠(yuǎn)程解壓 ansible dev -m shell -a "tar -xf $p_store/$p_name -C $p_target" #ansible 軟聯(lián)接 ansible dev -m file -a "src=$p_target dest=$www state=link"#!/bin/bash

jenkins這邊就配置完了

但是注意:jenkins跟gitlab怎么通信觸發(fā)呢?

大家請(qǐng)留意觸發(fā)器那塊有一句話:
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來連接兩者的有關(guān)系。


?


6、gitlab設(shè)置webhook

在對(duì)應(yīng)的gitlab項(xiàng)目設(shè)置當(dāng)中,設(shè)置webhooks

當(dāng)然,這里也可以設(shè)置triger事件:

保存后會(huì)在底部,生成webhook,點(diǎn)擊test,看jenkins界面是否觸發(fā)了!





登錄jenkins界面:192.168.1.49:8180

然后大功告成!


?

轉(zhuǎn)載于:https://www.cnblogs.com/to-be-rich/p/7655838.html

總結(jié)

以上是生活随笔為你收集整理的gitlab与jenkins的自动化部署(通过webhook与ansible)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。