简体   繁体   English

从Jenkins Groovy脚本访问GitLab JSON

[英]Accessing GitLab JSON from Jenkins Groovy script

In the project I'm working on, we use GitLab CE along with Jenkins for CI. 在我正在进行的项目中,我们将GitLab CE和Jenkins用于CI。

Currently we merge changes using Jenkins job by just passing branch name as an input parameter. 当前,我们仅通过将分支名称作为输入参数来使用Jenkins作业合并更改。 I want to create a job which takes merge request ID (eg 123) and uses GitLab URL API to get info such as eg source branch, target branch etc. and then does the rest (build, merge). 我想创建一个需要合并请求ID(例如123)并使用Gi​​tLab URL API来获取信息(例如源分支,目标分支等)的作业,然后进行其余的工作(构建,合并)。

I'm a total beginner (both to Jenkins and Groovy) and what I did is just: 我是一个初学者(包括Jenkins和Groovy),我所做的只是:

// Removing whitespace from input parameter
def mergeRequest = MergeRequestID
mergeRequest = mergeRequest.replaceAll("\\s","")

// Obtaining JSON with merge request information, 13 is an ID of the project
def apiURL = new URL("https://example.com/api/v4/projects/13/merge_requests/" + mergeRequest)
List json = new JsonSlurper().parse(apiUrl.newReader())

// Here: check for upvotes/downvotes, discussions or anything else. 
assert json.upvotes >= 2  : "Request should have at least 2 upvotes"
assert json.downvotes == 0 : "Request cannot have any downvotes"
assert json.target_branch == "master": "Target branch is not set to master"

def map = ["sourceBranchName ": json.source_branch, "targetBranchName ": json.target_branch]
return map

I end up with FileNotFoundException and later on, I figured out I get 404 response when trying to access this URL from that job, it works in my browser though. 我最终遇到FileNotFoundException,后来发现,尝试从该作业访问此URL时,我得到404响应,尽管它在我的浏览器中有效。 I assume it might be authorization issue for Jenkins machine. 我认为这可能是Jenkins机器的授权问题。

I know that we've got some gitlab-plugin in Jenkins and the communication is possible (in the past we managed to trigger builds using webhooks or post build statuses back on GitLab). 我知道我们在Jenkins中有一些gitlab插件,并且可以进行通信(过去,我们设法使用webhooks触发构建或将构建状态发布回GitLab)。 Can I use any other way to get this JSON in groovy script (executed at the beginning or a job)? 我可以使用其他任何方式以groovy脚本(在开始或工作中执行)获取此JSON吗?

It turned out that it was an authentication issue. 原来,这是一个身份验证问题。
I solved this by providing GitLab API private token in header of a request: 我通过在请求的标头中提供GitLab API专用令牌来解决此问题:

...
import com.cloudbees.plugins.credentials.CredentialsProvider
import com.dabsquared.gitlabjenkins.connection.GitLabApiTokenImpl

def branch = Branch
branch = branch.replaceAll("\\s","")

// Get GitLab API token(s) already stored in Jenkins credentials
def gitlabToken = CredentialsProvider.lookupCredentials(GitLabApiTokenImpl.class).apiToken[0]
def mergeRequestURL = new URL("https://example.com/api/v4/projects/1/merge_requests/" + branch)

// Put the token in request properties before sending it
def contentJson = new JsonSlurper().parse(
    mergeRequestURL.newReader(
        requestProperties: ['Private-Token': gitlabToken.toString()]))

def map = ["Branch": contentJson.source_branch]
return map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM