干貨分享建議收藏:Jenkins集成GitLab
Jenkins免密拉取GitLab項(xiàng)目
1.在Jenkins上為GitLab創(chuàng)建一個(gè)專有的拉取代碼的賬號(hào) Jenkins需要構(gòu)建哪些項(xiàng)目就在GitLab給予賬號(hào)相應(yīng)權(quán)限 我這里已經(jīng)創(chuàng)建過(guò)Jenkins用戶,下面用它登錄后添加SSH-KEY
2.在Jenkins服務(wù)器上生成ssh-key
[root@jenkins ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:IUQIuu0SAdSbZvENbIjFYTrUrnuqKGBmZtwPj6lvz60 root@jenkins
The key's randomart image is:
+---[RSA 2048]----+
|ooB=+oo |
|ooo*.= |
|oo. * + . |
| +.* . o . |
|+ * S |
|.@ o |
|O o * |
|oo =.o. |
|=o*..E.. |
+----[SHA256]-----+
查看公鑰
[root@jenkins ~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7ckQQf4N93Rjf0ts3Lk4siL6FZmZiBiB+ouwTgw9kKof2NeJTguf0aNfDsgSEet4+bJ53ZKztsFZE/C+sqk7grqeLeYDFBWgdZBz1dmCYT51tRFmZPDEDclOIMc2tz0G50g6DFA1dvIfkzeVbKjBNN0o80FSAwAzrtjMrkyaDcrOmYekiSPM8JYJTTcFLCzIBXz7SXOmKH5vyoAIVpdcwnIhkHNLKfD0MdzGb7Kz/pKMnLubodcxyNir6fqw76qwMe1DE0NtQzpaCrTYhVnAizVqCDek0GMZjTG1vWYNn6a8G7omV3Gde1XjcmjAj6ftVQiVLBRJaIGKI4R/B//nd root@jenkins
將Jenkins的公鑰填入GitLab賬號(hào)中
3.測(cè)試SSH-KEY 到j(luò)enkins服務(wù)器上拉取項(xiàng)目來(lái)測(cè)試ssh-key免密是否生效
[root@jenkins ~]# yum install git -y
[root@jenkins ~]# git clone git@106.14.10.124:dev01/sample.git
正克隆到 'sample'...
The authenticity of host '106.14.10.124 (106.14.10.124)' can't be established.
ECDSA key fingerprint is SHA256:bO22/HlgAAGXi9CXTxDE6wvNCUcTs2OajL9PinZMN/0.
ECDSA key fingerprint is MD5:ec:4f:14:0a:b6:72:cf:6e:da:5b:fa:5b:be:b9:2f:db.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '106.14.10.124' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
接收對(duì)象中: 100% (6/6), done.
如上,ssh-key已生效
配置jenkins自動(dòng)拉取代碼
1.jenkins 服務(wù)器添加證書(shū) 系統(tǒng)配置——》Manage Credentials
系統(tǒng)配置——》Manage Credentials——》Jenkins——》全局憑證——》添加憑證
這樣的話Jenkins服務(wù)器拉取GitLab的代碼就不需要再進(jìn)行認(rèn)證了。
Jenkins安裝GitLab插件
1.安裝插件 插件名稱:
?GitLab
?Gitlab Hook
?Gitlab Authentication
?GitLab Logo
安裝完成后重啟Jenkins
GitLab為Jenkins生成Token
1.我們使用Jenkins用戶登錄GitLab 然后使用Jenkins用戶創(chuàng)建Token
2.復(fù)制創(chuàng)建的Token
3.打開(kāi)Jenkins 系統(tǒng)管理——》系統(tǒng)配置
輸入以下相關(guān)內(nèi)容
選擇憑證,測(cè)試后保存
4.查看憑證 現(xiàn)在有以下兩種方式與GitLab進(jìn)行認(rèn)證
?通過(guò)GitLab上Jenkins用戶的密鑰(GitLab綁定Jenkins用戶的公鑰,Jenkins綁定GitLab上Jenkins用戶的私鑰)、
?通過(guò)GitLab上Jenkins用戶的API Token綁定到Jenkins上的GitLab authentication插件上進(jìn)行連接。
Jenkins構(gòu)建流水線
創(chuàng)建Project 新建任務(wù)——》流水線
2.選擇流水線語(yǔ)法
3.生成流水線腳本
復(fù)制生成的git腳本
4.編寫(xiě)Pipline腳本 我這里腳本如下
node {
stage('拉取代碼'){
git credentialsId: 'b907af22-5a74-4eee-aa5f-a822c764279c', url: 'git@172.19.95.139:dev01/sample.git'
echo "Code Pull"
}
stage('代碼掃描'){
echo "Code Scanning"
}
stage('代碼構(gòu)建'){
echo "Code Build"
}
stage('是否部署'){
input '是否部署'
}
stage('開(kāi)始部署'){
sh '/opt/jenkins/sample/sample_release.sh'
}
}
將以上腳本寫(xiě)流水線中
上面腳本最后執(zhí)行了/opt/jenkins/sample/sample_release.sh腳本,我們到執(zhí)行任務(wù)的Jenkins服務(wù)器上去編寫(xiě)這個(gè)部署腳本。腳本如下:jenkins服務(wù)器將/usr/local/src/sample.zip文件拷貝到了172.19.182.107上
[root@jenkins /]# cat /opt/jenkins/sample/sample_release.sh
#!/usr/bin/env bash
scp /usr/local/src/sample.zip root@172.19.182.107:/usr/local/
這里我們需要先讓Jenkins服務(wù)器與172.19.182.107做免密登錄,將Jenkins的公鑰拷貝到172.19.182.107服務(wù)中的/root/.ssh/authorized_keys文件中
#Jenkins的公鑰(這里為root的公鑰,Jenkins進(jìn)程就需要用root用戶運(yùn)行)
[root@jenkins /]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7ckQQf4N93Rjf0ts3Lk4siL6FZmZiBiB+ouwTgw9kKof2NeJTguf0aNfDsgSEet4+bJ53ZKztsFZE/C+sqk7grqeLeYDFBWgdZBz1dmCYT51tRFmZPDEDclOIMc2tz0G50g6DFA1dvIfkzeVbKjBNN0o80FSAwAzrtjMrkyaDcrOmYekiSPM8JYJTTcFLCzIBXz7SXOmKH5vyoAIVpdcwnIhkHNLKfD0MdzGb7Kz/pKMnLubodcxyNir6fqw76qwMe1DE0NtQzpaCrTYhVnAizVqCDek0GMZjTG1vWYNn6a8G7omV3Gde1XjcmjAj6ftVQiVLBRJaIGKI4R/B//nd root@jenkins
#172.19.182.107服務(wù)器的authorized_keys文件
cat /root/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7ckQQf4N93Rjf0ts3Lk4siL6FZmZiBiB+ouwTgw9kKof2NeJTguf0aNfDsgSEet4+bJ53ZKztsFZE/C+sqk7grqeLeYDFBWgdZBz1dmCYT51tRFmZPDEDclOIMc2tz0G50g6DFA1dvIfkzeVbKjBNN0o80FSAwAzrtjMrkyaDcrOmYekiSPM8JYJTTcFLCzIBXz7SXOmKH5vyoAIVpdcwnIhkHNLKfD0MdzGb7Kz/pKMnLubodcxyNir6fqw76qwMe1DE0NtQzpaCrTYhVnAizVqCDek0GMZjTG1vWYNn6a8G7omV3Gde1XjcmjAj6ftVQiVLBRJaIGKI4R/B//nd root@jenkins
#測(cè)試免密登錄
[root@jenkins /]# ssh root@172.19.182.107
Last login: Tue Jun 30 20:26:12 2020 from 172.19.206.72
Welcome to Alibaba Cloud Elastic Compute Service !
Jenkins執(zhí)行任務(wù)
1.進(jìn)入到任務(wù)中
2.點(diǎn)擊立即構(gòu)建
3.是否部署