Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use options in a Jenkins scripted pipeline?

I'm using a Jenkinsfile with a Scripted Pipeline. I would like to modify the buildDiscarder option for this pipeline, but I can't get it working. https://jenkins.io/doc/book/pipeline/syntax/#options

How to define pipeline options in a scripted pipeline?

like image 350
Captainju Avatar asked Dec 12 '17 17:12

Captainju


1 Answers

You can't use options as that is a specific piece of the Declarative Pipeline feature set.

For the multibranch-style builds you should use the properties step. The Snippet Generator can help you find the correct syntax to help build out pieces of your Jenkinsfile. For example, in this case you would use the buildDiscarder symbol that resolves to that configuration option.

properties(
    [
        buildDiscarder(
            logRotator(
                daysToKeepStr: '7',
                numToKeepStr: '25'
            )
        )
    ]
)

The properties step will be evaluated when your Jenkinsfile is executed, so that is when the configuration will be effective. I tend to put the properties at the top as a "best practice". See the full options on your instance's Snippet Generator for the other things you can keep (such as artifacts).

like image 179
mkobit Avatar answered Sep 19 '22 11:09

mkobit