Jenkins Copy Artifacts from Upstream Job to Downstream and back

Viewed 791

I have Job A that call to another Job B

Job B know copy from Job A the artifact.

How I can do copy back of another artifact from Job B to Job A?

Job A
//////////

 stage('Validation'){
                'Binary Scan':{
                    build job: 'JOB_B'
                },    
           )
        }


Job B 
/////////

stage('Copy Artifacts from Job A'){

copyArtifacts filter: 'testz.zip', 
fingerprintArtifacts: true,
projectName: currentBuild.upstreamBuilds[0].fullProjectName,
selector: upstream()

}

stage('Do something'){
}


stage('Copy Artifacts back to Job A'){

/////?????????
}
1 Answers

From within a project, you can't normally execute arbitrary code into a different project.

What you can do is for the Job B to use archiveArtifacts, and when it completes, the calling job (Job A) can retrieve the artifacts from the instance of Job B it triggered. The build command will return a job 'handle' that has enough information to let you build a copyArtifacts command.

job A
//////////

stage('Validation'){
     //////
     def jobB = build job: 'JOB_B'

     // Retrieve artifacts from Job B after it completes
     final String artifactsFilter = "build/*.csv"
     copyArtifacts filter: artifactsFilter, projectName: "JOB_B", selector: specific("${jobB.number}")
     // re-attach the same artifacts to Job A
     archiveArtifacts artifacts: artifactsFilter
}


Job B 
/////////

stage('Copy Artifacts from Job A'){
    copyArtifacts filter: 'testz.zip', fingerprintArtifacts: true,
                projectName: currentBuild.upstreamBuilds[0].fullProjectName,
                selector: upstream()
}
stage('Do something'){
}
stage('Archive Artifacts for Job A'){
    // Archive desired artifacts
    archiveArtifacts artifacts: "build/**.csv"
}
Related