Captainju
Captainju

Reputation: 317

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?

Upvotes: 16

Views: 15476

Answers (1)

mkobit
mkobit

Reputation: 47319

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

Upvotes: 27

Related Questions