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哪里的勾选是否已去掉

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

相关推荐
weixin_3077791313 小时前
Linux下Jenkins数据故障的系统化排查Shell脚本
linux·运维·服务器·jenkins
糖果罐子♡13 小时前
TortoiseGit 首次上传本地工程至已初始化的GitLab仓库教程
gitlab
Byron Loong15 小时前
【Git】如何检查 Ubuntu 系统上 gitLab 是否开启
git·ubuntu·gitlab
QN1幻化引擎1 天前
SFA 信号场注意力:用8KB参数换248x KV Cache压缩,边缘设备也能跑长序列
人工智能·算法·gitee·gitlab
阿里云云原生1 天前
研发福音:云效 AI 智能评审正式支持 GitLab 集成,VPC 内网即可实现自动审阅
gitlab
heimeiyingwang2 天前
【架构实战】CI/CD流水线:从手动部署到一键上线
ci/cd·架构
海兰4 天前
【Elasticsearch】工作流自动化评估
elasticsearch·自动化·jenkins
tianyuanwo4 天前
深入掌握 java -jar 命令:从基础到 Jenkins Agent 实战
java·jenkins·jar
小小测试开发4 天前
Promptfoo 源码级解析:LLM 评估框架的核心设计与 CI/CD 集成实践
人工智能·ci/cd
csdn_aspnet4 天前
GitHub Actions自动化运维实战,用CI/CD流水线实现测试、部署、安全扫描一体化
运维·安全·ci/cd·自动化·github