Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the Jenkins Copy Artifacts Plugin from within the pipelines (jenkinsfile)?

I am trying to find an example of using the Jenkins Copy Artifacts Plugin from within Jenkins pipelines (workflows).

Can anyone point to a sample Groovy code that is using it?

like image 439
sorin Avatar asked Apr 25 '16 14:04

sorin


People also ask

Under which section is the copy artifacts option present in Jenkins?

Next you need to install the Copy Artifact plugin in the Manage Plugins section of Jenkins. Go to "[PROJECT-NAME]-Output" > configure and add a new build step. Because you have installed the Copy Artifact plugin you should see an option called 'copy artifacts from another project' in the drop down menu.

How do you get artifacts in Jenkins?

You can find jenkins home by going to the environment variables page in the job build jenkins page. Show activity on this post. You have to extract branch name, job name and repository name from JOB_NAME environment variable.


2 Answers

With a declarative Jenkinsfile, you can use following pipeline:

pipeline {     agent any     stages {         stage ('push artifact') {             steps {                 sh 'mkdir archive'                 sh 'echo test > archive/test.txt'                 zip zipFile: 'test.zip', archive: false, dir: 'archive'                 archiveArtifacts artifacts: 'test.zip', fingerprint: true             }         }          stage('pull artifact') {             steps {                 copyArtifacts filter: 'test.zip', fingerprintArtifacts: true, projectName: '${JOB_NAME}', selector: specific('${BUILD_NUMBER}')                 unzip zipFile: 'test.zip', dir: './archive_new'                 sh 'cat archive_new/test.txt'             }         }     } } 

Before version 1.39 of the CopyArtifact, you must replace second stage with following (thanks @Yeroc) :

stage('pull artifact') {     steps {         step([  $class: 'CopyArtifact',                 filter: 'test.zip',                 fingerprintArtifacts: true,                 projectName: '${JOB_NAME}',                 selector: [$class: 'SpecificBuildSelector', buildNumber: '${BUILD_NUMBER}']         ])         unzip zipFile: 'test.zip', dir: './archive_new'         sh 'cat archive_new/test.txt'     } } 

With CopyArtifact, I use '${JOB_NAME}' as project name which is the current running project.

Default selector used by CopyArtifact use last successful project build number, never current one (because it's not yet successful, or not). With SpecificBuildSelector you can choose '${BUILD_NUMBER}' which contains current running project build number.

This pipeline works with parallel stages and can manage huge files (I'm using a 300Mb file, it not works with stash/unstash)

This pipeline works perfectly with my Jenkins 2.74, provided you have all needed plugins

like image 166
Nelson G. Avatar answered Oct 21 '22 21:10

Nelson G.


If you are using slaves in your master and you want to copy artifacts between each other you can use stash/unstash, for example:

stage 'build' node{    git 'https://github.com/cloudbees/todo-api.git'    stash includes: 'pom.xml', name: 'pom' }  stage name: 'test', concurrency: 3 node {    unstash 'pom'    sh 'cat pom.xml'  } 

You can see this example in this link:

https://dzone.com/refcardz/continuous-delivery-with-jenkins-workflow

like image 28
Daniel Hernández Avatar answered Oct 21 '22 22:10

Daniel Hernández