2

I wanted to run a jenkins job by accepting a date field (in format YYYY-MM-DD) from user. I found a link where user can enter a string parameter:

job('example') {
    parameters {
        stringParam('myParameterName', 'my default stringParam value', 'my description')
    }
}

But in string param user can enter any thing. So how do I force user to enter a date field like a calender field and select date from the calender ?

Liam
  • 27,717
  • 28
  • 128
  • 190
Anupam K
  • 309
  • 1
  • 4
  • 17

2 Answers2

2

There seems to be no plugin which provides a date chooser.

But you can use the Validating String Parameter Plugin, which can use a regular expression to validate a string parameter. See Regex to validate date format dd/mm/yyyy for regular expressions matching date values.

The Job DSL plugin has no built-in support for the Validating String Parameter Plugin, but you can use a Configure Block to add the relevant config XML.

job('example') {
  configure { project ->
    project / 'properties' / 'hudson.model.ParametersDefinitionProperty' / parameterDefinitions << 'hudson.plugins.validating__string__parameter.ValidatingStringParameterDefinition' {
      name('DATE')
      description('date in YYYY-MM-DD format')
      defaultValue('2016-03-01')
      regex(/\d\d\d\d-\d\d-\d\d/)
      failedValidationMessage('Enter a YYYY-MM-DD date value!')
    }
  }
}
Community
  • 1
  • 1
daspilker
  • 8,154
  • 1
  • 35
  • 49
  • how to validate the date ? Someone can enter 1234-56-78. And it will be valid date ;) – Anupam K Mar 18 '16 at 17:20
  • @AnupamK You can do the semantic validation in a build or prebuild step by writing a little shell script. The mentioned plugin doesn't support a semantic date validation. – Patrick Pötz Mar 11 '19 at 11:39
0

I came across this same issue today and this is how I solved it.

Using: Active Choice Plugin

In a Declarative Pipeline I added the following parameter

 [$class: 'DynamicReferenceParameter',
     choiceType: 'ET_FORMATTED_HIDDEN_HTML',
     description: '',
     name: 'YOUR_PARAMETER_NAME',
     omitValueField: true,
     referencedParameters: '',
     script: [
         $class: 'GroovyScript',
         fallbackScript: [classpath: [], sandbox: false, script: ''],
             script: [
                 classpath: [], 
                 sandbox: false, 
                 script: 'return "<input type=\\"date\\" name=\\"value\\" value=\\"\\" />"'
             ]
         ]
     ]

Basically it adds an HTML Input Element of type Date, which you can then catch the value during the run.

pipeline {
agent { label "master" }

stages {
    stage('Output Date') {
        steps {
            script {
                println params.YOUR_PARAMETER_NAME
            }
        }
    }
}

}

Here's a picture of how it looks on Chrome: HTML Date parameter

Note: You can also use it to add parameters of type TextArea and so on.