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

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

相关推荐
江湖人称小鱼哥10 小时前
jenkins在windows配置sshpass
运维·jenkins
koboides10 小时前
我的第一个开源项目-jenkins集成k8s项目
linux·运维·云原生·容器·kubernetes·jenkins
苦逼IT运维2 天前
Jenkins + SonarQube 从原理到实战三:SonarQube 打通 Windows AD(LDAP)认证与踩坑记录
运维·服务器·windows·docker·云计算·jenkins·devops
朱小弟cs62 天前
Orange的运维学习日记--41.Ansible基础入门
linux·运维·学习·ci/cd·自动化·ansible·devops
Hello.Reader2 天前
Elasticsearch Node.js 客户端连接指南(Connecting)
elasticsearch·node.js·jenkins
阑梦清川3 天前
派聪明RAG知识库----关于elasticsearch报错,重置密码的解决方案
大数据·elasticsearch·jenkins
无名咸鱼3 天前
Git与CI/CD相关知识点总结
git·ci/cd
hl04063 天前
GitLab CI + Docker 自动构建前端项目并部署 — 完整流程文档
ci/cd·docker·gitlab
先天打工圣体的男人3 天前
Linux环境gitlab多种部署方式及具体使用
运维·git·gitlab
孙克旭_3 天前
day073-Jenkins消息通知与pipline流水线
linux·运维·jenkins