Jenkins Declarative Pipeline: How to read choice from input step?

LeoLuz :

I'm trying to access a variable from an input step using the declarative pipelines syntax but it seems not to be available via env or params. This is my stage definition:

stage('User Input') {
    steps {
        input message: 'User input required', ok: 'Release!',
            parameters: [choice(name: 'RELEASE_SCOPE', choices: 'patch\nminor\nmajor', description: 'What is the release scope?')]
        echo "env: ${env.RELEASE_SCOPE}"
        echo "params: ${params.RELEASE_SCOPE}"
    }
}

Both echo steps print null. I also tried to access the variable directly but I got the following error:

groovy.lang.MissingPropertyException: No such property: RELEASE_SCOPE for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)

What is the correct way to access this choice parameter?

Jon S :

Since you are using declarative pipelines we will need to do some tricks. Normally you save the return value from the input stage, like this

def returnValue = input message: 'Need some input', parameters: [string(defaultValue: '', description: '', name: 'Give me a value')]

However this is not allowed directly in declarative pipeline steps. Instead, what you need to do is wrap the input step in a script step and then propagate the value into approprierte place (env seems to work good, beware that the variable is exposed to the rest of the pipeline though).

pipeline {
    agent any
    stages {
        stage("foo") {
            steps {
                script {
                    env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
                            parameters: [choice(name: 'RELEASE_SCOPE', choices: 'patch\nminor\nmajor', description: 'What is the release scope?')]
                }
                echo "${env.RELEASE_SCOPE}"
            }
        }
    }
}

Note that if you have multiple parameters in the input step, then input will return a map and you need to use map references to get the entry that you want. From the snippet generator in Jenkins:

If just one parameter is listed, its value will become the value of the input step. If multiple parameters are listed, the return value will be a map keyed by the parameter names. If parameters are not requested, the step returns nothing if approved.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jenkins: How to use choice parameter in declarative pipeline?

Jenkins Declarative Pipeline - Dynamically Populate the Choices of an Input Step from the Output of a Command Run in the Workspace

Jenkins input on declarative pipeline

Jenkins Declarative Pipeline - How to add input step only if when condition is met

Conditional input step in declarative pipeline

How to autogenerate parameters in an input step from Jenkins pipeline?

Jenkins, how to run XmlParser / Read XML in Jenkins declarative pipeline

How to use choice parameter integer value as array in Jenkins Declarative pipeline file

How can I tag a project in git from a jenkins declarative pipeline

How to retrieve all lines from a text parameter in a Jenkins declarative pipeline?

Declarative jenkins pipeline do not abort on input

Creating a sequential step in a jenkins declarative pipeline preceding a parallel stage

Assigning variables in a parallel step using Declarative Pipeline steps in Jenkins

How to know which user answered a Jenkins-Pipeline input step?

How to lock multiple stages of declarative Jenkins pipeline?

How to change a Jenkins Declarative Pipeline environment variable?

How to add sidecar MySQL in declarative Jenkins pipeline?

Jenkins Declarative Pipeline: How to inject properties

How to loop parameter value in Jenkins Declarative pipeline

What exactly is "Declarative Pipeline" in Jenkins? How to switch from the previous "pipeline" concept?

How to populate Jenkins build parameter values from URL in Jenkins declarative pipeline

Jenkins parallel declarative pipeline

Can a Jenkins pipeline have an optional input step?

Jenkins Pipeline: "input" step blocks executor

Declarative Jenkins pipeline: how to remove branches from the Project without deleting them from svn?

Jenkins scripted pipeline or declarative pipeline

How do I use Docker's --cache-from build flag in a declarative Jenkins pipeline?

How to replace Artifactory File Spec "Spec Vars" from Jenkins Declarative Pipeline

Failed to get the output of jenkins pipeline sh step result inside Declarative Pipeline

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive