Jenkins Multibranch的工作与每个阶段的声明性管道克隆回购

Cos*_*min 3 jenkins jenkins-pipeline

尝试使用Declarative Pipeline在Jenkins中创建一个工作流来执行以下操作:

  1. 查看'master'上的代码
  2. 在'master'上构建解决方案(我知道这不是一种安全的方法,但是Jenkins在Intranet中,所以对我们来说应该没问题)
  3. 存储工件(.dll,.exe,.pdb等)=>第一阶段
  4. 根据需要对节点上的工件进行解压缩(从属设备上的单元测试,另一个上的集成测试和另一个上的Selenium测试)=>第二阶段
  5. 根据slave =>并行运行的第3阶段运行测试

我面临的问题是每个阶段都会执行git checkout(GitSCM).

我的管道看起来像这样:

pipeline {
    agent {
        label {
            label "master"
            customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
        }
    }

    options {
        timestamps()
    }

    stages {
        stage("Build") {
            agent {
                label {
                    label "master"
                    customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
                }
            }
            steps {
                /*
                    steps to build the solution here
                */

                //Sleep because stashing fails otherwise
                script {
                    sleep(1)
                }

                dir("${env.WORKSPACE}\\UnitTests\\bin\\Release") {
                    stash name: 'unit-tests'
                }

                dir("${env.WORKSPACE}\\WebUnitTests\\bin\\x64\\Release") {
                    stash name: 'web-unit-tests'

            }
        }

        stage('Export artefacts') {
            agent {
                label {
                    label "UnitTest"
                    customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
                }
            }
            steps {
                echo "Copying dlls from master to ${env.NODE_NAME}"
                dir("${env.WORKSPACE}\\UnitTests\\bin\\Release") {
                    unstash 'unit-tests'
                }
            }
        }

        stage('Run tests') {
            parallel {
                stage("Run tests #1") {
                    agent {
                        label {
                            label "UnitTest"
                            customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
                        }
                    }
                    steps {
                        /*
                            run tests here
                        */
                    }
                    post {
                        //post results here
                    }
                }
                //other parallel stages
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,如前所述,GitSCM(代码检出)是每个阶段的一部分并执行:构建阶段 在此输入图像描述

出口阶段 在此输入图像描述

Rob*_*les 5

一些简单的改变应该解决这个问题.每次分配节点时,您都​​需要告诉管道脚本默认不签出.然后你需要告诉它在你需要的地方办理结账:

pipeline {
    agent {
        label {
            label "master"
            customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
        }
    }

    options {
        timestamps()
        skipDefaultCheckout()      // Don't checkout automatically
    }

    stages {
        stage("Build") {
            agent {
                label {
                    label "master"
                    customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
                }
            }
            steps {
                checkout scm                      //this will checkout the appropriate commit in this stage
                /*
                    steps to build the solution here
                */

                //Sleep because stashing fails otherwise
                script {
                    sleep(1)
                }

                dir("${env.WORKSPACE}\\UnitTests\\bin\\Release") {
                    stash name: 'unit-tests'
                }

                dir("${env.WORKSPACE}\\WebUnitTests\\bin\\x64\\Release") {
                    stash name: 'web-unit-tests'

            }
        }

        stage('Export artefacts') {
            agent {
                label {
                    label "UnitTest"
                    customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
                }
            }
            steps {
                echo "Copying dlls from master to ${env.NODE_NAME}"
                dir("${env.WORKSPACE}\\UnitTests\\bin\\Release") {
                    unstash 'unit-tests'
                }
            }
        }

        stage('Run tests') {
            parallel {
                stage("Run tests #1") {
                    agent {
                        label {
                            label "UnitTest"
                            customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
                        }
                    }
                    steps {
                        /*
                            run tests here
                        */
                    }
                    post {
                        //post results here
                    }
                }
                //other parallel stages
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我在那里添加了2行.一个在选项部分(skipDefaultCheckout())中,一个checkout scm在第一阶段.