【问题标题】:Jenkins Multibranch job with declarative pipeline cloning repo for every stageJenkins Multibranch 作业,每个阶段都有声明性管道克隆 repo
【发布时间】:2017-10-30 15:09:58
【问题描述】:

尝试使用声明式管道在 Jenkins 中创建工作流来执行以下操作:

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

我面临的问题是每个阶段都会执行 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
            }
        }
    }
}

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

出口阶段

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    几个简单的改变应该可以解决这个问题。每次分配节点时,您都​​需要告诉管道脚本默认情况下不要签出。然后你需要告诉它在你需要的地方进行结帐:

    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
                }
            }
        }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 2022-10-27
      相关资源
      最近更新 更多