使用场景
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
}
}
]