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