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 小时前
DevOps实战(3) - 使用Arbess+GitLab+Hadess实现Java项目自动化部署
gitlab·jenkins·tiklab·arbess·开源cicd工具
戎码江湖7 小时前
使用CI/CD部署项目(前端Nextjs)
ci/cd·自动部署项目·自动部署前端项目·github部署项目·一键部署项目
小薛博客10 小时前
22、Jenkins容器化部署Java应用
java·运维·jenkins
wallflower202013 小时前
从零到全流程 CI/CD:多技术栈项目自动化部署实战与优化指南
ci/cd·github
小薛博客20 小时前
17、DevOps持续集成、持续部署
运维·ci/cd·devops
观测云1 天前
Jenkins 可观测最佳实践
jenkins
一念一花一世界1 天前
DevOps实战(2) - 使用Arbess+GitPuk+Docker实现Java项目自动化部署
jenkins·tiklab·arbess·开源cicd工具·docker构建部署
faimi1 天前
🚀程序员必收藏!最全Git命令手册:解决90%团队协作难题
前端·gitlab
rivercoder1 天前
Gitea:轻量级的自托管Git服务
git·gitlab·gitea
切糕师学AI1 天前
持续集成和持续交付 (CI/CD) 工具——Jenkins
运维·ci/cd·jenkins