Ask Your Question
2

How can I populate the Active Choices Parameter when running a shell script from a Groovy Script in Jenkins?

asked 2023-05-06 14:30:17 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-06 14:56:02 +0000

nofretete gravatar image

You can use the following code snippet to populate the Active Choices Parameter when running a shell script from a Groovy Script in Jenkins:

job('my-job') {
  parameters {
    activeChoiceParam('paramName') {
      groovyScript {
        """
        def shellScriptOutput = sh(script: 'my-shell-script.sh', returnStdout: true).trim()
        return shellScriptOutput.tokenize('\\n')
        """
      }
    }
  }
}

In this code snippet, the activeChoiceParam parameter is used to define an Active Choices Parameter with the name paramName. The groovyScript block is used to execute a shell script called my-shell-script.sh and return its output as a list of choices for the Active Choices Parameter. The tokenize method is used to split the output by newline characters and return the resulting list of choices.

Note that this code snippet assumes that the sh method is available in the current context and that the shell script returns a list of choices separated by newline characters. If your shell script returns a different format or you need to customize the behavior of the sh method, you may need to modify this code accordingly.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-06 14:30:17 +0000

Seen: 10 times

Last updated: May 06 '23