dsf5989
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
    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.

    点赞 评论

相关推荐