gitlab通知触发自动构建:

配置要点:

一、前提:gitlab和jenkins同在本地网络时,需要以管理员权限设置“外发请求”,启用“允许webhook和服务对本地网络的请求”;

  1. 授予jenkins访问gitlab上仓库中特定用户的代码权限;

  2. jenkins基于SSH协议获取代码仓库中的内容,因而事先需要配置jenkins能基于ssh密钥接入gitlab;

二、Gitlab API访问授权

  1. 配置Gitlab API访问认证(Access token)及操作权限;

  2. 配置Jenkins启用/project端点认证,配置他能通过配置的Access Token接入Gitlab API;

三、Jenkins pipeline 项目的通知授权

  1. 创建pipeline任务,选择使用的"GitLab Connect",并选定使用“Build when a change is pushed to GitLab.” 触发器;

  2. 而后为构建触发器生成Secret token;

  3. 配置Gitlab Repository 能通过webhook,基于该Secret token触发构建

具体配置文档见:https://docs.gitlab.com/ce/integration/jenkins.html

操作:

一、外发请求

基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_desktop

 

 生成ssh密钥,将私钥放置在jenkins上,将公钥放在gitlab上,这样jenkins就可以通过私钥访问gitlab了。

[root@master01 ~]# cd .ssh/
[root@master01 .ssh]# ls
id_rsa  id_rsa.pub  known_hosts
[root@master01 .ssh]# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC6ZX2JiJhsOh1SH76DXXhyTuIjR3XJ8UF3tVk6/kUHfVyKIvNUr……
[root@master01 .ssh]# ssh-keygen

将公钥保存在gitlab的ssh密钥上,preferences下:

基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_触发器_02

 

 然后要把私钥信息创建为jenkins上的credential---全局----添加凭据-----ssh Username with private key;私钥与公钥是一对,在~/.ssh/id_rsa文件

基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_spring_03

 

二、在Gitlab上创建Access Token,可以让Jenkins向Gitlab传递自己构建结果的信息;

 基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_desktop_04

 

 基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_spring_05

 

 然后授权Jenkins通过Access token能够连接Gitlab,所以首先需要安装gitlab插件,一旦安装成功,在jenkins上Configure System就会多一个gitlab的配置项

基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_git_06

 

 三、配置jenkins的项目可经由Gitlab上的事件触发,即构建触发器,让项目和仓库建立关联关系;

基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_spring_07

 

该触发器设置一个token,不能谁都可以完成触发器构建工作:构建触发器-----高级

 基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_git_08

 

 配置gitlab的webhooks,能够通过该secret token触发构建,这个配置是项目内的配置,需要在项目下-------设置-----webhooks

基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_推送_09

 

 如果出现了这个报错:“Urlis blocked: Requests to the local network are not allowed”  请检查第一步“允许webhook和服务对本地网络的请求”是否保存好。

测试,保存完webhooks后下方就有测试项目:

基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_desktop_10

 

 测试成功后,就可以修改pipeline的版本号,和项目里pom.xml和spring-boot-helloworld\src\main\java\com\neo\controller\下HelloWorldController.java的版本号

然后推送代码:

 1 huan@DESKTOP-07DR243 MINGW64 ~/Desktop/git/spring-boot-helloworld/gitlab-self/spring-boot-helloworld (master)
 2 $ git add *
 3 
 4 huan@DESKTOP-07DR243 MINGW64 ~/Desktop/git/spring-boot-helloworld/gitlab-self/spring-boot-helloworld (master)
 5 $ git remote -v
 6 origin  http://192.168.3.100:30080/root/spring-boot-helloworld.git (fetch)
 7 origin  http://192.168.3.100:30080/root/spring-boot-helloworld.git (push)
 8 
 9 huan@DESKTOP-07DR243 MINGW64 ~/Desktop/git/spring-boot-helloworld/gitlab-self/spring-boot-helloworld (master)
10 $ git commit -m "update to 0.9.2"
11 [master 8e44f1f] update to 0.9.2
12  2 files changed, 2 insertions(+), 2 deletions(-)
13 
14 huan@DESKTOP-07DR243 MINGW64 ~/Desktop/git/spring-boot-helloworld/gitlab-self/spring-boot-helloworld (master)
15 $ git push origin master
16 Enumerating objects: 19, done.
17 Counting objects: 100% (19/19), done.
18 Delta compression using up to 8 threads
19 Compressing objects: 100% (8/8), done.
20 Writing objects: 100% (10/10), 736 bytes | 368.00 KiB/s, done.
21 Total 10 (delta 3), reused 0 (delta 0)
22 To http://192.168.3.100:30080/root/spring-boot-helloworld.git
23    d92b711..8e44f1f  master -> master
24 
25 huan@DESKTOP-07DR243 MINGW64 ~/Desktop/git/spring-boot-helloworld/gitlab-self/spring-boot-helloworld (master)
26 $

就发现可以自动推送了,现在最后一步,要把jenkinsfile文件也使用SCM仓库推送:

基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_desktop_11

 

 基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_spring_12

 

 改好后,我们修改下3个文件的版本标签,推送试下:

基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_推送_13

 

 基于kubernetes和jenkins的gitops实战-jenkins与gitlab相关联_desktop_14