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@lanxiang.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": "lanxiang",
        "email": "lanxiang@lanxiang.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@lanxiang.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
      }
   }
]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM