詹金斯:从最后阶段将工件从一个代理复制到另一个代理

Jenkins : Copy artifacts from one agent to another from last stage

我在声明性管道中使用了多个代理。无论如何要将工件 (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'
            }
        }
    }
}

使用存储和取消存储。

示例来自: 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"
}