首页 > 解决方案 > 如何在 Docker 容器之外执行部分 Jenkins 管道阶段?

问题描述

我们有一个容器化的 Jenkins 管道,对于其中一个阶段,阶段的一部分,我们希望在容器上执行,一些在 Jenkins 主服务器上执行(在我们的例子中是 Windows) -

pipeline {
    agent {
        docker {
            label "<node-name>"
            image "<docker-image-path>"
        }
    }
    stages {
        stage('Testing') {
            steps {
                script {
                    //This below part will be executed on container
                    println "This below part will be executed on container"
                    sh '''pwd
                        hostname -i     
                    '''

                    // Now want to execute below code on master which is Windows
                    println "Now want to execute below code on master which is Windows"
                    node('master') {
                        bat 'dir'
                    }
                }
            }
        }
    }
}

要在容器上执行的部分已成功执行,但要在 Windows Jenkins master 上执行的代码失败 -

Cannot run program "docker" (in directory "C:\Jenkins\workspace\TestDocker"): CreateProcess error=2, The system cannot find the file specified

编辑

当我在 Windows 机器上安装 docker 时,不会抛出上述错误,而是永远卡在那里。

你能帮我如何按需在节点或容器上执行代码吗?

标签: dockerjenkinsdockerfilejenkins-pipelinejenkins-groovy

解决方案


推荐阅读