2

I wan to check from groovy pipeline if a specific file (artifact) was collected or not.

How can I access the list of artefacts?

archiveArtifacts artifacts: 'foo.txt', allowEmptyArchive: true
...
// much later
// check if 'foo.txt' was collected?

Please note that I am looking for a solution that does not imply modification of the code that collects the artifacts. This is because this code is in multiple places, I only need something to do at the end, not at any possible call of archiveArtifacts (which can be deeply hidden).

sorin
  • 161,544
  • 178
  • 535
  • 806

2 Answers2

2

You can get the list of artifacts using jenkins api in either XML/JSON format.

call the below url. insert job-name and build-number

  http://localhost:8080/jenkins/job/job-name/build-number/api/json?pretty=true 

Json Example:

"artifacts" : [
{
  "displayPath" : "temp.jar",
  "fileName" : "temp.jar",
  "relativePath" : "target/temp.jar"
}
]
TrafLaw
  • 308
  • 1
  • 9
  • This would not work because I would hit https://stackoverflow.com/questions/42380712/jenkins-pipeline-conditional-stage-succeeds-but-jenkins-shows-build-as-failed -- if this runs outside the node block, and it does, by design. I need something to access artifact list, i bet there is an object that can be queried. – sorin Nov 30 '17 at 14:10
  • i see. In that case Jenkins api can help you to get the info you need. I updated my answer – TrafLaw Nov 30 '17 at 14:32
  • 1
    I don't like the idea of using API to get this info as it would increase the Jenkins server load considerably. This should be accessible directly. – sorin Feb 08 '18 at 13:50
2

It's possible to access the artifacts using something like currentBuild.rawBuild.artifacts.each { println it.fileName }, but you will need to whitelist rawBuild, artifacts and fileName properties.