48

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?

sorin
  • 161,544
  • 178
  • 535
  • 806

4 Answers4

60

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: env.JOB_NAME, selector: specific(env.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

Kevin Brotcke
  • 3,765
  • 26
  • 34
Nelson G.
  • 5,145
  • 4
  • 43
  • 54
  • Do you happen to know where this step([ $class: 'CopyArtifact', syntax is documented? I think I remember it was generated by the Snippet generator, can't find it there now.. I'm especially wondering how you could use the parametrized build selector. – inger Oct 18 '17 at 15:33
  • I saw an exemple [here](https://wiki.jenkins.io/display/JENKINS/Copy+Artifact+Plugin). About `SpecificBuildSelector`, I guessed it by analyzing the [plugin code](https://github.com/jenkinsci/copyartifact-plugin/tree/copyartifact-1.38.1/src/main/java/hudson/plugins/copyartifact) – Nelson G. Oct 18 '17 at 16:15
  • 6
    As of version 1.39 of the CopyArtifact plugin you can use cleaner syntax: `copyArtifacts filter: 'test.zip', fingerprintArtifacts: true, projectName: '${JOB_NAME}', selector: specific('${BUILD_NUMBER}')` – Yeroc Mar 16 '18 at 22:57
  • 3
    If anyone has issues with Jenkins stating that it is unable to find the project to copy the artifact from, changing `projectName: '${JOB_NAME}', selector: specific('${BUILD_NUMBER}')` to `projectName: env.JOB_NAME, selector: specific(env.BUILD_NUMBER)` worked for me (see also https://stackoverflow.com/a/55377899/758165). – Pit Mar 27 '19 at 13:05
  • 1
    copyArtifacts(projectName: /') worked for me – JARC Jun 07 '19 at 08:50
34

If you are using agents in your controller 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

briantist
  • 45,546
  • 6
  • 82
  • 127
Daniel Hernández
  • 4,078
  • 6
  • 27
  • 38
32

If builds are not running in the same pipeline you can use direct CopyArtifact plugin, here is example: https://www.cloudbees.com/blog/copying-artifacts-between-builds-jenkins-workflow and example code:

node {
   // setup env..
   // copy the deployment unit from another Job...
   step ([$class: 'CopyArtifact',
          projectName: 'webapp_build',
          filter: 'target/orders.war']);
   // deploy 'target/orders.war' to an app host
}
krynio
  • 2,442
  • 26
  • 30
1
name = "/" + "${env.JOB_NAME}"
def archiveName = 'relNum'
try {
    step($class: 'hudson.plugins.copyartifact.CopyArtifact', projectName: name, filter: archiveName)
} catch (none) {
    echo 'No artifact to copy from ' + name + ' with name relNum'
    writeFile file: archiveName, text: '3'
}

def content = readFile(archiveName).trim()
echo 'value archived: ' + content

try that using copy artifact plugin