HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Jenkins - Scripted Pipeline - Input String

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
jenkinsinputstringpipelinescripted

Problem

I am struggling to set up an input step in a scripted Jenkins pipeline that will let me as the user to input a string.

Scenario:
I am building a Docker image. I want to ask the user what tags to tag the image with once it's built. I want the user to supply a comma separated list like so:
latest,0.0.4,some-tag-name

All I have right now is an input asking the user if the core image should be built:

def buildCoreImage = input(
    message: 'Build Core Image?',
    ok: 'Yes', 
    parameters: [
      booleanParam(defaultValue: true, description: 'Push the button to build core image.',name: 'Yes?')
    ]
  )

  echo "Build core?:" + buildCoreImage


So in theory it should be as easy as changing the booleanParam to a stringParam, BUT the documentation does not have it:
Jenkins - Pipeline: Input Step

Thanks for your time!

Solution

After some more research I found the answer:

def coreImageTags = input(
    id: 'coreImageTags', message: 'Enter a comma separated list of additional tags for the image (0.0.1,some-tagname,etc):?', 
    parameters: [
      [$class: 'StringParameterDefinition', defaultValue: 'None', description: 'List of tags', name: 'coreImageTagsList'],
    ]
  )

  echo ("Image tags: "+coreImageTags)


Important note

It is stated in the documentation but I want to point it out specifically: If you copy/paste my solution here, coreImageTags will contain a string directly. If you do multiple parameters like so:

parameters: [
      [$class: 'StringParameterDefinition', defaultValue: 'None', description: 'List of tags', name: 'coreImageTagsList'],
      [$class: 'StringParameterDefinition', defaultValue: 'None', description: 'Something else', name: 'somethingElse'],
    ]


You will not get a single values but an array of values:

echo ("Image tags: "+coreImageTags['coreImageTagsList'])
  echo ("Image tags: "+coreImageTags['somethingElse'])


The following posts will help:

stackoverflow.com read-interactive-input-in-jenkins-pipeline-to-a-variable

CloudBees - Pipeline-How-to-manage-user-inputs

A list of classes you can use in this way and what they do is in the Jenkins Input-Step documentation:
Pipeline - Input Step

Code Snippets

def coreImageTags = input(
    id: 'coreImageTags', message: 'Enter a comma separated list of additional tags for the image (0.0.1,some-tagname,etc):?', 
    parameters: [
      [$class: 'StringParameterDefinition', defaultValue: 'None', description: 'List of tags', name: 'coreImageTagsList'],
    ]
  )

  echo ("Image tags: "+coreImageTags)
parameters: [
      [$class: 'StringParameterDefinition', defaultValue: 'None', description: 'List of tags', name: 'coreImageTagsList'],
      [$class: 'StringParameterDefinition', defaultValue: 'None', description: 'Something else', name: 'somethingElse'],
    ]
echo ("Image tags: "+coreImageTags['coreImageTagsList'])
  echo ("Image tags: "+coreImageTags['somethingElse'])

Context

StackExchange DevOps Q#6533, answer score: 2

Revisions (0)

No revisions yet.