如何在Jenkins中使用HTTP构建器

时间:2017-10-13 14:10:32

标签: jenkins groovy jenkins-pipeline

我正在尝试使用HTTP Builder在管道脚本中发出POST请求(确实在共享库中通过命令行工作)但需要它在Jenkins中工作

在Jenkins中运行时出现以下错误。

No suitable ClassLoader found for grab

我的脚本如下所示

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')

import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.Method.POST
import groovyx.net.http.HTTPBuilder

def gitUpdateStatus() {
  String targetUrl = 'https://api.github.com/repos/myOrg/'
  def http = new HTTPBuilder(targetUrl)
  http.request(POST) {
  uri.path = "myRepo/statuses/commit_id_here"
  requestContentType = JSON
  body = [state: 'failure', description: 'Jenkins Unit Tests', target_url: 'http://test.co.uk', context: 'unit tests']
  headers.'Authorization' = "token 123"
  headers.'User-Agent' = 'Jenkins Status Update'
  headers.Accept = 'application/json'

  response.success = { resp, json ->
    println "GitHub updated successfully! ${resp.status}"
  }

  response.failure = { resp, json ->
    println "GitHub update Failure! ${resp.status} " + json.message
  }
}

node {

  stage('Echo Client JS')
    git branch: 'master', credentialsId: '${JENKINS_CREDENTIALS_ID}', url: 'git@github.com:myOrg/myRepo.git'
    gitUpdateStatus()

}

我看过许多帖子出现了同样的问题,但我似乎无法弄清楚这是如何解决的,有人可以协助吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您不能直接在管道中使用@Grab。您需要将gitUpdateStatus()函数移至Jenkins shared library。请参阅Using third party libraries