【问题标题】:How to get windows curl response code in Jenkinsfile如何在 Jenkinsfile 中获取 windows curl 响应代码
【发布时间】:2021-01-28 15:14:53
【问题描述】:

在 Jenkinsfile 中,我试图找出 Artifactory 中是否存在文件。 Jenkins 代理在 Windows 上运行。 所以我将How to get response code from curl in a jenkinsfileHow do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)? 2 个stackoverflow 解决方案组合成以下代码:

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}"

我希望 env.CHECK_URL 包含 200(已找到)或 404(未找到)。但是 http 代码 200 被写入输出,而不是变量。 curl 返回错误 23 (Failed writing body)。

上述函数的输出:

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

任何建议如何解决它?或者如果文件存在于 Artifactory 中,Jenkinsfile 的另一种方法来查找 ouf?

【问题讨论】:

    标签: curl jenkins-pipeline artifactory


    【解决方案1】:

    虽然我没有成功使用 curl,但我至少能够找到使用 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()
    

    ...这取决于文件是否存在返回 0 或 1。

    【讨论】:

      猜你喜欢
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      相关资源
      最近更新 更多