Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy artifacts of multiple builds on same job in Jenkins

Tags:

jenkins

I'm using MultiJob plugin and have a job (Job-A) that triggers Job-B several times. My requirement is to copy some artifact (xml files) from each build.

The difficulty I have is that using Copy Artifact Plugin with "last successful build" option will only take the last build of Job-B, while I need to copy from all builds that were triggered on the same build of Job-A

The flow looks like: Job-A starts and triggers:

`Job-A` -->
   Job-B build #1
   Job-B build #2
   Job-B build #3
   ** copy artifcats of all last 3 builds, not just #3 **

Note: Job-B could be executed on different slaves on the same run (I set the slave to run on dynamically by setting parameter on upstream job-A)

When all builds are completed, I want Job-A to copy artifact from build #1, #2 and #3 , and not just from last build. How can I do this?

like image 486
etaiso Avatar asked Aug 16 '15 08:08

etaiso


People also ask

How to copy artifacts from one build to another in Jenkins?

Copy artifacts from a build that is a downstream of a build of the specified project. You can use variable expressions. Downstream builds are found using fingerprints of files. That is, a build that is triggered from a build isn't always considered downstream, but you need to fingerprint files used in builds to let Jenkins track them.

How to use'use the newest build'in Jenkins?

"Use the newest" copies artifacts from the upstream build with the largest build number (that is, newest). The default value is "Use global setting", which behaves as configured in "Manage Jenkins" > "Configure System". Target base directory for copy, or leave blank to use the workspace.

Why can't I find my project for artifact copy?

Unable to find project for artifact copy: YOUR_PROJECT_WITH_ARTIFACTS This may be due to incorrect project name or permission settings; see help for project name in job configuration. Build step 'Copy artifacts from another project' marked build as failure.

How do I allow a task to copy artifacts?

When configuring jobs, we can add information about which task can copy artifacts: On the job configuration page there is an option Permission to Copy Artifact. When this option is checked, we can specify tasks that will have permission to copy artifacts.


1 Answers

Here is more generic groovy script; it uses the groovy plugin and the copyArtifact plugin; see instructions in the code comments.

It simply copies artifacts from all downstream jobs into the upstream job's workspace.

If you call the same job several times, you could use the job number in the copyArtifact's 'target' parameter to keep the artifacts separate.

// This script copies artifacts from downstream jobs into the upstream job's workspace.
//
// To use, add a "Execute system groovy script" build step into the upstream job
// after the invocation of other projects/jobs, and specify
// "/var/lib/jenkins/groovy/copyArtifactsFromDownstream.groovy" as script.

import hudson.plugins.copyartifact.*
import hudson.model.AbstractBuild
import hudson.Launcher
import hudson.model.BuildListener
import hudson.FilePath

for (subBuild in build.builders) {
  println(subBuild.jobName + " => " + subBuild.buildNumber)
  copyTriggeredResults(subBuild.jobName, Integer.toString(subBuild.buildNumber))
}

// Inspired by http://kevinormbrek.blogspot.com/2013/11/using-copy-artifact-plugin-in-system.html
def copyTriggeredResults(projName, buildNumber) {
   def selector = new SpecificBuildSelector(buildNumber)

   // CopyArtifact(String projectName, String parameters, BuildSelector selector,
   // String filter, String target, boolean flatten, boolean optional)
   def copyArtifact = new CopyArtifact(projName, "", selector, "**", null, false, true)

   // use reflection because direct call invokes deprecated method
   // perform(Build<?, ?> build, Launcher launcher, BuildListener listener)
   def perform = copyArtifact.class.getMethod("perform", AbstractBuild, Launcher, BuildListener)
   perform.invoke(copyArtifact, build, launcher, listener)
}
like image 80
tarantoga Avatar answered Sep 21 '22 06:09

tarantoga