Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 572
mc88 Avatar asked Oct 24 '19 08:10

mc88


People also ask

How do I add a choice parameter in Jenkins pipeline?

We will first create a sample pipeline project for this. Login to Jenkins, click on New Item, in the next page provide the name of your choice for your pipeline and select the Pipeline and click on Ok. On the configure job page select the This project is parameterized checkbox in the general tab.

How are parameters defined in scripted pipeline?

Configuring parameters with Scripted Pipeline is done with the properties step, which can be found in the Snippet Generator. If you configured your pipeline to accept parameters using the Build with Parameters option, those parameters are accessible as members of the params variable.


1 Answers

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

like image 153
Szymon Stepniak Avatar answered Sep 20 '22 06:09

Szymon Stepniak