darwin
(Alternates)
May 12, 2020, 8:48am
1
Hi all,
anyone ever to call api within jenkins pipeline (groovy script) ?
i try to call api with terminal with below statement and it’s work fined:
cov=(curl -k -u {password} ‘{url}/api/measures/componentmetricKeys=coverage&component= {keys}’);
but on groovy script the statement was error,
anyone have experience on calling api with more than 1 parameter in groovy script?
Hi,
i’ve created a shared library for our jenkins pipelines, works without problems for multiple parameters
// vars/sqRest.groovy
import groovy.json.*
@NonCPS
def call(url, method, apitoken) {
println "sqRest => " + url
def jsonSlurper = new JsonSlurperClassic()
raw = apitoken + ':'
bauth = 'Basic ' + javax.xml.bind.DatatypeConverter.printBase64Binary(raw.getBytes())
def conn = new URL(url).openConnection() as HttpURLConnection
conn.setRequestMethod(method)
conn.setRequestProperty("Authorization", bauth)
conn.connect()
httpstatus = conn.responseCode
println "sqRest ResponseCode = $httpstatus"
def object = jsonSlurper.parseText(conn.content.text)
}
Note the @NonCPS annotation, the use of JsonSlurperClassic and the def scope to prevent
a NotSerializableException
And it’s used like that, e.g. =
[...]
withCredentials([string(credentialsId: sonarcreds, variable: 'sonarapitoken')]) {
sqjson = sqRest("https://${sonarhost}/api/projects/search?projects=${groupId + ':' + artifactId}", 'GET', sonarapitoken)
println sqjson
if(sqjson.components.size() == 0) {
println "create Sonarqube Project ${groupId + ':' + artifactId}"
sqRest("https://${sonarhost}/api/projects/create?project=${groupId + ':' + artifactId}&name=$artifactId", 'POST', sonarapitoken)
}
}
[...]
If problems you may also use slashy strings $/ .... /$
, e.g.
sqRest($/https://${sonarhost}/api/projects/create?project=${groupId + ':' + artifactId}&name=$artifactId/$, 'POST', sonarapitoken)
Gilbert
system
(system)
Closed
May 20, 2020, 2:59am
4
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.