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的更多相关文章

  1. Kubernetes笔记(三):Gitlab+Jenkins Pipeline+Docker+k8s+Helm自动化部署实践(干货分享!)

    通过前面两篇文章,我们已经有了一个"嗷嗷待哺"的K8s集群环境,也对相关的概念与组件有了一个基本了解(前期对概念有个印象即可,因为只有实践了才能对其有深入理解,所谓"纸上 ...

  2. vue独立构建和运行构建

    有两种构建方式,独立构建和运行构建.它们的区别在于前者包含模板编译器而后者不包含. 模板编译器:模板编译器的职责是将模板字符串编译为纯 JavaScript 的渲染函数.如果你想要在组件中使用 tem ...

  3. jenkins pipeline构建后发送邮件通知

    jenkins pipeline构建后发送邮件通知 mail配置 进入系统配置 找到最下边的邮件通知 创建任务 Pipeline片段 post { always { bat "" ...

  4. 避免代码merge后无法构建发布(GItlabCI + Jenkins)

    1.准备工作 目标: 开发人员提交代码后触发GitlabCI ,如果有merge请求则触发Jenkins对源分支在开发环境构建测试. 2.GItlab配置 开启仅允许pipeline成功后才能merg ...

  5. Jenkins+pipeline+参数构建+人工干预确定

    Jenkins+pipeline+参数构建+人工干预 实现了以下功能 1. 可以选择环境,单选:可以选择需要发布的项目,多选 2.发布过程可视化 3. 可以人工干预是否继续发布. 初始化配置需要很久, ...

  6. jenkins pipeline构建项目

    以前用的jenkins自由风格发布代码.界面丑陋,出现问题位置不够清晰.今天改进一下流程使用jenkins pipeline构建项目. 学习使我快乐 步骤一.安装pipeline插件 点击系统管理-& ...

  7. jmeter+ant+jenkins+mac 构建后自动发送邮件

    1.安装Email Extension Plugin插件 2.进入系统管理-系统设置,按如下进行设置: ------------------------------------------------ ...

  8. jenkins构建后操作添加“Publish to Subversion repository”与Eclipse更新提交SVN文件冲突

    jenkins配置环境信息: 1.安装“SVN Publisher plugin”插件: 2.在系统管理-系统设置中“Global SVN Publisher Settings” 填写信息:

  9. Jenkins构建后发送邮件

    我们首先安装Jenkins邮件扩展插件“ Email Extension Plugin ”. Jenkins和插件的安装方法见上一篇文章:http://qicheng0211.blog.51cto.c ...

随机推荐

  1. 单臂路由实现不同vlan间通信

    单臂路由实现不同vlan间通信 拓扑图 PC配置 PC1 :192.168.1.1 vlan10 192.168.1.254 PC2 :192.168.2.1 vlan20 192.168.2.254 ...

  2. C语言进阶日志二

    1.一个功能模块最好使用一个文件夹包含 2.基本就需要几个文件,比如定时器取反  LED: bsp_led.c 和 bsp_led.h : 包含你需要的led外设GPIO的初始化代码 led_it.c ...

  3. ifconfig显示的网卡信息和我的配置文件名不符

    比如我的配置文件, cd /etc/sysconfig/network-scripts/ifcfg-Auto_eth0是这个名称,但是我使用ifconfig显示的信息却是 eth6   Link en ...

  4. Docker的镜像理解以及容器的备份、恢复和迁移操作

    Docker的镜像理解以及容器的备份.恢复和迁移操作 这篇文章主要介绍了Docker的镜像理解以及容器的备份.恢复和迁移操作,覆盖很多原理知识以及容器的重要操作步骤,极力推荐!需要的朋友可以参考下 D ...

  5. linux服务器环境安全防范教程

    一.目录权限设置很重要:可以有效防范黑客上传木马文件. 如果通过 chmod 644 * -R 的话,php文件就没有权限访问了. 如果通过chmod 755 * -R 的话,php文件的权限就高了. ...

  6. .NET Core中插件式开发实现

    前言: 之前在文章- AppDomain实现[插件式]开发 中介绍了在 .NET Framework 中,通过AppDomain实现动态加载和卸载程序集的效果. 但是.NET Core 仅支持单个默认 ...

  7. AI推理与Compiler

    AI推理与Compiler AI芯片编译器能加深对AI的理解, AI芯片编译器不光涉及编译器知识,还涉及AI芯片架构和并行计算如OpenCL/Cuda等.如果从深度学习平台获得IR输入,还需要了解深度 ...

  8. 三维点云去噪无监督学习:ICCV2019论文分析

    三维点云去噪无监督学习:ICCV2019论文分析 Total Denoising: Unsupervised Learning of 3D Point Cloud Cleaning 论文链接: htt ...

  9. Appium_Android自动化测试Genymotion之模拟器联网设置

    目的: 使用Genymotion做Android项目,需要考虑到联网,以下是设置操作 操作步骤: 打开VM VirtualBox,设置->网络-> 启动模拟器,设置  备注: 模拟器的手机 ...

  10. Handler_read_*的总结

    在分析一个SQL的性能好坏时,除了执行计划,另外一个常看的指标是"Handler_read_*"相关变量. Handler_read_key Handler_read_first ...