Jenkins等待工件下载完成

vohkndzv  于 2023-04-20  发布在  Jenkins
关注(0)|答案(2)|浏览(141)

我正在使用Artifactory下载多个工件

rtDownload (
serverId: 'Artifactory-1',
spec: '''{
      "files": [
        {
          "pattern": "bazinga-repo/froggy-files/",
          "target": "bazinga/"
        }
      ]
}''',

// Optional - Associate the downloaded files with the following custom build name and build number,
// as build dependencies.
// If not set, the files will be associated with the default build name and build number (i.e the
// the Jenkins job name and number).
buildName: 'holyFrog',
buildNumber: '42'
)

但是这个是异步工作的,我必须在它完成后立即使用结果。我如何在管道语法中等待每个rtDownload?

vcudknz3

vcudknz31#

这段代码可以下载两个工件:

def target = params.BuildInfo.trim()

def downloadSpec = """{
  "files": [{
    "pattern": "${artifactory}.zip",
    "target": "./${target}.zip"
  }]
}"""

def buildInfo = server.download spec: downloadSpec
def files = findFiles(glob: "**/${target}.zip") // Define `files` here

if (files) { // And verify `it` here, so it'll wait 
...
}
vc9ivgsu

vc9ivgsu2#

就我个人而言,我最终以这种方式实现了Khoa的想法

try {
    // attempt to retreive the results from artifactory
    rtDownload (
      serverId: 'my_arti_server',
      spec: """{
        "files": [
          {
            "pattern": "somepath/run_*.zip",
            "target": "run/"
          }
        ]
      }""",
      failNoOp: false, // no failure if no file was found
    )

    // rtDownload is async, we have to wait for dl completion
    def count = 5
    while(count > 0) {
      sh script:"""#!/bin/bash +e
        chmod 777 run/run_*.zip
      """  
      def files = findFiles glob: "run/run_*.zip"
      if (files.length > 0 ){
        break
      }
      sleep(5)
      count--
    }
  } catch (Exception e) {
    echo 'Exception occurred: ' + e.toString() 
  }

  def files = findFiles glob: "run/run_*.zip"
  if (files.length == 0 ){
    error("files couldn't be found")
  }

这不是完美的,但它等待一些文件存在。如果你只有一个文件,它应该工作,但如果你有几个文件,它可能会继续只要一个文件下载。我还没有检查,但与此我假设:

  • 一个文件可以找到一旦下载完成(没有文件大小经常改变)
  • 所有文件都被下载“在同一时间”

相关问题