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?
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.
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.
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:
And when you run the parameterized pipeline, the validation will be applied:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With