简体   繁体   English

Jenkins Pipeline 检查参数化构建是否真的参数化

[英]Jenkins Pipeline check if parameterized build is really parameterized

Currently i am working with Jenkins pipelines and parameterized builds.目前我正在使用 Jenkins 管道和参数化构建。

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.但是您可以在script块中控制它。

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...还可以使用params.each来检查任何参数“choices”是否为null或空字符串""或者值是否应该像“OK”等...

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 :此修改后的版本将验证每个可用的构建参数并在参数为BAD导致ABORT

# 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..."
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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