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'
Related
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"
}
I'm writing a Jenkins Pipeline using a sh command with curl.
Using a regular command prompt it is possible to store the response cookies and after that sending those cookies in another request, for instance to store the cookies
curl -k -c my_cookies.txt -X POST https://myLogin
and then send a request with those cookies
curl -k -b my_cookies.txt -X GET https://myGetRequest
Is it possible with Jenkins Pipeline to have something similar ?
I have tried something like:
final String url = "https://myLogin"
final String response = sh(script: "curl -c my_cookie.txt -k -X POST $url ", returnStdout: true).trim()
But this generates an error like
curl: (35) Encountered end of file
Any help is appreciated.
It can be done like this :
pipeline {
agent any;
stages {
stage('call 1st api') {
steps {
sh """
curl --cookie-jar $WORKSPACE/cookie.txt https://62e96ddc0c77.ngrok.io/put
"""
}
}
stage('call 2nd api') {
steps {
sh """
curl --cookie $WORKSPACE/cookie.txt https://62e96ddc0c77.ngrok.io/get
"""
}
}
}
}
In the above example if the first call is a success the cookie will store on $WORKSPACE/cookie.txt location and the 2nd call will read it from the same location.
$WORKSPACE - is the location of the Job workspace. you no need to hardcode the location, as Jenkins takes care it
I am trying to execute the curl command from Jenkins declarative pipeline on a remote server, however it is running on Jenkins node instead of server.
pipeline {
agent {
label
}
stages {
stage('TEst ssh') {
steps {
script {
sh '''
ssh -t user#test << ENDSSH
echo "ssh to server"
cd /opt/apps
url=$(curl -H 'X-JFrog-Art-Api: Artifactory_token' 'Artifactory_url' |jq -r '.uri')
echo $url
ENDSSH
'''
}
}
}
}
}
I am getting "Curl command not found". can anyone suggest a solution for the same?
Just put the full path to curl in the command, /bin/curl? Or change the cmd to set and check tbe path, then figure out why /bin is not in the path or if curl is even there.
Note: remote ssh login is not the same login sequence as a remote shell.
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?
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.