Jenkins整合GitLab
Jenkins免密拉取GitLab專案
1.在Jenkins上為GitLab建立一個專有的拉取程式碼的賬號
Jenkins需要構建哪些專案就在GitLab給予賬號相應許可權
我這裡已經建立過Jenkins使用者,下面用它登入後新增SSH-KEY
2.在Jenkins伺服器上生成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賬號中
3.測試SSH-KEY
到jenkins伺服器上拉取專案來測試ssh-key免密是否生效
[root@jenkins ~]# yum install git -y [root@jenkins ~]# git clone [email protected]: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 接收物件中: 100% (6/6), done.
如上,ssh-key已生效
配置jenkins自動拉取程式碼
1.jenkins 伺服器新增證書
系統配置——》Manage Credentials
系統配置——》Manage Credentials——》Jenkins——》全域性憑證——》新增憑證
這樣的話Jenkins伺服器拉取GitLab的程式碼就不需要再進行認證了。
Jenkins安裝GitLab外掛
1.安裝外掛
外掛名稱:
- GitLab
- Gitlab Hook
- Gitlab Authentication
- GitLab Logo
安裝完成後重啟Jenkins
GitLab為Jenkins生成Token
1.我們使用Jenkins使用者登入GitLab
然後使用Jenkins使用者建立Token
2.複製建立的Token
3.開啟Jenkins
系統管理——》系統配置
輸入以下相關內容
選擇憑證,測試後儲存
4.檢視憑證
現在有以下兩種方式與GitLab進行認證
- 通過GitLab上Jenkins使用者的金鑰(GitLab繫結Jenkins使用者的公鑰,Jenkins繫結GitLab上Jenkins使用者的私鑰)、
- 通過GitLab上Jenkins使用者的
API Token
繫結到Jenkins上的GitLab authentication
外掛上進行連線。
Jenkins構建流水線
1.建立Project
新建任務——》流水線
2.選擇流水線語法
3.生成流水線指令碼
複製生成的git指令碼
4.編寫Pipline指令碼
我這裡指令碼如下
node {
stage('拉取程式碼'){
git credentialsId: 'b907af22-5a74-4eee-aa5f-a822c764279c', url: '[email protected]:dev01/sample.git'
echo "Code Pull"
}
stage('程式碼掃描'){
echo "Code Scanning"
}
stage('程式碼構建'){
echo "Code Build"
}
stage('是否部署'){
input '是否部署'
}
stage('開始部署'){
sh '/opt/jenkins/sample/sample_release.sh'
}
}
將以上指令碼寫流水線中
上面指令碼最後執行了/opt/jenkins/sample/sample_release.sh
指令碼,我們到執行任務的Jenkins伺服器上去編寫這個部署指令碼。
指令碼如下:jenkins伺服器將/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 [email protected]:/usr/local/
這裡我們需要先讓Jenkins伺服器與172.19.182.107做免密登入,將Jenkins的公鑰拷貝到172.19.182.107
服務中的/root/.ssh/authorized_keys
檔案中
#Jenkins的公鑰(這裡為root的公鑰,Jenkins程序就需要用root使用者執行)
[root@jenkins /]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7ckQQf4N93Rjf0ts3Lk4siL6FZmZiBiB+ouwTgw9kKof2NeJTguf0aNfDsgSEet4+bJ53ZKztsFZE/C+sqk7grqeLeYDFBWgdZBz1dmCYT51tRFmZPDEDclOIMc2tz0G50g6DFA1dvIfkzeVbKjBNN0o80FSAwAzrtjMrkyaDcrOmYekiSPM8JYJTTcFLCzIBXz7SXOmKH5vyoAIVpdcwnIhkHNLKfD0MdzGb7Kz/pKMnLubodcxyNir6fqw76qwMe1DE0NtQzpaCrTYhVnAizVqCDek0GMZjTG1vWYNn6a8G7omV3Gde1XjcmjAj6ftVQiVLBRJaIGKI4R/B//nd root@jenkins
#172.19.182.107伺服器的authorized_keys檔案
cat /root/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7ckQQf4N93Rjf0ts3Lk4siL6FZmZiBiB+ouwTgw9kKof2NeJTguf0aNfDsgSEet4+bJ53ZKztsFZE/C+sqk7grqeLeYDFBWgdZBz1dmCYT51tRFmZPDEDclOIMc2tz0G50g6DFA1dvIfkzeVbKjBNN0o80FSAwAzrtjMrkyaDcrOmYekiSPM8JYJTTcFLCzIBXz7SXOmKH5vyoAIVpdcwnIhkHNLKfD0MdzGb7Kz/pKMnLubodcxyNir6fqw76qwMe1DE0NtQzpaCrTYhVnAizVqCDek0GMZjTG1vWYNn6a8G7omV3Gde1XjcmjAj6ftVQiVLBRJaIGKI4R/B//nd root@jenkins
#測試免密登入
[root@jenkins /]# ssh [email protected]
Last login: Tue Jun 30 20:26:12 2020 from 172.19.206.72
Welcome to Alibaba Cloud Elastic Compute Service !
Jenkins執行任務
1.進入到任務中
2.點選立即構建
3.是否部署
※更多文章和資料|點選後方文字直達 ↓↓↓
100GPython自學資料包
阿里雲K8s實戰手冊
[阿里雲CDN排坑指南]CDN
ECS運維指南
DevOps實踐手冊
Hadoop大資料實戰手冊
Knative雲原生應用開發指南
OSS 運維實戰手冊
雲原生架構白皮書
Zabbix企業級分散式監控系統原始碼文件
Linux&Python自學資料包
10G面試題戳領