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.

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

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus