0

I have a Jenkins setup where some additional stages (like sanitizers for example) are executed only on the master branch.

It's done this way since the rebuilds and reruns of the unit tests on these stages takes too long for us to execute it on all feature branches.

However there are some developers ignoring these failures. So ideally I would like to have these stages execute on all branches if they're failing on master. That way there would be a forcing function to keep the master branch green, without too much additional load on the servers, and too long delays when doing PRs.

Is something like this possible?

CodeMonkey
  • 4,067
  • 1
  • 31
  • 43
  • You could get pipeline results from the Jenkins API or CLI, or if you're hooked up to Github or similar, from that API, for the master builds. Then that could be used to decide if you want to skip stages or not, maybe in a `when` on the stage. I had a similar problem but ended up doing the opposite: enforcing all checks on PRs, enforcing passing tests for merges to master, and then removing all the slow stuff from the master build. That way anything going into master is guaranteed to be good, and deploys were faster. – Zac Anger Jan 19 '21 at 02:56
  • @ZacAnger thank you, I still think building for ~1h on each PR is very slow in terms of feedback for the normal case. We have BitBucket, which only gets very rough information on whether Jenkins has failed or not. Can you tell me where I can look in the Jenkins API? – CodeMonkey Jan 19 '21 at 07:35
  • 1
    Oh yeah, an hour is pretty long... docs on the API are [here](https://www.jenkins.io/doc/book/using/remote-access-api/) and there are some example scripts for checking build results on [this question](https://stackoverflow.com/questions/28311030/check-jenkins-job-status-after-triggering-a-build-remotely). – Zac Anger Jan 19 '21 at 16:21
  • @ZacAnger Thank you, I'll read it and check if I can find something. – CodeMonkey Jan 20 '21 at 09:59
  • @ZacAnger I found an 80% solution without the remote API, using the groovy API directly. – CodeMonkey Jan 20 '21 at 12:09

1 Answers1

0

With some sleuthing I found that you can use the groovy API to check for failures of a specific build.

def isMasterFailing = jenkins.model.Jenkins.instance.getItem("MyProject").getItem("master").lastCompletedBuild.result == Result.FAILURE

By using lastCompletedBuild you don't get any running builds that may not have a result yet. And comparing to Result.FAILURE you don't get results that are e.g. timeouts or aborted builds which may or may not mean that there's a systematic problem. Other results are NOT_BUILT, SUCCESS and UNSTABLE. Maybe you want to check something else here.

Now you can use isMasterFailing to run the optional stages on your feature branch (e.g. sanitizers).

Ideally I'd still like to know which stage the build failed on, since then I could decide whether that's a stage that's not done one my feature branches, but this will do for now.

CodeMonkey
  • 4,067
  • 1
  • 31
  • 43