首页 > 解决方案 > 在詹金斯奴隶之间复制文件在管道中

问题描述

我们的目标是用多个代理划分我们的管道。

我们有一个名为 slave1 的从站,它的唯一目的是在 git 上签出,并构建可执行文件。

最终,当 slave1 完成时,我们希望将它的输出传递给 slave2,它的唯一目的是测试 slave1 的可执行文件。

请注意,这里的想法不是拆分作业,而是在同一管道中实现文件。

Jenkinsfile这是一个更有意义的示例:

pipeline
{
    agent
    {
        label 'slave1'
    }
    stages
    {
        stage("Initialize & Build")
        {
            steps
            {
                script
                {
                    println("Im starting the pipeline with slave1!")

                    // Builds Files
                    // ....

                    // Has many files that needs to pass to slave2
                }
            }
        }
        stage("Execute & Test")
        {
            agent
            {
                label 'slave2'
            }
            steps
            {
                script
                {
                    println("Im in the new slave - slave2!")

                    // How does this slave get the files?
                }
            }
        }
    }
}

如何在代理之间传递这些文件?

我阅读了有关工件的信息,但似乎它的目标是从工作中返回对象,这不一定是需要的。

标签: jenkinsjenkins-pipelinejenkins-groovy

解决方案


如果您的两个代理都在 linux 服务器上,您可以简单地将代理 1 的构建输出 scp 到代理 2。此外,首先您需要在这 2 个代理之间建立无密码 SSH 连接。

这是一个例子。

pipeline
{
    agent
    {
        label 'slave1'
    }
    stages
    {
        stage("Initialize & Build")
        {
            steps
            {
                script
                {
                    println("Im starting the pipeline with slave1!")

                    // Builds Files
                    // ....
 // option 1: copy files in the workspace of agent 2.
                    scp $WORKSPACE/build_output/* <user>@agent2:/home/<user>/workspace/<job_name>/
// option 2: copy files to any known location of agent 2
                     scp $WORKSPACE/build_output/* <user>@agent2:/<destination_path>



                }
            }
        }
        stage("Execute & Test")
        {
            agent
            {
                label 'slave2'
            }
            steps
            {
                script
                {
                    println("Im in the new slave - slave2!")

                    //option 1
                         dir($WORKSPACE) {
                               // Test execution steps
                   }
                     // option 2
                          dir(<destination_path>) {
                              // Test execution steps
                   }
                }
            }
        }
    }
}

推荐阅读