我想在Jenkins SharedLibraries中使用第三方库(HTTPBuilder)。但是发生了错误。第三方库自动缓存在我的Jenkins master上的~/.groovy/grapes/中。
HttpRequest.goorvy
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.GET
import static groovyx.net.http.Method.POST
import static groovyx.net.http.ContentType.JSON
import groovyx.net.http.ContentType
def http = new HTTPBuilder('http://192.168.100.10:9100')
def userExist
@NonCPS
public int searchUser(String userLogin) {
http.request(GET, JSON) { req ->
uri.path = '/sonarqube/api/users/search'
headers.'User-Agent' = 'curl/7.60.0'
headers.'Authorization' = "Basic $userPassBase64"
headers.Accept = 'application/json'
uri.query = [q: userLogin]
response.success = { resp, reader ->
assert resp.statusLine.statusCode == 200
userExist = reader.paging.total
return userExist
}
}
}Jenkins管道
@Library('SharedLibraries') _
import com.test.jenkins.HttpRequest
AddUserSonarProject user = new AddUserSonarProject()
pipeline {
agent { any }
environment {
userLogin = 'cycwll'
}
stages {
stage('Pull Source code') {
steps {
script{
user.searchUser(userLogin)
}
}
}
}在Jenkins控制台输出中
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@25d9b671
**Caused: java.io.NotSerializableException: groovyx.net.http.HTTPBuilder**
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:926)

发布于 2020-05-20 21:08:47
解决方案:将声明http放入方法中。
public int searchUser(String userLogin) { def http = new HTTPBuilder('http://192.168.100.10:9100') ....}
https://stackoverflow.com/questions/61903800
复制相似问题