blog

12021-08-23

SEAN K.H. LIAO

jenkins job dsl

I was browsing through our central jenkins jobs repo at work (which uses the Job DSL plugin) and noticed that all the git jobs were triggered by:

job('something--build-master') {
  triggers {
    scm('H/5 * * * *')
  }
}

Which i thought was... not very efficient. Thankfully the fix was relatively easy since we're using github:

job('something--build-master') {
  triggers {
    githubPush()
  }
}

as well as registering a webhook for push events in github for: (note this is different from the webhook for pull requests you may also be using)

https://jenkins.example.com/github-webhook/

freestyle jobs

Job DSL is all nice and good, but we also have a "Seed Job", which pulls in our git repo of Job DSL jobs. I also wanted this to trigger on push:

import com.cloudbees.jenkins.* // has the definition for GitHubPushTrigger

def job = new FreeStyleProject(Jenkins.getinstance(), "Seed-Job");
job.addTrigger(new GitHubPushTrigger());

note on job definitions

There are... many ways to write jenkins jobs, but if you think something doesn't make sense, just remember that everything compiles down to an XML document describing the job. Looking at these output XML files, it should be easy to reverse engineer what you need to write in whatever job syntax you happen to be stuck with.