0

If I had a Git repository full of Job DSL groovy scripts and a typical seed job e.g.:

job('seed') {
    //... scm, triggers etc.
    steps {
        dsl {
            external 'jobs/**/*.groovy'
        }
    }
    //... more config etc.
}

what happens if just one of the job dsl scripts throws an exception for some reason, for example:

job('deliberate-fail') {
    throw new Exception("Arrrgggghhh")
}

Is it possible to handle this exception in the seed job or will the whole seed job fail?

If all but one would work - is it possible for the seed job to record an UNSTABLE result rather than FAILURE?

I don't really want one bad apple to spoil the bunch.

Mark McLaren
  • 11,470
  • 2
  • 48
  • 79

1 Answers1

0

Based on Opal's suggestion to use a try-catch, I modifed the job to capture the exception and print an error to the console.

job('deliberate-fail') {
    try {
        throw new Exception("Arrrgggghhh")
    } catch (Exception ex){
        println("deliberate-fail job is [UNSTABLE]")
    }
}

As I am currently using the Job DSL plugin (and not a Jenkins Pipeline script), I don't think Opal's suggestion to use "currentBuild.result = 'UNSTABLE'" was available to me. After a little digging I found I could use the Text-Finder plugin to search the console for the "[UNSTABLE]" error and change the seed job state accordingly.

job('seed-job') {
     steps {
        dsl {
            external '**/*_jobdsl.groovy'
        }
    }
    publishers {
        textFinder(/[UNSTABLE]/, '', true, false, true)
    }    
}

A bit convoluted but it seems to work!

Mark McLaren
  • 11,470
  • 2
  • 48
  • 79