Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

jenkins pipeline - parameter from function - Required context class hudson.FilePath is missing

Ask Question
@NonCPS
def getLastRelease() {
    def RES = sh(script: '''cat version''', returnStdout: true).trim()
    return RES
pipeline{
    parameters {
            choice(name: 'RELEASE_VERSION', choices: '${getLastRelease()}', description: 'desc')

But for some reason it does not work - if i try:

'${getLastRelease()}'

I am getting error:

durable-73075a87/script.sh: line 1: ${getLastRelease()}: bad substitution

if i use:

"${getLastRelease()}"

I am getting error:

[Pipeline] Start of Pipeline [Pipeline] sh [Pipeline] End of Pipeline org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing Perhaps you forgot to surround the code with a step that provides this, such as: node, dockerNode

I am not completely sure if you can or want to provide dynamic choices for user input parameters. Default values sure, but choices not so much. – Matt Schuchard Oct 8, 2019 at 14:19 I want to get the release version, which is actually generated by bash script, and i want it to be default option. The script is correct and I am able to echo this value inside the pipeline RES = getLastRelease() echo "${RES}" But for some reason it does not work for parameter – Wojtas.Zet Oct 8, 2019 at 14:24
  • Remove the annotation @NonCPS, since NonCPS functions should not use Pipeline steps internally.
  • Since you execute shell scripts, wrap the expressions in your function in a node {...} block.
  • Simply invoke the function getLastRelease() inside the choice the parameter without the quotes or the curly braces.
  • Working sample:

    def getLastRelease() {
        node {
            def RES = sh (script: 'cat version', returnStdout: true).trim()
            return RES
    pipeline {
        agent any
        parameters {
            choice(name: 'RELEASE_VERSION', choices: [getLastRelease(), <more choices, ...>], description: 'desc')
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.