Jenkins pipeline, how can I copy artifact from previous build to current build?

本小妞迷上赌 提交于 2020-04-30 10:41:20

问题


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.


回答1:


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")
            }
        }
    }
}



回答2:


You Can Use Copy Artifact Plugin

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




回答3:


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



来源:https://stackoverflow.com/questions/54382949/jenkins-pipeline-how-can-i-copy-artifact-from-previous-build-to-current-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!