Gitlab和Jenkins集成 实现CI (二)

配置Gitlab api token

配置 Gitlab

进入gitlab
用户头像 Edit profile 选择同级的Access Tokens Add new token Token name 填 jenkins Expiration date 选择最长一年的有效期 Select scopes 选择API一项即可

点击保存,切记先将tokens复制出来,后续是无法查看的

Dashboard - Manage Jenkins - System - GitLab

jenkins配置全局凭据

Dashboard Manage Jenkins Credentials System Add credentials New credentials Kind 选择 Git API Token Scope 选择Global Jenkins, nodes, items, all child items, etc API token 填写上一步gitlab中复制出来的token Description 填写 jenkins api ID 不要填写 自动生成

jenkins添加GitLab connections

Dashboard Manage Jenkins System GitLab GitLab connections Connection name 填写 jenkins GitLab host URL 填写gitlab的裸url Credentials 选择刚才添加的全局凭据

添加完成后,点击test connection,如果出现success,说明配置成功

jenkins项目中配置gitlab api token

进入项目
Configure General GitLab Connection 选择第3步添加的

配置jenkins免密拉取gitlab代码

生成公私钥对

shell 复制代码
#进入jenkins
docker container exec -it jenkins /bin/bash

#生成密钥对,一路回车确认就行
ssh-keygen -t rsa

cd ~
#获取公钥
cat .ssh/id_rsa.pub

#获取私钥
cat .ssh/id_rsa

配置Gitlab上的公钥

头像 Edit profile 选择同级的 SSH Keys Add new key Key 粘贴刚才的公钥

配置Jenkins上的私钥

注意:配置的是全局凭据
Dashboard Manage Jenkins Credentials New credentials Kind 选择SSH Username with private key Scope 选择Global Jenkins, nodes, items, all child items, etc Username 填写gitlab上配置公钥的用户名 如 root Private Key 填写刚才生成的私钥 ID 不填, Description 填写描述

Jenkins上创建项目

Dashboard New Item 输入项目名称 选择 Freestyle project

配置项目

进入项目
Configure GitLab Connection 选择 jenkins Source Code Management git Repositories 填写gitlab仓库的ssh地址 Credentials 选择刚才添加的密钥对凭据 Branches to build Branch Specifier 根据真实情况填写 比如 main Repository browser 选择gitlab URL 填写gitlab仓库的http地址 Version 填写gitlab的版本

这里填写 Repositories 的时候容易出错: No ED25519 host key is known for

需要检查 【配置Jenkins上的私钥】一章中username,需要填写生成sshkey的用户,比如我这默认的是jenkins

然后进入jenkins容器,运行

docker container exec -it jenkins /bin/bash
git ls-remote ssh://git@ip:port/developer/codename.git
#询问是否保存指纹
填写yes保存

返回到项目配置这里,重新点击,发现问题已经解决

到这里,没有错误的情况下,jenkins已经可以正确拉到gitlab的代码。可以在项目中构建一次,构建完成应该是绿色标的

shell 复制代码
docker container exec -it jenkins /bin/bash
#查看代码已经正确拉取
ls -l /var/jenkins_home/workspace/项目名称/* 

配置触发器

进入jenkins容器

  1. 生成安全随机数备用openssl rand -hex 12
  2. 配置jenkins

Configure Build Triggers Authentication Token 填入上面的安全随机数 Build when a change... 注意这里勾选上,并且后面附的地址后续gitlab上要用到

  1. 配置gitlab
    取消Webhooks访问限制
    进入gitlab

Admin area Settings Network Outbound requests 勾上两个Allow

进入项目
Settings Webhooks Add new webhook Project Hooks Webhooks URL 填写上面 需要注意 的那个url Secret token 填写和上面配置一样的安全随机数 Trigger 根据实际情况勾选 SSL verification 根据实际情况是否启用 Test 点击后面的Push events, 出现successfully就表示成功

1.这里如果test的时候报4xx错误,检查 gitlab中webhook限制访问是否已打开

  1. 参考 Gitlab和Jenkins集成 实现CI (一) 中 基础配置 章节 gitlab哪里的勾选是否已去掉

都看到这里了,点个赞再走吧!^ _ ^

相关推荐
Good_Starry16 小时前
Git介绍--github/gitee/gitlab使用
git·gitee·gitlab·github
henan程序媛1 天前
Jenkins Pipline流水线
运维·pipeline·jenkins
陌殇殇殇1 天前
使用GitLab CI构建持续集成案例
运维·ci/cd·云原生·容器·kubernetes·gitlab
吕玉生1 天前
基于GitLab 的持续集成环境
ci/cd·gitlab
wudinaniya2 天前
使用rsync+jenkins实现服务自动部署全流程
运维·jenkins·自动化部署
henan程序媛2 天前
jenkins项目发布基础
运维·gitlab·ansible·jenkins
小石潭记丶2 天前
elasticsearch设置账号和密码
大数据·elasticsearch·jenkins
识途老码2 天前
Centos7安装Jenkins
运维·servlet·jenkins
奔跑吧邓邓子2 天前
Jenkins从入门到精通,构建高效自动化流程
servlet·自动化·jenkins
江南剑雨3 天前
jenkinsfile实现镜像构建、发布
linux·运维·jenkins