jenkins pipeline构建后返回构建结果给gitlab
jenkins pipeline构建后返回构建结果给gitlab
使用场景
gitlab 合并请求时要求管道任务必须成功,否则无法执行合并操作,又不想使用gitlab ci 工具。
实现方法
1.Generic Webhook Trigger 插件实现gitlab合并请求触发Jenkins任务
2.pipeline中获取触发中post请求的参数
3.调用gitlab Api 返回任务执行结果。
合并请求post参数
{
"object_kind": "merge_request",
"event_type": "merge_request",
"user": {
"id": 31,
"name": "wangdabao",
"username": "wangdabao",
"avatar_url": "https://www.gravatar.com/avatar/67949034c065ebaf503fff96c56583a7?s=80\u0026d=identicon",
"email": "wangdabao@naura.local"
},
"project": {
"id": 23, //使用此参数指定接收状态结果的项目
"name": "XTXtest",
"description": null,
"web_url": "http://192.168.1.101/wangdabao/XTXtest",
"avatar_url": null,
"git_ssh_url": "git@192.168.1.101:wangdabao/XTXtest.git",
"git_http_url": "http://192.168.1.101/wangdabao/XTXtest.git",
"namespace": "wangdabao",
"visibility_level": 0,
"path_with_namespace": "wangdabao/XTXtest",
"default_branch": "master",
"ci_config_path": null,
"homepage": "http://192.168.1.101/wangdabao/XTXtest",
"url": "git@192.168.1.101:wangdabao/XTXtest.git",
"ssh_url": "git@192.168.1.101:wangdabao/XTXtest.git",
"http_url": "http://192.168.1.101/wangdabao/XTXtest.git"
},
"object_attributes": {
"assignee_id": 31,
"author_id": 31,
"created_at": "2021-07-08 17:54:17 +0800",
"description": "",
"head_pipeline_id": null,
"id": 55,
"iid": 2,
"last_edited_at": null,
"last_edited_by_id": null,
"merge_commit_sha": null,
"merge_error": null,
"merge_params": {},
"merge_status": "preparing",
"merge_user_id": null,
"merge_when_pipeline_succeeds": false,
"milestone_id": null,
"source_branch": "master",
"source_project_id": 23,
"state_id": 1,
"target_branch": "DevOps_Test_Release",
"target_project_id": 23,
"time_estimate": 0,
"title": "Master",
"updated_at": "2021-07-08 17:54:17 +0800",
"updated_by_id": null,
"url": "http://192.168.1.101/wangdabao/XTXtest/-/merge_requests/2",
"source": {
"id": 23,
"name": "XTXtest",
"description": null,
"web_url": "http://192.168.1.101/wangdabao/XTXtest",
"avatar_url": null,
"git_ssh_url": "git@192.168.1.101:wangdabao/XTXtest.git",
"git_http_url": "http://192.168.1.101/wangdabao/XTXtest.git",
"namespace": "wangdabao",
"visibility_level": 0,
"path_with_namespace": "wangdabao/XTXtest",
"default_branch": "master",
"ci_config_path": null,
"homepage": "http://192.168.1.101/wangdabao/XTXtest",
"url": "git@192.168.1.101:wangdabao/XTXtest.git",
"ssh_url": "git@192.168.1.101:wangdabao/XTXtest.git",
"http_url": "http://192.168.1.101/wangdabao/XTXtest.git"
},
"target": {
"id": 23,
"name": "XTXtest",
"description": null,
"web_url": "http://192.168.1.101/wangdabao/XTXtest",
"avatar_url": null,
"git_ssh_url": "git@192.168.1.101:wangdabao/XTXtest.git",
"git_http_url": "http://192.168.1.101/wangdabao/XTXtest.git",
"namespace": "wangdabao",
"visibility_level": 0,
"path_with_namespace": "wangdabao/XTXtest",
"default_branch": "master",
"ci_config_path": null,
"homepage": "http://192.168.1.101/wangdabao/XTXtest",
"url": "git@192.168.1.101:wangdabao/XTXtest.git",
"ssh_url": "git@192.168.1.101:wangdabao/XTXtest.git",
"http_url": "http://192.168.1.101/wangdabao/XTXtest.git"
},
"last_commit": {
"id": "79da59751bc7f205d7e85ffeb0379a5c0723e652",
//使用此ID 指定接收结果合并请求的commit
"message": "x=1 wrong\n",
"title": "x=1 wrong",
"timestamp": "2021-07-08T17:24:44+08:00",
"url": "http://192.168.1.101/wangdabao/XTXtest/-/commit/79da59751bc7f205d7e85ffeb0379a5c0723e652",
"author": {
"name": "liangxiaoyi",
"email": "liangxiaoyi@naura.local"
}
},
"work_in_progress": false,
"total_time_spent": 0,
"human_total_time_spent": null,
"human_time_estimate": null,
"assignee_ids": [
31
],
"state": "opened",
"action": "open"
},
"labels": [],
"changes": {
"merge_status": {
"previous": "unchecked",
"current": "preparing"
}
},
"repository": {
"name": "XTXtest",
"url": "git@192.168.1.101:wangdabao/XTXtest.git",
"description": null,
"homepage": "http://192.168.1.101/wangdabao/XTXtest"
},
"assignees": [
{
"id": 31,
"name": "wangdabao",
"username": "wangdabao",
"avatar_url": "https://www.gravatar.com/avatar/67949034c065ebaf503fff96c56583a7?s=80\u0026d=identicon",
"email": "wangdabao@naura.local"
}
]
}
pipeline 样例
pipeline {
agent {
label "win7_x64" //节点标签
}
triggers{
GenericTrigger(
genericVariables:[
[key:'event_name',value:'$.event_type'], //触发动作 pubat or tag_pubat
[key:'user_email',value:'$.user.email'], //GitLab公共邮箱需要自行配置否则获取不到
[key:'project_name',value:'$.project.name'], //项目名称 DevOps_Test
[key:'project_id',value:'$.project.id'], //项目ID
[key:'git_url',value:'$.project.git_http_url'], //git_url http://192.168.1.19/devops/DevOps_Test.git
[key:'group_name',value:'$.project.namespace'], //GITLAB_GROUP
[key:'commits_id',value:'$.object_attributes.last_commit.id'],//触发请求中的last_commit.id [key:'source_branch',value:'$.object_attributes.source_branch']
],
token:"qazwsx",
//causeString:'Triggered on $ref',
printContributedVariables:true,
printPostContent:true
)
}
stages {
stage('Build') {
steps {
bat "echo hahhaha"
}
post {
always {
bat ""
echo '构建结束...'
}
success {
echo '恭喜您,构建成功!!!'
//成功返回GITLAB信息,拼接项目ID 和 last_commit.id,LDmVp1RATzDYt9nxxx gitlab令牌
bat "curl --request POST --header \"PRIVATE-TOKEN: LDmVp1RATzDYt9nxxx\" \"${gitlab_url}/api/v4/projects/${project_id}/statuses/${commits_id}?state=success\""
}
failure {
echo '抱歉,构建失败!!!'
//失败返回GITLAB信息,拼接项目ID 和 last_commit.id
bat "curl --request POST --header \"PRIVATE-TOKEN: LDmVp1RATzDYt9nTxxx\" \"${gitlab_url}/api/v4/projects/${project_id}/statuses/${commits_id}?state=failed\""
}
unstable {
echo '该任务已经被标记为不稳定任务....'
}
changed {
echo ''
}
}
}
gitlab相关api
将构建状态发布到提交
添加或更新提交的构建状态。
POST /projects/:id/statuses/:sha
属性 | 类型 | 必需的 | 描述 |
---|---|---|---|
id | integer/string | yes | 已验证用户拥有的项目的 ID 或URL 编码路径 |
sha | string | yes | 提交 SHA |
state | string | yes | 状态的状态。可以是以下之一:pending,running,success, failed,canceled |
ref | string | no | ref状态所指的(分支或标签) |
name或者context | string | no | 用于区分此状态与其他系统状态的标签。默认值为default |
target_url | string | no | 与此状态关联的目标 URL |
description | string | no | 状态的简短描述 |
coverage | float | no | 总代码覆盖率 |
pipeline_id | integer | no | 要设置状态的管道 ID。在同一 SHA 上有多个管道的情况下使用 |
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/17/statuses/18f3e63d05582537db6d183d9d557be09e1f90c8?state=success"
Example response
[
{
"id":45,
"iid":1,
"project_id":35,
"title":"Add new file",
"description":"",
"state":"opened",
"created_at":"2018-03-26T17:26:30.916Z",
"updated_at":"2018-03-26T17:26:30.916Z",
"target_branch":"master",
"source_branch":"test-branch",
"upvotes":0,
"downvotes":0,
"author" : {
"web_url" : "https://gitlab.example.com/thedude",
"name" : "Jeff Lebowski",
"avatar_url" : "https://gitlab.example.com/uploads/user/avatar/28/The-Big-Lebowski-400-400.png",
"username" : "thedude",
"state" : "active",
"id" : 28
},
"assignee":null,
"source_project_id":35,
"target_project_id":35,
"labels":[ ],
"work_in_progress":false,
"milestone":null,
"merge_when_pipeline_succeeds":false,
"merge_status":"can_be_merged",
"sha":"af5b13261899fb2c0db30abdd0af8b07cb44fdc5",
"merge_commit_sha":null,
"squash_commit_sha":null,
"user_notes_count":0,
"discussion_locked":null,
"should_remove_source_branch":null,
"force_remove_source_branch":false,
"web_url":"http://https://gitlab.example.com/root/test-project/merge_requests/1",
"time_stats":{
"time_estimate":0,
"total_time_spent":0,
"human_time_estimate":null,
"human_total_time_spent":null
}
}
]
jenkins pipeline构建后返回构建结果给gitlab的更多相关文章
- Kubernetes笔记(三):Gitlab+Jenkins Pipeline+Docker+k8s+Helm自动化部署实践(干货分享!)
通过前面两篇文章,我们已经有了一个"嗷嗷待哺"的K8s集群环境,也对相关的概念与组件有了一个基本了解(前期对概念有个印象即可,因为只有实践了才能对其有深入理解,所谓"纸上 ...
- vue独立构建和运行构建
有两种构建方式,独立构建和运行构建.它们的区别在于前者包含模板编译器而后者不包含. 模板编译器:模板编译器的职责是将模板字符串编译为纯 JavaScript 的渲染函数.如果你想要在组件中使用 tem ...
- jenkins pipeline构建后发送邮件通知
jenkins pipeline构建后发送邮件通知 mail配置 进入系统配置 找到最下边的邮件通知 创建任务 Pipeline片段 post { always { bat "" ...
- 避免代码merge后无法构建发布(GItlabCI + Jenkins)
1.准备工作 目标: 开发人员提交代码后触发GitlabCI ,如果有merge请求则触发Jenkins对源分支在开发环境构建测试. 2.GItlab配置 开启仅允许pipeline成功后才能merg ...
- Jenkins+pipeline+参数构建+人工干预确定
Jenkins+pipeline+参数构建+人工干预 实现了以下功能 1. 可以选择环境,单选:可以选择需要发布的项目,多选 2.发布过程可视化 3. 可以人工干预是否继续发布. 初始化配置需要很久, ...
- jenkins pipeline构建项目
以前用的jenkins自由风格发布代码.界面丑陋,出现问题位置不够清晰.今天改进一下流程使用jenkins pipeline构建项目. 学习使我快乐 步骤一.安装pipeline插件 点击系统管理-& ...
- jmeter+ant+jenkins+mac 构建后自动发送邮件
1.安装Email Extension Plugin插件 2.进入系统管理-系统设置,按如下进行设置: ------------------------------------------------ ...
- jenkins构建后操作添加“Publish to Subversion repository”与Eclipse更新提交SVN文件冲突
jenkins配置环境信息: 1.安装“SVN Publisher plugin”插件: 2.在系统管理-系统设置中“Global SVN Publisher Settings” 填写信息:
- Jenkins构建后发送邮件
我们首先安装Jenkins邮件扩展插件“ Email Extension Plugin ”. Jenkins和插件的安装方法见上一篇文章:http://qicheng0211.blog.51cto.c ...
随机推荐
- 通用PE u盘装Ghost Win7系统教程
通用PE u盘装Ghost Win7系统教程 导读 通用pe工具箱是现在最老牌的的U盘装系统和维护电脑的专用工具之一,一键式制作.操作简单便捷,几乎100%支持所有U盘,不再为装机烦恼们,抓紧时间下载 ...
- wmctrl像xmonad那样方便地用快捷键来控制任务窗口的显示
窗口左右互搏之wmctrl篇 分类: LINUX 2012-10-24 16:34:41 一直有个念头,就是能够像xmonad那样方便地用快捷键来控制任务窗口的显示,今天弄wmctrl,刚好有时间 ...
- Microk8s 安装helm3
Microk8s 安装helm3 原本使用官方的命令时这样的.但是使用的是官方的源,网络极不稳定.就出现了下面的状况.解决方法是更改为国内的源. microk8s enable helm3 curl: ...
- 11.16-18 lsci、ipcs、ipcrm:清除ipc相关信息
lspci:显示所有PCI设备 lspci命令用来显示系统中的所有PCI总线设备或是连接到该总线上的所有设备. lspci命令的参数选项及说明 -v 显示详细信息 -vv 显示更详细的信 ...
- Linux中级之lvs三个模式的图像补充(nat,dr,tun)
负载均衡(Load Balance)集群提供了一种廉价.有效.透明的方法,来扩展网络设备和服务器的负载.带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性. (1)单台计算机无法承受大规 ...
- C++ short/int/long/long long 等数据类型大小
表 1 整型数据类型 数据类型 字节大小 数值范围 short int (短整型) 2 字节 -32 768 〜+32 767 unsigned short int(无符号短整型) 2 字节 0 〜+ ...
- Python数模笔记-StatsModels 统计回归(4)可视化
1.如何认识可视化? 图形总是比数据更加醒目.直观.解决统计回归问题,无论在分析问题的过程中,还是在结果的呈现和发表时,都需要可视化工具的帮助和支持. 需要指出的是,虽然不同绘图工具包的功能.效果会有 ...
- 拖动登录框 HTML+CSS+js
先上效果 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- [leetcode] 33. 搜索旋转排序数组(Java)
33. 搜索旋转排序数组 说实话这题我连题都没有看懂....真是醉了 二分,没意思,直接交了- - https://www.jiuzhang.com/solutions/search-in-rotat ...
- Selenium八种元素定位方法源码阅读
接触过Selenium的都知道元素定位有八种方法,但用不同的方法在执行时有什么区别呢? 元素定位8种方法(Python版),当然还有每一个方法对应的find_elements方法 find_eleme ...