In Groovy is there a way to execute a `build` step only if there had been changes since the last build?

79 Views Asked by At

I need a way to run a job B from or before a job A, but I want to avoid unnecessary builds (i.e. without changes since the last run).

I'm currently using the build step to trigger job B from job A programmatically. And I know the pollSCM trigger which schedules a job to run every time and only if there had been changes for a given scm instance - taking includedRegions into account, i.e. it will only run if there had been changes for a given set of files/directories.

Is there a way to let build run conditionally the same way as pollSCM does, i.e. letting it run only if there are changes reported for the scm instance of the job I need to run?

Put differently job B (or it's artifacts) are a dependency for job A and I need it to be up to date (in terms of it's scm object), while at the same time avoiding redundant builds.

So the optimal solution would somehow look like

build(job: "JobB", if: changeSetNotEmpty())

A manual approach could compare the commitIds of Jenkins.instance.getItemByFullName("JobB").lastCompletedBuild and the most recent on on that repo/branch but that wouldn't take includedRegions into account.

A better manual approach would check if changeSet() for a build that has not yet been triggered is empty. But is that even possible?

This problem seems to be quite common to me but even ChatGPT was only confused :)

1

There are 1 best solutions below

3
ycr On

Are you looking for something like below?

if(currentBuild.changeSets.size() > 0) {
   build(job: "JobB")
}