I am trying to execute the curl post command from Jenkins declarative pipeline, however, it is throwing a syntax error -- Expecting '}' found ':'
The pipeline script is below:
pipeline {
agent { label ' Linux01'}
stages {
stage('Hello') {
steps {
sh 'curl -u username:password -X POST -d '{"body":"Jenkinspipleinecomment"}' -H "Content-Type:application/json" http://localhost:8080/rest/api/2/issue/someissue/comment'
}
}
}
}
Kindly help.
Try this out
pipeline {
agent { label ' Linux01'}
stages {
stage('Hello') {
steps {
sh """curl -u username:password -X POST -d '{"body":"Jenkinspipleinecomment"}' -H "Content-Type:application/json" http://localhost:8080/rest/api/2/issue/someissue/comment"""
}
}
} }
Related
I call createItem in a function, from a loop derived from the list of git's currentBuild.changeSets
Here is the function:
def boolean shCreateJob(jobName) {
jobName = jobName.toString()
command = "curl -s -XPOST '$JENKINS_URL/createItem?name=$jobName' -u user:secret --data-binary ${TEMPLATE_FILE} -H 'Content-Type:text/xml'"
command = command.toString()
println("In shCreateJob, command=$command")
println("In shCreateJob, command.getClass()=${command.getClass()}")
try {
sh "curl $command"
}
catch (err) {
echo 'Exception during job creation : ' + err
}
return true
}
As you can see, in the function, I'm trying to cast the name and the command to String to ensure the sh command doesn't get anything exotic.
Here is the output:
In shCreateJob, command=curl -s -XPOST 'http://10.128.128.168:8080//createItem?name=cz_AAA' -u user:secret --data-binary #//var/lib/jenkins/jobs/_upload_template/config.xml -H 'Content-Type:text/xml'
[Pipeline] echo
In shCreateJob, command.getClass()=class java.lang.String
[Pipeline] sh
[Pipeline] echo
Exception during job creation : java.io.NotSerializableException: java.util.LinkedHashMap$Entry
And yet, the job ends in success, and also the new job is created as expected!
What's the deal?
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.
I have the following Jenkinsfile
def ARTIFACTS_JOB_ID;
def ARTIFACTS_URL;
node('CentOs') {
checkout scm
stage('Example') {
echo 'Download artifacts'
sh 'curljson --header "PRIVATE-TOKEN: tdzis3" "https://gitlab-sample.com/api/v4/projects/63/jobs" > jobs.json'
ARTIFACTS_JOB_ID = sh(returnStdout: true, script: 'python GetID.py').trim()
ARTIFACTS_URL = "https://gitlab-sample.com/api/v4/projects/63/jobs/${ARTIFACTS_JOB_ID}/artifacts"
echo "======> $ARTIFACTS_URL"
sh 'echo $ARTIFACTS_URL'
sh 'curl --output artifacts.zip --header "PRIVATE-TOKEN: tdzis3" "$ARTIFACTS_URL"'
}
}
while trying to get the artifact, it's calling
'curl --output artifacts.zip --header "PRIVATE-TOKEN: tdzis3" ""' with empty url
Similarly
echo "======> $ARTIFACTS_URL" //works fine
sh 'echo $ARTIFACTS_URL' // shows empty string, tried with ${ARTIFACTS_URL} aswell.
is there a way I can run it as below (so I don't have to use sh)
def ARTIFACTS_JOB_ID;
def ARTIFACTS_URL;
node('CentOs') {
checkout scm
stage('Example') {
script {
echo 'Download artifacts'
curljson --header "PRIVATE-TOKEN: tdzis3" "https://gitlab-sample.com/api/v4/projects/63/jobs" > jobs.json
}
}
}
without sh I am getting invalid syntax error while doing
curljson --header
The variable declaration ARTIFACTS_URL = "https://..." is a Groovy global variable. Hence, it is not naturally available to the sh step as a shell variable.
You need to wrap the commands inside the sh step with double quotes instead of single quotes as sh "echo $ARTIFACTS_URL" for Groovy to interpolate it as a Groovy variable.
Have you tried
sh "curl --output artifacts.zip --header 'PRIVATE-TOKEN: tdzis3' '{$ARTIFACTS_URL}'"
?
for multiple sh commands you could also use
sh """
curl command here
cd command next
etc
"""
I have written a jenkins scripted pipeline which has 3 stages in it. And in each stage i am calling a curl command to start a jenkins job which is on my remote server. But, the problem is 2nd stage is getting executed before 1st stage completes its execution.
Please help me how to resolve this?
node{
properties([
disableConcurrentBuilds()
])
stage('stage1'){
sh 'curl -X POST -H "Content-Type: application/json" -d "{ "tagname": "$tagname" }" -vs http://pkg.rtbrick.com:8080/generic-webhook-trigger/invoke?token=qwerty'
}
stage('stage2'){
sh 'curl -X POST -H "Content-Type: application/json" -d "{ "tagname": "$tagname" }" -vs http://image.rtbrick.com:8080/generic-webhook-trigger/invoke?token=1234'
}
stage('stage3'){
sh 'curl -X POST -H "Content-Type: application/json" -d "{ "tagname": "$tagname" }" -vs http://image.rtbrick.com:8080/generic-webhook-trigger/invoke?token=1804'}
}
}
"Stage2" should start only if "stage1" is completed.
It's doable using the Jenkins REST API and the waitUntil step in combination with timeout (without a timeout it could hang forever):
def response
timeout(30) {
waitUntil {
response = sh(
script: 'curl http://pkg.rtbrick.com:8080/view/job/my-job/lastBuild/api/json | grep "\"result\":\"SUCCESS\""',
returnStatus: true
)
return (response == 0)
}
}
if (response != 0) {
build.result = 'ERROR'
}
I have used both jenkins/jenkins:latest and jenkinsci/blueocean:latest docker images with pipeline script from SCM settings.
General setting "GitHub project" was enabled with https://github.com/alamsarker/test
Now When I build. its shows the following error:
+ Builing...
/var/jenkins_home/workspace/pipeline-test#tmp/durable-2aac8cac/script.sh: line 1: Builing...: not found
Can you please to fix the issue?
I run docker by:
docker run \
-u root \
--rm \
-d \
-p 8080:8080 \
-p 50000:50000 \
-v jenkins-data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
jenkinsci/blueocean
My Jenkinsfile is simple as follows:
pipeline {
agent any
stages {
stage('build') {
steps {
sh 'Builing...'
}
}
stage('Test') {
steps {
sh 'Testing...'
}
}
stage('Deploy') {
steps {
sh 'Deploying...'
}
}
}
}
the pipeline step sh is used to execute linux cmd. Building is not a valid linux cmd, that's why you get the error.
If you want to print out some word you can use step echo which is cross-platform or execute the linux cmd: echo via step sh, like sh 'echo Building...' which only work on linux-like agent.
pipeline {
agent any
stages {
stage('build') {
steps {
echo 'Builing...'
}
}
stage('Test') {
steps {
sh 'echo Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}