gitlab-plugin:Jenkins Pipeline和Gitlab之间的openConnection

时间:2019-02-14 08:43:59

标签: jenkins groovy gitlab jenkins-pipeline

我想用groovy开发Jenkins Pipeline,我有一个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)

    }
}

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

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

1 个答案:

答案 0 :(得分:0)

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

这应该由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密钥,然后再使用它。