问题描述
我在声明性管道中使用了多个代理。有没有将工件(input.txt)从agent1复制到agent2?这是我的声明性管道,
pipeline {
agent none
stages {
stage('Build') {
agent {
label 'agent1'
}
steps {
sh 'echo arjun > input.txt'
}
post {
always {
archiveArtifacts artifacts: 'input.txt',fingerprint: true
}
}
}
stage('Test') {
agent {
label 'agent2'
}
steps {
sh 'cat input.txt'
}
}
}
}
解决方法
您可以使用 Copy Artifact Plugin 来做到这一点。
给定您的 Jenkinsfile,它会变成这样:
pipeline {
agent none
stages {
stage('Build') {
agent { label 'agent1' }
steps {
sh 'echo arjun > input.txt'
}
post {
always {
archiveArtifacts artifacts: 'input.txt',fingerprint: true
}
}
}
stage('Test') {
agent { label 'agent2' }
steps {
// this brings artifacts from job named as this one,and this build
step([
$class: 'CopyArtifact',filter: 'input.txt',fingerprintArtifacts: true,optional: true,projectName: env.JOB_NAME,selector: [$class: 'SpecificBuildSelector',buildNumber: env.BUILD_NUMBER]
])
sh 'cat input.txt'
}
}
}
}
,
使用 stash 和 unstash。
示例来自: https://www.jenkins.io/doc/pipeline/examples/
// First we'll generate a text file in a subdirectory on one node and stash it.
stage "first step on first node"
// Run on a node with the "first-node" label.
node('first-node') {
// Make the output directory.
sh "mkdir -p output"
// Write a text file there.
writeFile file: "output/somefile",text: "Hey look,some text."
// Stash that directory and file.
// Note that the includes could be "output/","output/*" as below,or even
// "output/**/*" - it all works out basically the same.
stash name: "first-stash",includes: "output/*"
}
// Next,we'll make a new directory on a second node,and unstash the original
// into that new directory,rather than into the root of the build.
stage "second step on second node"
// Run on a node with the "second-node" label.
node('second-node') {
// Run the unstash from within that directory!
dir("first-stash") {
unstash "first-stash"
}
// Look,no output directory under the root!
// pwd() outputs the current directory Pipeline is running in.
sh "ls -la ${pwd()}"
// And look,output directory is there under first-stash!
sh "ls -la ${pwd()}/first-stash"
}