日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/7/14 编程问答 92 豆豆
生活随笔 收集整理的這篇文章主要介紹了 gitlab与jenkins的自动化部署(通过webhook与ansible) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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


1、部署介紹

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端口


?

2、gitlab安裝?

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安裝包,并且安裝
方法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)第三步,啟用配置,并且啟動 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.

其實只要下載到gitlab ce包(ce是社區版,不用驗證的),直接安裝就行了,啟動命令為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) 注冊用戶
先開啟用戶注冊按鈕,用戶注冊完畢以后,關閉用戶注冊


?

4、配置jenkins拉取gitlab項目的賬號的sshkey

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

?


?

5、配置jenkins項目

? 1、登錄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來連接兩者的有關系。


?


6、gitlab設置webhook

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

當然,這里也可以設置triger事件:

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





登錄jenkins界面:192.168.1.49:8180

然后大功告成!


?

轉載于:https://www.cnblogs.com/to-be-rich/p/7655838.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。