1

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:\Jenkins\workspace\my_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?

jing
  • 1,919
  • 2
  • 20
  • 39

1 Answers1

1

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.

jing
  • 1,919
  • 2
  • 20
  • 39