Jenkins pipeline, how can I copy artifact from previous build to current build?
在 Jenkins Pipeline 中,如何将工件从以前的构建复制到当前构建?
即使以前的构建失败,我也想这样做。
Stuart Rowe 还向我推荐了 Pipeline Authoring Sig Gitter 频道,我查看了 Copy Artifact Plugin,还给了我一些 Jenkins Pipeline 语法示例供我使用。
根据他给出的建议,我想出了这个更完整的 Pipeline 示例
它将先前构建中的工件复制到当前构建中,
之前的构建是成功还是失败。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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") } } } } |
假设你想从以前的构建中获得一个文件,你甚至可以在 mvn 调用之前使用 curl 将文件放置在工作区中。
1 2 3 4 5 6 | 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" } } |
谢谢,
阿什
您可以使用复制工件插件
有关配置,请访问 https://wiki.jenkins.io/display/JENKINS/Copy Artifact Plugin