jenkinsmultibranch-pipeline

Jenkins parameter does not exist on first run (multibranch)


I'm having a problem with a Jenkins multibranch pipeline, which is parameterized. The parameters are all declared in the Jenkinsfile.

The problem is that these parameters do not exist on the very first run of the job. So, the very first execution will fail with groovy.lang.MissingPropertyException. Any subsequent run is now aware of the parameters and will not fail.

Since this is a multibranch pipeline this happens for every new pull request or tracked branch. Is there any workaround to avoid this problem?

I tried setting the parameters in the UI as well, however there is no option on the pipeline configuration page to set parameters. Probably because this is a multibranch pipeline?

Cheers


Solution

  • This is a known issue with Parameters in Pipelines. To know which parameters are needed, Jenkins needs to execute the Jenkinsfile once. For example the parameters in the GUI are not available until after the first run of the pipeline.

    To prevent errors, you could specify sensible default values like this:

    pipeline {
        agent any
        parameters {
            string(name: 'Greeting', defaultValue: 'Hello', description: 'How should I greet the world?')
        }
        stages {
            stage('Example') {
                steps {
                    echo "${params.Greeting} World!"
                }
            }
        }
    }
    
    

    Update: further clarification in the comments on this answer show that the defaults where properly set. The issue that leads to the same error message is that the parameters were referred by ${foo} instead of ${params.foo}.