Jenkins wait for artifact download to finish

Viewed 602

So I am downloading multiple artifacts with jFrog

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'
)

Which works but this works async and I have to use the results as soon as it finished. How do I await for each rtDownload in pipeline syntax?

2 Answers

Below working for me with downloading 2 Artifacts :

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

Personally, I ended up implementing Khoa's idea this way

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

This is not perfect but it waits for some files to be present. If you have only one file it should work but if you have several files, it may continue as soon as one file is downloaded. I haven't checked but with this I assume that:

  • a file can be found once the download is completed (no file size changing regularly)
  • all files are downloaded "at the same time"
Related