简体   繁体   中英

Jenkins Pipeline check if parameterized build is really parameterized

Currently i am working with Jenkins pipelines and parameterized builds.

I know i can start a pipeline stage where i can check if the given parameters are empty or not, but is there another way how to check if a parameterized build is really parameterized?

Like a step before i go into stages? A stage where we can check some conditions before going into the stages.

In Declarative Pipeline where is no way run something 'before'. You can run another job which trigger 'the job' or try something like this, if some parameters not pass validation whole job will be aborted. But You can control that in script block.

def ex(param){
    currentBuild.result = 'ABORTED'
    error('BAD PARAM: ' + param)
}

pipeline {
    agent any

    parameters {
        choice(name: 'a', choices:"NO\nOK\nMaybe", description: "Tests?" )
        choice(name: 'b', choices:"NO\nOK\nMaybe", description: "Tests?" )
        choice(name: 'c', choices:"NO\nOK\nMaybe", description: "Tests?" )
        choice(name: 'd', choices:"NO\nOK\nMaybe", description: "Tests?" )
    }   

    stages { 
        stage("check params") {
            steps {
                script {
                    //make some crazy validation
                    if ("${params.a}" != "OK") {ex("a")}
                    if ("${params.b}" != "OK") {ex("b")}
                    if ("${params.c}" != "OK") {ex("c")}
                    if ("${params.d}" != "OK") {ex("d")}
                }
            }
        }
        stage("Next stage") {
            steps {
                echo "Run"
            }
        }
    }
}

One can also use params.each to check if any parameter "choices" is null or empty string "" or are not the value is should be like "OK" etc...

Useful when parameters are passed from another job, and a variable or method is used which happened to not have an acceptable value:

build job: "some-jobname",
        parameters: [
                [$class: 'StringParameterValue', name: 'a', value: thisMethodReturnsOK()],
                [$class: 'StringParameterValue', name: 'b', value: oopsThisMethodReturnsEmptySTRING()],
                [$class: 'StringParameterValue', name: 'c', value: oopsThisMethodReturnsNULL()],
                [$class: 'StringParameterValue', name: 'd', value: oopsThisVariableContainsUnacceptableVALUE],
                           ]

This modified version will validate each available build parameter and cause an ABORT if parameter is BAD :

# Job: "some-jobname"

def ex(param){
    currentBuild.result = 'ABORTED'
    error('BAD PARAM: ' + param)
}

pipeline {
    agent any

    testA = param.a
    testB = param.b
    testC = param.c
    testD = param.d

    stages { 
        stage("check params") {
            steps {
                script {
                    params.each {
                        if (it == null || it == "" || it != "OK")
                            ex(it)
                    }
                }
            }
        }
        stage("Next stage") {
            steps {
                echo "Run and use parameters passed to TestA etc..."
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM