dsf5989 2018-11-28 19:45
浏览 91
已采纳

如何在每个测试中重复使用预处理的詹金斯/凹槽

Im using the following code to run our voter , currently I’ve one target which is called Run Tests Which use exactly the same steps as the last (lint) , currently I duplicate it which I think is not a good solution , Is there is nice way to avoid this duplication and done it only once as per-requisite process ?

I need all the steps until the cd to the project

The only difference is one target I run

go test ...

and the second

go lint 

All steps before are equal

#!/usr/bin/env groovy

    try {
        parallel(
                'Run Tests': {
                    node {

                        //————————Here we start
                        checkout scm
                        def dockerImage = 'docker.company:50001/crt/deg:0.1.3-09’
                        setupPipelineEnvironment script: this, 
                        measureDuration(script: this, measurementName: 'build') {
                            executeDocker(dockerImage: dockerImage,  dockerWorkspace: '/go/src') {
                                sh """
                                mkdir -p /go/src/github.com/ftr/myGoProj
                                cp -R $WORKSPACE/* /go/src/github.com/ftr/MyGoProj
                                cd  /go/src/github.com/ftr/MyGoProj
                        //————————Here we finish and TEST
                                go test -v ./...                           
                                """
                            }
                        }
                    }
                },
                ‘Lint’: {
               node {
                        //————————Here we start
                        checkout scm
                        def dockerImage = 'docker.company:50001/crt/deg:0.1.3-09’
                        setupPipelineEnvironment script: this, 
                        measureDuration(script: this, measurementName: 'build') {
                            executeDocker(dockerImage: dockerImage,  dockerWorkspace: '/go/src') {
                                sh """
                                mkdir -p /go/src/github.com/ftr/myGoProj
                                cp -R $WORKSPACE/* /go/src/github.com/ftr/MyGoProj
                                cd  /go/src/github.com/ftr/MyGoProj
                        //————————Here we finish and LINT
                               go lint
                              """

                }
                }
        )
    }

        }
    }
  • 写回答

1条回答 默认 最新

  • dongqi8114 2018-11-29 09:03
    关注

    You can use function and pass Go arguments:

    try {
        parallel(
            'Run Tests': {
                node {
                    checkout scm
                    runTestsInDocker('test -v ./...')
                }
            },
            'Lint': {
                node {
                    checkout scm
                    runTestsInDocker('lint')
                }
            }
        )
    }
    
    
    def runTestsInDocker(goArgs) {
        def dockerImage = 'docker.company:50001/crt/deg:0.1.3-09'
        setupPipelineEnvironment script: this, 
        measureDuration(script: this, measurementName: 'build') {
            executeDocker(dockerImage: dockerImage,  dockerWorkspace: '/go/src') {
                sh """
                mkdir -p /go/src/github.com/ftr/myGoProj
                cp -R $WORKSPACE/* /go/src/github.com/ftr/MyGoProj
                cd  /go/src/github.com/ftr/MyGoProj
                go ${goArgs}                        
                """
            }
        }
    }
    

    Update

    If some actions can be separated out of runTestsInDocker they probably should be.

    For example setupPipelineEnvironment step. I don't know exact logic but maybe it can be run once before running test.

    node {
        stage('setup') {
            setupPipelineEnvironment script: this
        }
        stage ('Tests') {
            parallel(
                'Run Tests': {
                    node {
                        checkout scm
                        runTestsInDocker('test -v ./...')
                    }
                },
                'Lint': {
                    node {
                        checkout scm
                        runTestsInDocker('lint')
                    }
                }
            )
        }
    }
    
    
    def runTestsInDocker(goArgs) {
        def dockerImage = 'docker.company:50001/crt/deg:0.1.3-09'
        measureDuration(script: this, measurementName: 'build') {
            executeDocker(dockerImage: dockerImage,  dockerWorkspace: '/go/src') {
                sh """
                mkdir -p /go/src/github.com/ftr/myGoProj
                cp -R $WORKSPACE/* /go/src/github.com/ftr/MyGoProj
                cd  /go/src/github.com/ftr/MyGoProj
                go ${goArgs}                        
                """
            }
        }
    }
    

    Note

    If you are running parallel on remote agents you must remember that setup performed on master may be not aviailable on remote slave.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样