Jenkins: how to make parameters required in a parameterized build?
Asked Answered
S

6

34

In Jenkins is there a plugin for parameterized builds to make the parameters required? The fields under the standard "This build is parameterized" option do not seem to provide that.

Clarification: by "required" I mean that the build will not execute until the field is populated with a value. This would obviously preclude automated triggers.

Spoof answered 24/5, 2012 at 17:26 Comment(0)
T
16

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

Tally answered 24/5, 2012 at 17:40 Comment(1)
Actually, that plugin only shows a failure message in the parameter form if the parameter does not pass validation, but you can still trigger a build with invalid parameters.Coulee
S
22

The accepted answer is no longer valid.

There was a plugin that did that but is no longer maintained.

There's an open bug to support it.

In the mean time what you can do is check if your parameter is present and if not throw an error like:

if (!params.SomeParam) {
    error("Build failed because of this and that..")
}
Shortsighted answered 19/2, 2019 at 8:54 Comment(2)
this is indeed a good comment, that validation script, where would one place it?Bui
@MiguelCosta creating a step which the first thing it does is that validation. otherwise it will not continue.Twain
O
20

Couldn't comment to answer Miguel's question, so answering here:

To fail a build if a parameter is not set, one could do something like this:

stage('Checkout') 
    {
        steps
        {
            checkout scm
            script 
            {
                if (params.myParam == '') { // and/or whatever condition you want
                    currentBuild.result = 'ABORTED'
                    error('myParam not set')
                }
            }
        }
    }
Objurgate answered 18/2, 2020 at 7:31 Comment(0)
T
16

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

Tally answered 24/5, 2012 at 17:40 Comment(1)
Actually, that plugin only shows a failure message in the parameter form if the parameter does not pass validation, but you can still trigger a build with invalid parameters.Coulee
M
2

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*$).+

This will finally make your string parameter mandatory.

Mastitis answered 6/1, 2022 at 20:44 Comment(0)
S
0

Depending on your use-case then IMHO when {} is currently the best solution to this problem. Just skip all stages when the parameters aren't set correctly and do the validation/aborting/triggering error of each parameter required within steps{} then or just let the command there failing.

Example:

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'
            }
        }
    }
}

It's of cause a bit annoying when you need to do this on multiple steps, but at least you can move it into a function:

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'
            }
        }
    }
}
Sulfite answered 6/11, 2023 at 11:56 Comment(0)
J
-1

I tried to find a solution but ended up with adding some error checking of inputs in the build script and just throw an error at the very start of the script which will then stop the build process and show a nice error of your choosing in the build log. I think this might be why nobody really needs it much since: https://wiki.jenkins.io/display/JENKINS/Validating+String+Parameter+Plugin

doesnt work anyways and they still havn't fixed it in 4 years haha

Jd answered 9/7 at 14:5 Comment(3)
Mootje, a link to a solution is welcome, but please ensure your answer is useful without it: You need to provide at least a technical summary of how the problem is solved, so that it can be reproduced even without the link. It is not enough to advertise what it achieves. Also please add context around the link so your fellow users will have some idea what it is and why it is there. Answers that are little more than a link may be deleted.Hardan
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Blackwood
Excuse me? if you actually read the whole ticket youll see that im pasting the same link as the first answer did. I gave a good description of what I did. Put the error checking in your build script, what more do you want?? Everyone uses different languages for the buildscript so an example wouldnt make much sense. Can you actually read the whole ticket before writing rubbish?Jd

© 2022 - 2024 — McMap. All rights reserved.