Copy artifacts from several collections to the same job in Jenkins

I use the MultiJob plugin and have a job ( Job-A ) that launches Job-B several times. My requirement is to copy some artifacts (xml files) from each assembly.

The difficulty I am facing is that using the Copy Artifact plugin with the “last successful build” option will only take the last Job-B assembly, while I need to copy all the assemblies that were running on the same assembly of Job-A

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

 `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 can be run on different slaves at a single start (I set the slave to dynamically start by setting the parameter for job up-A)

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

+5
source share
3 answers

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

It simply copies artifacts from all downstream jobs to the upstream workspace.

If you call the same task several times, you can use the task number in the copyAtifact 'target' parameter to save artifacts separately.

 // This script copies artifacts from downstream jobs into the upstream job 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) } 
+5
source

I offer you the following approach:

  • Use the Execute System Groovy script from the Groovy Plugin to execute the following script:

     import hudson.model.* // get upstream job def jobName = build.getEnvironment(listener).get('JOB_NAME') def job = Hudson.instance.getJob(jobName) def upstreamJob = job.upstreamProjects.iterator().next() // prepare build numbers def n1 = upstreamJob.lastBuild.number def n2 = n1 - 1 def n3 = n1 - 2 // set parameters def pa = new ParametersAction([ new StringParameterValue("UP_BUILD_NUMBER1", n1.toString()), new StringParameterValue("UP_BUILD_NUMBER2", n2.toString()), new StringParameterValue("UP_BUILD_NUMBER3", n3.toString()) ]) Thread.currentThread().executable.addAction(pa) 

    This script will create three environment variables that correspond to the last three line numbers of the overlying job.

  • Add the three build steps of Copy artifacts from upstream project to copy artifacts from the last three collections of the upstream project (use the environment variables from the script above to set the build number):

enter image description here

  1. Run the build and validation build log, you should have something like this:

     Copied 2 artifacts from "A" build number 4 Copied 2 artifacts from "A" build number 3 Copied 1 artifact from "A" build number 2 

Note : perhaps the script needs to be adjusted to catch unusual cases, such as “an upstream project has only two assemblies”, “the current task does not have an upstream task”, “the current task has more than one upstream operation”, etc.

+2
source

You can use the following example from the Run Command Line assembly phase. Please note that it can only be run from the Jenkins Master machine, and the job invoking this step also called MultiJob.

 #-------------------------------------- # Copy Artifacts from MultiJob Project #-------------------------------------- PROJECT_NAME="MY_MULTI_JOB" ARTIFACT_PATH="archive/target" TARGET_DIRECTORY="target" mkdir -p $TARGET_DIRECTORY runCount="TRIGGERED_BUILD_RUN_COUNT_${PROJECT_NAME}" for ((i=1; i<=${!runCount} ;i++)) do buildNumber="${PROJECT_NAME}_${i}_BUILD_NUMBER" cp $JENKINS_HOME/jobs/$PROJECT_NAME/builds/${!buildNumber}/$ARTIFACT_PATH/* $TARGET_DIRECTORY done #-------------------------------------- 
0
source

All Articles