mc88
mc88

Reputation: 163

How to use Validating String Parameter Plugin in the Jenkins declarative pipeline code?

I'm curious as to if it is possible to define the String Validator Plugin in a Jenkins declarative pipeline code? I already have a working setup defined via job UI, but my intention is to put everything in the pipeline defined as:

string(name='', ......). 

Unfortunately, all examples on the web are explaining how to set up the validation in the UI, which I already have. Or is it one of those plugins that is not supported in a pipeline model?

Upvotes: 8

Views: 8666

Answers (2)

JimShapedCoding
JimShapedCoding

Reputation: 917

I am not sure why, but when I wrapped each of my arguments into a named key, I was able to bypass this error I received:

So this will give you javaposse.jobdsl.dsl.helpers.BuildParametersContext.validatingString() is applicable for argument types Error:

            validatingString (
                "EMAIL_VALIDATED"
                'defaultEmail'
                'someregex',
                'somevalidationfailuremessage',
                'Use your email' 
            )

However, this worked:

            validatingString {
                name("EMAIL_VALIDATED")
                defaultValue('defaultEmail')
                regex('someregex')
                failedValidationMessage('somevalidationfailuremessage')
                description('Use your email') 
            }

Upvotes: 1

Szymon Stepniak
Szymon Stepniak

Reputation: 42184

This plugin can be used as a validatingString parameter in the declarative pipeline code.

pipeline {
    agent any

    parameters {
        validatingString(name: "test", defaultValue: "", regex: /^abc-[0-9]+$/, failedValidationMessage: "Validation failed!", description: "ABC")
    }

    stages {
        stage("Test") {
            steps {
                echo "${params.test}"
            }
        }
    }
}

Keep in mind, that the first time you will run your pipeline after adding this code, the parameter won't show up - it will be added during the first run of the pipeline. After that you will see the parameter in the pipeline UI:

enter image description here

And when you run the parameterized pipeline, the validation will be applied:

enter image description here

Upvotes: 10

Related Questions