Ondřej Kmoch
Ondřej Kmoch

Reputation: 106

How to avoid Jenkins Job DSL to overwrite triggers in pipelines

Let's have a seed job in Jenkins Job DSL Groovy which generates a pipeline this way:

pipeline {
    agent any
    stages {
        stage("DSL") {
            steps {
                jobDsl scriptText: """pipelineJob("test-pipeline-generated") {
                    definition {
                        cps {
                            script 'pipeline { agent(any); triggers{ cron("H/2 * * * *") }; stages { stage("Some") { steps { echo "Working ..." } } } }'
                        }
                    }
                }
                """
            }
        }
    }
}

It generates a pipeline job which has to be triggered manually for the first time to process triggers definition and everything works fine. However, when the seed job is triggered again it overwrites the pipeline job definition and triggers are gone again. This means that seed job disables all such jobs.

Does anybody know if there is any solution for it?

We would like to keep triggers there and if there are changes in the pipeline it will be processed after the first run. We would like to not specify triggers in job DSL because users are used to specify them in the pipeline definition.

Upvotes: 4

Views: 1204

Answers (2)

thomas.st
thomas.st

Reputation: 413

There is a switch for not regenerating existing jobs/views:

Jenkins: Action for existing jobs and views

DSL API:

ignoreExisting()

Upvotes: 0

yorammi
yorammi

Reputation: 6458

If you put the triggers inside the template pipeline (as well as in the seed job), it will remain for the next runs as well.

Upvotes: -1

Related Questions