I am using multibranch pipeline with GHES and push support. When a branch is created and push does not have a commit, the branch is triggering. Since I am using GitHub checks API the result is getting overridden by the branch job as commit is same. Is there a way to avoid build when a branch is created and no commit is made?
You’re absolutely right to want to prevent duplicate GitHub Checks when a new branch is created but points to the same commit as an existing one (like main
). In Jenkins Multibranch Pipelines, this behavior is frustratingly common, and can pollute your Checks history and confuse reviewers.
Here’s a clearer breakdown of how to deal with it effectively (hopefully):
Recommended: Use a when
block to skip builds with duplicate commits
The most straightforward and portable approach is to use the when
condition in your Jenkinsfile to check whether the current commit (HEAD
) is already on another branch (like main
). If so, you can simply skip the meaningful parts of the pipeline.
Untested Example:
pipeline {
agent any
stages {
stage('Skip duplicate commit') {
when {
expression {
def mainSha = sh(script: "git rev-parse origin/main", returnStdout: true).trim()
def currentSha = sh(script: "git rev-parse HEAD", returnStdout: true).trim()
return mainSha != currentSha
}
}
steps {
echo "This is a new commit (not yet in main), proceeding with build."
}
}
// Other stages...
}
}
This should allow Jenkins to start the pipeline (so GitHub sees the Check), but skip execution unless there’s a meaningful difference. You can also short-circuit the build entirely with currentBuild.result = 'NOT_BUILT'
or even error('Skipping duplicate commit')
, depending on your desired UX.
Why Jenkins builds on branch creation (even with no commit)
When a new branch is created (via GitHub UI or CLI) but points to an existing commit, Jenkins sees a “new branch” and queues a build, even though the commit was already built elsewhere. This is likely because Jenkins keys builds by branch name, not just commit SHA.
Other strategies (less reliable):
Disable auto-build on branch indexing
This disables the immediate build on branch discovery, but then you lose first-run feedback entirely. Not ideal.
Filter via GitHub App/Webhook (if self-managed)
If you’re using a GitHub App integration, you could filter out create
events client-side. But afaik, Jenkins doesn’t natively distinguish between events triggered by a new branch and ones triggered by new commits.
Store commit SHAs externally
You could store a list of “already built” SHAs (in Artifactory, S3, or GitHub Checks metadata) and check it in the pipeline. This adds complexity and external dependencies.
Summary
- Use a
when
block to skip builds if the current commit matchesmain
(or any other relevant branch). - This avoids duplicate GitHub Checks and unnecessary builds while still reporting clearly.
- Jenkins itself doesn’t distinguish “new branch pointing to old commit”, so you have to add this logic yourself in the pipeline.