Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

jenkins pipeline - How to get windows curl response code in Jenkinsfile

In Jenkinsfile I am trying to find out if file exists in Artifactory. The Jenkins agent runs on windows. So I combined 2 stackoverflow solutions How to get response code from curl in a jenkinsfile and How do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)? into following code:

try {
  env.CHECK_URL = bat(returnStdout: true, script: "curl -s -o /dev/null -w "%%{http_code}" "https://myartifactory.com/myfile.exe"")
} catch (Exception e) {
  echo 'Exception occurred: ' + e.toString()
}
echo "URL Response: ${env.CHECK_URL}"

I hoped for env.CHECK_URL to contain 200 (found) or 404 (not found). But instead the http code 200 is written into output, not into variable. And curl returns error 23 (Failed writing body).

The output of above function:

C:Jenkinsworkspacemy_jenkins>curl -s -o /dev/null -w "%{http_code}" "https://myartifactory.com/myfile.exe" 
200
[Pipeline] echo
Exception occurred: hudson.AbortException: script returned exit code 23
[Pipeline] echo
URL Response: null

Any advice how to fix it? Or an alternative way for Jenkinsfile to find ouf if file exists in Artifactory?

question from:https://stackoverflow.com/questions/65939960/how-to-get-windows-curl-response-code-in-jenkinsfile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Although I didn't succeeded with curl, I was able to at least find an alternative solution using Artifactory JFrog CLI library:

stdout = bat(returnStdout: true, script: "jfrog rt s --url https:/myartifactory.com/ --apikey "${ART_PSW}" --user ${ART_USR} --count myteam/myfile.exe")
def deployed_count = stdout.readLines().last().trim()

... this returns 0 or 1 depending on if file exists or not.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...