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
2
Answers
Have you tried
?
for multiple sh commands you could also use
The variable declaration
ARTIFACTS_URL = "https://..."
is a Groovy global variable. Hence, it is not naturally available to thesh
step as a shell variable.You need to wrap the commands inside the
sh
step with double quotes instead of single quotes assh "echo $ARTIFACTS_URL"
for Groovy to interpolate it as a Groovy variable.