I am trying to launch all jobs starting with a preset string as a pipeline in Jenkins 2.7. So far I managed to write this groovy code:

node {
    jobs = hudson.model.Hudson.instance.getAllItems(FreeStyleProject)
for (job in jobs) {
      // here was an "if" statement: if (job.getFullName() =~ /my regex/) {
      stage job.getFullName()
      build job: job.getFullName(), propagate: false

However on every launch I get java.io.NotSerializableException, and only one job matching (out of two at this time) is being executed.

What am I doing wrong? :(


Here is an example, how I would do that:

import hudson.model.*;

// get all jobs which exists    
jobs = Hudson.instance.getAllItems(FreeStyleProject);

// iterate through the jobs
for (j in jobs) {

  // define a pattern, which jobs I do not want to run
  def pattern = 'trunk';
  def m = j.getName() =~ pattern;

  // if pattern does not match, then run the job
  if (!m) {
    // first check, if job is buildable
    if (j instanceof BuildableItem) {
      // run that job

I commented my code, so this should help you.

  • Thank you, this way it actually triggers said jobs, however using obj.scheduleBuild(), build time and state is not returned to the pipeline view. It seems I need to use pipeline's build function, but it throws me an exception... – J. Doe Jul 11 '16 at 8:32

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.