English 中文(简体)
Jenkins: 如何在参数化构建中设定所需的参数?
原标题:Jenkins: how to make parameters required in a parameterized build?
  • 时间:2012-05-24 17:26:32
  •  标签:
  • jenkins

在 Jenkins 中是否有一个参数化构建插件来设定所需的参数? 标准“ 此构建是参数化” 选项下的字段似乎没有提供 。

澄清 : “ 需要 ” 是指 建设不会执行, 直到字段中包含一个值。 这显然排除了自动触发 。

最佳回答

This is the plugin i use to do this type of stuff: link...
You can set a regular expression to validate the input against

问题回答

被接受的回答不再有效。

有一个 < a href=>", https://wiki.jenkins.io/display/JENKINS/Validate+String+Parater+Plugin" rel="无跟踪 nofollow noreferrer" > 插件, 这样做, 但不再维持。

有 < a href=> "https://issues.jenkins-ci.org/browse/JENKINS-3509" rel="没有跟随 nofollow noreferrer" > 打开的错误来支持它 。

平均时间中,您可以做的是检查参数是否存在,如果不是扔出错误,比如:

if (!params.SomeParam) {
    error("Build failed because of this and that..")
}

无法回答Miguel的提问,

如果参数未设定, 无法完成构建, 可以做类似的事情 :

stage( Checkout ) 
    {
        steps
        {
            checkout scm
            script 
            {
                if (params.myParam ==   ) { // and/or whatever condition you want
                    currentBuild.result =  ABORTED 
                    error( myParam not set )
                }
            }
        }
    }

There s a plugin called "Validating String Parameter". When you install this plugin in your project, you see an additional option of Validating String Parameter while adding parameters. Using this option will show an additional column of Regular expression. For non-empty string parameter write this inside Regular Expression field:

^(?!s*$).+

这将最终强制您使用字符串参数 。

取决于您的使用大小写, IMHO < code> when {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}} 是目前解决这一问题的最佳办法。当参数设置不正确时,请跳过所有阶段,并对 steps{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{>}} 中要求的每个参数进行验证/通过/触发错误,或让命令失效。

示例:

pipeline {
    agent any

    parameters {
        string(name:  FOO , defaultValue:   , description:  FUBAR , trim: true)
    }

    stages {
        stage( Test ) {
            when {
                anyOf {
                    triggeredBy  BuildUpstreamCause 
                    expression { return params.FOO }
                }
            }

            steps {
                script {
                    if (params.FOO.length() < 8) {
                        error("Parameter FOO must be at least 8 chars")
                    }
                }

                echo  Hello World 
            }
        }
    }
}

当您需要在多个步骤上这样做时, 造成有点烦恼, 但至少您可以将其移动到一个函数 :

def stageWhen() {
    return (triggeredBy( BuildUpstreamCause ) || params.FOO)
}

def validateParameterFoo() {
    if (params.FOO.length() < 8) {
        error("Parameter FOO must be at least 8 chars")
    }
}

pipeline {
    agent any
    
    parameters {
        string(name:  FOO , defaultValue:   , description:  FUBAR , trim: true)
    }

    stages {
        stage( Test ) {
            when {
                expression { return stageWhen() }
            }

            steps {
                script {
                    validateParameterFoo()
                }

                echo  Hello World 
            }
        }
        
        stage( Test 2 ) {
            when {
                expression { return stageWhen() }
            }

            steps {
                script {
                    validateParameterFoo()
                }

                echo  Hello World 
            }
        }
    }
}




相关问题
Reuse agent without needing to declare it multiple times

I m trying to define a agent in a jenkinsfile once, but use it in multiple stages without needing to fully declare it for each stage. As can be seen, I am using a stage to figure out which docker ...

在Jenkins Templating引擎实施版本

我们正计划利用JTE(Jenkins Templating的发动机)在我们org建造管道。 有许多管道将使用这一模板。 可能的话,模板的任何改动都会中断......

Hudson gitorious plugin?

Has anyone integrated a local copy of gitorious with Hudson? Specifically, linking the sha1 on Hudson web page back to the gitorious web page.

Problem with Hudson-JIRA-Perforce integration

I m looking at using the Hudson JIRA plugin to post a JIRA comment on each build. The SCM I m using is perforce which is already linked to the JIRA tasks using perforce jobs. I can see that the JIRA ...

Two Hudson Masters on the same Windows Server

I want to setup two Hudson Masters on the same Hardware. This will make administering Hudson easier, since both servers are used by two different (and independent) teams. So far I tried to install ...

Using Hudson and build steps with multiple git repositories

I m trying out Hudson to replace our current Buildbot setup. I installed the git plugin. Our current setup is like: ssh://server:/repo/test_framework.git ssh://server:/repo/project_a.git Now, to ...