首页 > 解决方案 > gitlab-plugin : Jenkins Pipeline 和 Gitlab 之间的 openConnection

问题描述

我想在 groovy 中开发一个 Jenkins 流水线,我有一个 Stage 有角色来更新 Gitlab 中的提交状态:.updateGitlabCommitStatus(name: 'someName', state: 'someState')。我的代码在 Groovy 类中外包:

package ...

import com.dabsquared.gitlabjenkins.*

class GitlabSender {
    def steps
    openConnection() {

        steps.properties([steps.gitLabConnection("application_name"])

    }
    updateGitlabCommitStatus(String name, String state) {

        steps.updateGitlabCommitStatus(name: name, state: state)

    }
}

我想通过在调用之前添加 if-else 条件或 aa bloc try catch 来检查我的连接是否updateGitlabCommitStatus打开

if (gitLabConnection(" ") {
   steps.properties([connectionObject])
   call updateGitlabCommitStatus
} else {
   do nothin
}

标签: jenkinsgroovygitlabjenkins-pipeline

解决方案


此提交状态很可能是提交构建状态。

这应该通过 Gitlab API 函数Post the build status to a commit 来完成

curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/17/statuses/18f3e63d05582537db6d183d9d557be09e1f90c8?state=success"

示例响应:

{
   "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
   },
   "name" : "default",
   "sha" : "18f3e63d05582537db6d183d9d557be09e1f90c8",
   "status" : "success",
   "coverage": 100.0,
   "description" : null,
   "id" : 93,
   "target_url" : null,
   "ref" : null,
   "started_at" : null,
   "created_at" : "2016-01-19T09:05:50.355Z",
   "allow_failure" : false,
   "finished_at" : "2016-01-19T09:05:50.365Z"
}

这个解决方案需要你获取 gitlab api 密钥然后使用它。


推荐阅读