Jenkins parameter does not exist on first run (multibranch)
Asked Answered
C

1

7

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

Contamination answered 27/8, 2020 at 8:44 Comment(0)
F
10

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}.

Faille answered 27/8, 2020 at 9:0 Comment(3)
Although this was not the solution in my case, as I already did define default values, it still lead me to the actual solution to my problem. I was not aware that parameters should be accessed through the params object and accessed them directly. I'll mark it as answer anyway. Thank you!Contamination
@xxSwordy may I know what was your solution eventually?Intelligentsia
@Intelligentsia You have to use ${params.Foo} instead of ${Foo}. It's hidden in the accepted answer.Contamination

© 2022 - 2024 — McMap. All rights reserved.