5

I am using the Job DSL Jenkins plugin, and I have got a problem regarding the trigger. It is deprecated and when I update the code, then the deprecation warning is still shown.

Here the code before:

protected def job
 void nightly(String schedule='H 0 * * *') {
        job.triggers {
            cron(schedule)
        }
    }

Then the update according to: https://github.com/jenkinsci/job-dsl-plugin/wiki/Migration

void nightly(String schedule='H 0 * * *') {
        properties {
            pipelineTriggers {
                job.triggers {
                    cron(schedule)
                }
            }
        }
    }

There is still a warning: Warning: (JobBuilder.groovy, line 100) triggers is deprecated

What am I doing wrong? Is the properties keyword wrong or should it be job.properties?

thanks in advance

DRMTZ
  • 63
  • 1
  • 3

1 Answers1

6

job basically represents project block of a job XML configuration file and its methods are converted to nested XML elements.

Your initial code

void nightly(String schedule = 'H 0 * * *') {
  job.triggers {
    cron(schedule)
  }
}

renders this part:

<triggers>
    <hudson.triggers.TimerTrigger>
        <spec>H 4 * * *</spec>
    </hudson.triggers.TimerTrigger>
</triggers>

Your updated code does effectively the same thing, because you are calling triggers method of job exactly like before the update. Another problem is that cron method specification for pipelineTriggers is different, so the correct code is:

void nightly(String schedule = 'H 0 * * *') {
  job.properties {
    pipelineTriggers {
      triggers {
        cron {
          spec(schedule)
        }
      }
    }
  }
}

You can view available Jenkins DSL methods at https://your.jenkins.domain/plugin/job-dsl/api-viewer/index.html

dizeee
  • 400
  • 2
  • 7
  • Thanks that helped a lot, how does the command looks like if I have 'scm' instead of 'cron' in the trigger? thanks in advance – DRMTZ Aug 01 '20 at 21:19
  • 1
    @DRMTZ See https://YOUR.JENKINS.DOMAIN/plugin/job-dsl/api-viewer/index.html#method/javaposse.jobdsl.dsl.helpers.triggers.TriggerContext.scm for `scm` method usage in `triggers` context. Jenkins DSL can vary depending on the particular Jenkins and installed plugin versions, so it's always best to check with the DSL API viewer on your Jenkins server. – dizeee Aug 03 '20 at 15:44
  • thanks, that is what I´ve done so far, but I get still a compile error. This is from DSL API Viewer: `scm(String cronString) { // Ignore changes notified by SCM post-commit hooks. ignorePostCommitHooks(boolean ignorePostCommitHooks = true) }` Thats my current implementation, but it still throws a compile error, that should be correct or? `void continuous(String schedule = 'H/5 * * * *') { job.properties { pipelineTriggers { triggers { scm(schedule) } } } }` – DRMTZ Aug 08 '20 at 07:17
  • 1
    I don't think you can use `scm` method in `pipelineTriggers`. This page should show the available methods: `/plugin/job-dsl/api-viewer/index.html#method/javaposse.jobdsl.dsl.helpers.properties.PropertiesContext.pipelineTriggers` – dizeee Aug 10 '20 at 14:40