How to use CURL command in groovy script - jenkins

I am using below curl command:
curl -u "nnarayan1" https://ipsddress:port/api/v3/orgs/RB-Health-rbESB/repos --insecure
How can I get output of above curl command using groovy scripting?
Kindly suggest groovy script for this.
I tried different solution from stackoverflow, none of them is working.

def res = sh(script: 'curl --insecure -u nnarayan1 https://ipaddress:port/api/v3/orgs/RB-Health-rbESB/repos', returnStdout: true)

def response
response = sh(
script: 'curl --insecure -u nnarayan1 https://ipsddress:port/api/v3/orgs/RB-Health-rbESB/repos',
returnStdout: true
)
I would personally avoid specifying the "--insecure" flag.

Related

Simple and double quotes in cURL in groovy

I have a Jenkins pipeline where this command works and send me a notification through google chat :
script {
sh 'curl -k "https://chat.googleapis.com/v1/spaces/AAAABHT3HT0/messages?key=*****&token=******" -d "#chat_notification.json" -XPOST -H "Content-Type: application/json; charset=UTF-8"'
}
But if I enter the url in a variable, that does not work any more :
script {
url = "https://chat.googleapis.com/v1/spaces/AAAAfF9CGEQ/messages?key=******&token=******"
sh 'curl -k ${url} -d "#chat_notification.json" -XPOST -H "Content-Type: application/json; charset=UTF-8"'
}
With the error :
curl -k -d #chat_notification.json -XPOST -H 'Content-Type: application/json; charset=UTF-8'
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
It's probably a quote issue ?
Yes, if you are using single quotes in Groovy, that means you cannot interpolate the string with variables using the $ syntax.
See https://groovy-lang.org/syntax.html#_string_interpolation .
Same thing applies to shell code, by the way, or sh in this case.
But since you are not using any shell variables in your code, simply swapping the quotes, i.e. quoting your Groovy string with ", and using ' within the curl command would probably work.
Yes it is a quote issue try to use "" instead of '' to use the value of url variable in curl command. You should also seperate -X and POST (you have -XPOST in your script)
script {
url = "https://chat.googleapis.com/v1/spaces/AAAAfF9CGEQ/messages?key=******&token=******"
sh "curl -k ${url} -d #chat_notification.json -X POST -H Content-Type: application/json; charset=UTF-8"
}

How to hide sensitive data from pipeline for sh commands

I have Jenkinsfile that creates mt object and passes vaultToken to psl library:
Jenkinsfile:
#Library('shared-library#psl')
Maintenance mt = new Maintenance()
mt.setVaultToken(config.vaultToken)
mt.setApiUrl(config.apiUrl)
pslService.createMaintenance(mt)
pslService:
String createMaintenance(Maintenance mt){
dockerBuildHelper.getDockerImage(dockerBuildHelper.getWdBuildDockerImageName()).inside('-u root'){
String cmd = "curl -X POST '${mt.getApiUrl()}'" +
" -H 'Content-Type: application/json'" +
" -H 'Authorization: Api-Token ${mt.getVaultToken()}'" +
" -d ${mt.getPayload()} | jq -r '.id'"
return sh(script: cmd, returnStdout: true).trim()
}
}
But this prints curl command and exposes vault token in the pipeline.
Does anyone know how I can hide the sensitive info and/or entire curl command?
I do not want to store this in credentials store, unless I have no choice.
I heard I can use set +x. But I am not sure how to use it and if it helps. Any thoughts?
Try to use mask password plugin. Or create secrets in jenkins and call them in environment block of pipeline.
I went with set +x
String cmd = """set +x
curl -X POST '${mt.getApiUrl()}' -H 'Content-Type: application/json' -H 'Authorization: Api-Token ${mt.getVaultToken()}' -d ${mt.getPayload()} | jq -r '.id'
"""
return sh(script: cmd, returnStdout: true).trim()

can i make jenkins pipeline not exit when curl request returns invalid status code

My Jenkins pipeline is as follows
pipeline{
agent any
stages{
stage{
script{
res1=sh(returnStdout:true, script: 'curl -o /dev/null -sf -w "%{http_code}" http://xyz1.jsp').replace(' ','').toInteger()
echo "${res1}"
res2=sh(returnStdout:true, script: 'curl -o /dev/null -s -w "%{http_code}" http://xyz2.jsp').replace(' ','').toInteger()
echo "${res2}"
}
}
}
}
When 1st curl request returns a status code of 000, script is returning exit code 7 and build is failing. But I want the 2nd curl request also to run and then fail?

Jenkins: Run a curl command within groovy scrpt

I have a requirement where I need to send the status of the jenkins slave to influxdb. To do so I need to run a curl command from Jenkins Groovy script.
My script looks like this :
int value=0;
for (Node node in Jenkins.instance.nodes) {
if (!node.toComputer().online){
value=1;
}
else{
value=0;
}
curl -i -XPOST http://localhost:8086/write?db=jenkins_db&u=user&p=pass --data-binary 'mymeas,tag=$node.nodeName status=$value'
But after running the script values do not appear in influxdb.
Any Idea what might be wrong here?
PS I also tried
def response = [ 'bash', '-c', "curl", "-i", "-XPOST", "http:/localhost:8086/write?db=jenkins_db&u=user&p=pass", "--data-binary", "\'mymeas tag=$node.nodeName status=$value"\' ].execute().text
You just need to echo your curl command
echo curl -i -XPOST http://localhost:8086/write?db=jenkins_db&u=user&p=pass --data-binary 'mymeas,tag=$node.nodeName status=$value'

Trigger jenkins job with parameters from shell

I am trying to trigger a trigger jenkins job from shell but as of now no sucess.I tried both these methods
curl -u ceadmin:ceadmin -X POST http://abc-lnx:8080/job/ci_demo/build --data token=ci_build -data-urlencode json='{"parameter": [{"name":"Branch", "value":"master"}, {"name":"verbosity", "value":"high"}]}'
curl -X POST http://abc-lnx:8080/job/ci_demo/buildWithParameters?token=ci_build&Branch=master
I have defined ce_admin as token in my job. also ce_admin is a admin user in Jenkins.Anonymous user do not have any permission other then read on jobs and views.
What am i missing?
This works for me:
#parameters:
username=user
password=password
jenkins_url=example.com/build
some_token=hi
job_name=dostuff
curl -u $username:$password -X POST https://$jenkins_url/job/$job_name/build\?token\=$some_token
So this should work for your example (I escaped "?", and "&"):
curl -u ceadmin:ceadmin -X POST http://abc-lnx:8080/job/ci_demo/buildWithParameters\?token=ci_build\&Branch=master

Resources