5

In Jenkins Pipeline, how can I copy the artifacts from a previous build to the current build? I want to do this even if the previous build failed.

Craig Rodrigues
  • 771
  • 2
  • 7
  • 16

3 Answers3

12

Stuart Rowe also recommended to me on the Pipeline Authoring Sig Gitter channel that I look at the Copy Artifact Plugin, but also gave me some sample Jenkins Pipeline syntax to use.

Based on the advice that he gave, I came up with this fuller Pipeline example which copies the artifacts from the previous build into the current build, whether the previous build succeeded or failed.

pipeline {
    agent any;

    stages {
        stage("Zeroth stage") {
            steps {
                script {
                    if (currentBuild.previousBuild) {
                        try {
                            copyArtifacts(projectName: currentBuild.projectName,
                                          selector: specific("${currentBuild.previousBuild.number}"))
                            def previousFile = readFile(file: "usefulfile.txt")
                            echo("The current build is ${currentBuild.number}")
                            echo("The previous build artifact was: ${previousFile}")
                        } catch(err) {
                            // ignore error
                        }
                    }
                }
            }
        }

        stage("First stage") {
            steps {
                echo("Hello")
                writeFile(file: "usefulfile.txt", text: "This file ${env.BUILD_NUMBER} is useful, need to archive it.")
                archiveArtifacts(artifacts: 'usefulfile.txt')
            }
        }

        stage("Error") {
            steps {
                error("Failed")
            }
        }
    }
}
bdbaddog
  • 3,357
  • 2
  • 22
  • 33
Craig Rodrigues
  • 771
  • 2
  • 7
  • 16
2

Suppose you want a single file to from previous build, you can even use curl to place file in workspace before mvn invocation.

stage('Copy csv') {
            steps {
                   sh "mkdir -p ${env.WORKSPACE}/dump"
                    sh "curl http://<jenkins-url>:<port>/job/<job-folder>/job/<job-name>/job/<release>/lastSuccessfulBuild/artifact/dump/sample.csv/*view*/ -o ${env.WORKSPACE}/dump/sample.csv"
            }
        }    

Thanks, Ashish

ashish
  • 21
  • 1
  • And if your jenkins requires authentication you can pass the credentials on the url as stated on [this question](https://stackoverflow.com/questions/10698419/how-can-a-jenkins-user-authentication-details-be-passed-to-a-script-which-uses) – André Roggeri Campos Jan 20 '22 at 21:13
0

You Can Use Copy Artifact Plugin

For configuration visit https://wiki.jenkins.io/display/JENKINS/Copy+Artifact+Plugin

Subhash
  • 762
  • 9
  • 25
  • 3
    Yes, the Copy Artifact Plugin can be used, but how? All the examples point to copying artifacts from other Jenkins jobs, not from previous builds of the same Jenkins job. Also, how can this be done in a Pipeline script? Not via a menu configuration in a Freestyle job? – Craig Rodrigues Jan 27 '19 at 22:30
  • 1
    `stage('Copy') { copyArtifacts filter: '**/*.json', projectName: 'pipeline-test', selector: lastSuccessful(), target: 'dest' }` the project name can be any job. Even the same to get the previous build's artifact – Kaus2b Oct 10 '19 at 23:19
  • io performance of copyArtifact plugin is abysmal when traversing a network from controller to agent. You will get faster throughput using sh/curl, especially for larger artifact files and on faster network links. Ashish's first answer should be accepted. – timblaktu Feb 27 '21 at 03:16