1

I want to create an input step that prompts the user to select a git tag. To do this, I want to populate a dropdown box with the values returned by git tag.

Here is my current pipeline:

pipeline {
    agent any
    stages {
        stage('My Stage') {
            input {
                message "Select a git tag"
                parameters {
                    choice(name: "git_tag", choices: TAGS_HERE, description: "Git tag")
                }
            }
            steps {
                echo "The selected tag is: ${git_tag}"
            }
        }
    }
}

I would like TAGS_HERE to be a variable or method that contains the output given by the git tags command.

So far I have tried:

  • Setting the tags to an environment variable in a previous step - doesn't work because these variables are for some reason not accessible in the input block
  • Calling a seperate groovy method that runs the command and returns the output - doesn't work because the workspace is lost and the commands are all run in /

I have searched extensively for a solution but all the examples I can find avoid these two pitfalls by either exclusively using scripted pipeline steps or by using commands that are not workspace dependant.

JShorthouse
  • 1,450
  • 13
  • 28
  • Possible duplicate of [Jenkins dynamic declarative pipeline parameters](https://stackoverflow.com/questions/44570163/jenkins-dynamic-declarative-pipeline-parameters) – hakamairi Mar 07 '19 at 10:43
  • All of those answers seem to be use scripted pipelines (or using declarative pipelines but wrapping everything in a script block), I'm hoping that there's a way to achieve this while keeping the input step declarative. – JShorthouse Mar 07 '19 at 10:55
  • You are right, let me revert the flag. Also it's all about pipelines parameters not the input step. – hakamairi Mar 07 '19 at 12:07

2 Answers2

5

By improving the answer of @hakamairi, you can do something like this :

pipeline {
    agent any
    stages {
        stage('My Stage') {
            steps {
                script {
                    def GIT_TAGS = sh (script: 'git tag -l', returnStdout:true).trim()
                    inputResult = input(
                        message: "Select a git tag",
                        parameters: [choice(name: "git_tag", choices: "${GIT_TAGS}", description: "Git tag")]
                    )
                }
            }
        }
        stage('My other Stage'){
            steps{
                echo "The selected tag is: ${inputResult}"
            }
        }
    }
}
SmartTom
  • 691
  • 7
  • 14
  • Thank you, but this still requires that everything is enclosed inside a script tag, which is not ideally what I wanted, but if there's no other way to achieve this perhaps this will have to do. With this solution is $inputResult accessible from other declarative steps / stages? I remember having trouble with passing variables out of script steps before. – JShorthouse Mar 07 '19 at 16:51
  • I edited my answer so that `inputResult` is accessible from other steps and stages. Concerning the fact that all is enclosed inside a script tag, I think that this is the only way if you want to dynamically populate the choices of the input. Full declarative pipeline input does not seem to accept something else than `'Choice1\nChoice2\nChoice3'` or `['Choice1', 'Choice2', 'Choice3']`. – SmartTom Mar 08 '19 at 09:48
  • 1
    Thank you, `inputResult = input(...` was the key part I was missing when trying to access the variables in another stage. When using an input step outside of a script block the choice names ( e.g. `git_tag` ) become variables that are set to the input values, I wasn't aware that it was different for scripted steps. – JShorthouse Mar 08 '19 at 10:05
0

Maybe you are not in the node (in the old pipeline script way), you could try this. Possibly the script is not needed.

pipeline {
    agent any
    stages {
        stage('My Stage') {
            steps {
                def inputResult = input {
                    message "Select a git tag"
                    parameters {
                        choice(name: "git_tag", choices: getTags(), description: "Git tag")
                    }
                }
                echo "The selected tag is: ${inputResult.git_tag}"
            }
        }
    }
}

getTags should return choices separated by new line.

hakamairi
  • 4,464
  • 4
  • 30
  • 53
  • Thanks for your answer, but take a look at the second bullet point in my question. You can call functions from inside the parameters block fine, but the called function will be outside of the pipeline block and so the git command will not be executed inside the workspace, but in the root directory. I tried to get around this by passing the workspace as a parameter to the function (so I could cd to it and then run the git command) but then I ran into the issue in bullet point 1 - no environment variables are accessible in the parameter block so I couldn't even access env.WORKSPACE to pass it. – JShorthouse Mar 07 '19 at 13:48
  • Did you try my code with and without the script part? – hakamairi Mar 07 '19 at 20:48
  • I updated my answer, in general just moving it to steps should do the trick, also it won't set any variables, you have to read the return value. – hakamairi Mar 07 '19 at 21:04