Skip to content
Shakthivel Arumugam

Stopping a Jenkins pipeline from rebuilding itself

In a GitOps pipeline that commits its own image tag, polling cannot filter by path. Here are the two rebuild loops I hit and the comparison that fixed them.

22 November 2026, 3 min read

  • Jenkins
  • GitOps
  • CI/CD
  • Argo CD

My Java CI/CD platform has a Jenkins pipeline that builds an image and then commits the new tag to deploy/ in the same repository, where Argo CD picks it up. That design has an obvious trap: the pipeline's own commit is a new commit on main, so Jenkins can see it and build again. I hit this twice, in two different forms.

Why path filters did not work

A laptop kind cluster has no inbound address, so Jenkins cannot receive webhooks and polls about every five minutes. Polling uses git ls-remote, which only reports the branch head. It cannot tell which files changed, so my "ignore deploy/" rule did nothing, and every tag commit triggered a full build.

First fix, and the loop it created

I moved the decision into the Jenkinsfile. The first stage lists the files changed since a base commit and skips the build unless something under app/ or the Jenkinsfile itself changed. A skipped build is marked NOT_BUILT.

The first version used the last successful build's commit as the base. That looks reasonable until a release fails:

  1. A change under app/ is built, and the release fails at the load test, after the pipeline has already committed the new tag to deploy/.
  2. That tag commit is compared against the last green build, which is older than the failed change.
  3. The diff still contains the failed app/ change, so the guard decides a build is needed and the same code runs again.

Each failed release retriggered itself through its own tag commit until something passed.

The fix

The base is now the previous build's commit, whatever its result:

def base = env.GIT_PREVIOUS_COMMIT ?: 'HEAD~1'
def changed = sh(returnStdout: true, script: "git diff --name-only ${base} HEAD || true").trim()
def relevant = changed.readLines().any { it.startsWith('app/') || it == 'Jenkinsfile' }
env.SKIP_CI = (!params.FORCE_BUILD && changed && !relevant) ? 'true' : 'false'

Each commit is now judged once, on what it changed. A failed commit is not re-examined when the next commit arrives, and the pipeline's own tag commit touches only deploy/, so it is skipped.

Two details matter:

  • An empty diff means build. It happens on a manual re-run of a commit that was already built, and refusing to build would surprise whoever clicked the button.
  • FORCE_BUILD overrides the guard. It exists for the cases where the diff is not the reason to build, such as warming caches.

What I would still change

This is a workaround for polling. With a webhook, the provider can send the changed paths, and a path filter at the trigger is simpler than a guard stage. The guard also assumes one branch and no concurrent builds, which the pipeline enforces with disableConcurrentBuilds(). I found out that this setting is dropped when a configuration reload recreates the job, until a run re-applies it, and I documented that in the runbook.

Check that it worked

Manifest-only commits, including the pipeline's own tag commits, now show up in the build history as grey NOT_BUILT entries. The Jenkins stage view in the project README shows them next to the red builds.