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

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

相关推荐
宋均浩3 天前
# GitHub Actions 实战:从零搭建 CI/CD 流水线的 5 个核心配置
ci/cd
醉颜凉5 天前
Elasticsearch高性能优化:Bulk API大规模数据导入性能调优全攻略
elasticsearch·性能优化·jenkins
霸道流氓气质5 天前
GitLab CI/CD 完全指南
linux·ci/cd·gitlab
sbjdhjd5 天前
从零搭建企业级 CI/CD(下):Jenkins+GitLab+Harbor 全链路实战指南
git·servlet·ci/cd·云原生·云计算·gitlab·jenkins
小美元5 天前
【爽之】使用jenkins实现前端一键发版
运维·jenkins
糖果店的幽灵5 天前
软件测试接口测试从入门到精通:接口测试CI_CD集成
软件测试·ci/cd·接口测试
changxiang6 天前
Jenkins备忘
运维·jenkins
用什么都重名6 天前
Git 合并两个无共同历史的分支:从报错到解决全记录
git·gitlab
master3366 天前
GitLab (Docker) 常用命令及解决方案清单
docker·容器·gitlab
qq_356408666 天前
GitLab 单机私有化部署文档(基于 Docker 环境)
docker·gitlab