Jenkins配合gitlab实现代码提交触发构建
Jenkins安装:https://www.cnblogs.com/hovin/p/13516574.html
gitlab安装:https://www.cnblogs.com/hovin/p/13524323.html
新建用户,系统管理-->管理用户
更改认证方式,系统管理 --> 全局安全配置,默认创建的用户登录后可以做任何操作,取决于默认的认证授权方式。
创建角色,系统管理 --> Manage and Assign Roles
对角色分配权限
将用户分配到角色
(1) 生成授权码
在QQ邮箱,设置 --> 账户 下,生成授权码。
Jenkins --> 系统管理 --> 系统配置 --> Jenkins Location。
(3) 发件配置
Jenkins --> 系统管理 --> 系统配置 --> 邮件通知。
[root@jenkins ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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:/jt9h8iR8iRrmY2fQ/IoOqa+QiltEJ1luEEReY91t30 root@jenkins The key's randomart image is: +---[RSA 2048]----+ | o+Bo | |. *.. . . . | | . + + . . o | |. . . . . . E | | o . S o | |. = . + = | | + . ^ o . | | . o ..O O.o .| | o++.o oo+oo . | +----[SHA256]-----+ [root@jenkins ~]# cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDsoFGwnuq3pqH8LvIXDXgFHZ1nw9kGB3TZGz99W9uvxoIco8wNW95z0dVZTwrB06fDbOmw0B/p4t9FpnyZOMwgBIkl5C5B2X/fS8h5WWPzXelJOJgcRApTN5qnf7fTQqn+Q+anVxKsQdTGZnhG4Wn4CMZA4nm0dwKYKo+qaYLawxx2YRXImMtyq4NNQVRvALBS1yVxjFjfXsnUAal+e7SUqHGGrHaLgOPlehmDK4yOJFNc+Xx3kQGC4wBxrEcjvrdP0w6I2SDbSaSUC3SxvtiS3O5568NUgXdFXQCN9FcpUoe7ujfbt8nhtAsivwS2Vzmq4nA/79IHp6qwyp1cHjzT root@jenkins
(2) 将key添加到gitlab
(3) 测试key拉取代码
[root@jenkins ~]# git clone git@192.168.145.37:test-service/test-project.git Cloning into 'test-project'... The authenticity of host '192.168.145.37 (192.168.145.37)' can't be established. ECDSA key fingerprint is SHA256:NjIthsyJF52ZNkDd6/a8BL0DUk8n7fH9tMLJXNKBmLQ. ECDSA key fingerprint is MD5:03:6e:5d:65:26:32:32:22:3e:68:c2:86:83:4b:da:bf. Are you sure you want to continue connecting (yes/no)? yes #第一次连接需输yes Warning: Permanently added '192.168.145.37' (ECDSA) to the list of known hosts. remote: Enumerating objects: 9, done. remote: Counting objects: 100% (9/9), done. remote: Compressing objects: 100% (6/6), done. remote: Total 9 (delta 0), reused 0 (delta 0) Receiving objects: 100% (9/9), done. [root@jenkins ~]# ll test-project/ total 8 -rw-r--r-- 1 root root 110 Aug 18 10:00 index.html -rw-r--r-- 1 root root 23 Aug 18 10:00 README.md
[root@jenkins ~]# cat ~/.ssh/id_rsa
Jenkins --> 系统管理 --> Manage Credentials --> 全局凭据 --> 添加凭据
(3) jenkins 创建 project
配置 git 项目地址和用户
(4) 测试构建项目
#本地也有项目了 [root@jenkins ~]# ls /var/lib/jenkins/workspace/test-project index.html README.md
创建后会成生一个Tokens值,复制到jenkins。
(2) 登录jenkins,添加api token
Jenkins --> 系统管理 --> Manage Credentials --> 全局凭据 --> 添加凭据。
然后打开系统管理,进入系统设备,配置gitlab。
(3) jenkins新建一个pipline job
(4) 生成拉取代码的 pipline 脚本
点击流水线语法,可以生成拉取gitlab上代码的语句,如下所示:
也可以执行shell命令:
(5) 进入gitlab,进行项目 --> settings --> Integrations
本地测试会遇到以下错误:
解决办法如下:
添加后webhook后,还可以测试连接是否生效:
jenkins也自动触发了构建:
至此,触发器就已配置成功,开发一旦有提交代码,就会自动构建。