I have a Jenkinsfile that I've set up with a cron
for a pipelineTriggers
parameter. I can't seem to figure out how to disable the job from building from a merge to the master branch of the repo. Is there a way in the Jenkinsfile to disable the automatic build from an scm change?

- 4,673
- 5
- 31
- 44
-
2Can you provide more context about how you configured your job ? Are you using `Pipeline script from SCM` option ? – Pom12 Mar 15 '17 at 09:01
11 Answers
If you're using a Multibranch Pipeline, you should be able to do this on the job's Configure page:
- Scroll down to "Branch Sources"
- Under "Property strategy", choose "Named branches get different properties"
- Click "Add exception", enter "master" as the branch name
- Click "Add property", choose "Suppress automatic SCM triggering"
- Save
That would prevent changes to the master
branch from triggering a build of the corresponding job.

- 110,418
- 27
- 198
- 193
-
20Do you know if it is possible to specify this some how in the Jenkinsfile itself? – Hans Kristian Aug 02 '17 at 21:12
-
7This is great for a single Multibranch Pipeline, but not helpful for granular control of a Branch Source Pipeline (eg Github Org, Stash, etc) where job configuration applies to an entire hierarchy of projects. Jenkinsfile is the logical place for this, yet having many "Aborted" builds in the history (as in the other solutions) is ugly, so a better solution is still needed (something like: properties [autoScmTrigger: false] ) – Akom Apr 05 '18 at 20:05
-
2@Akom Is there a better solution? I am using Team/Project and need a solution. – yusuf tezel Apr 09 '18 at 06:33
-
2@yusuftezel, no, but see these tickets in case the discussion goes somewhere: https://issues.jenkins-ci.org/browse/JENKINS-35988 https://issues.jenkins-ci.org/browse/JENKINS-43842 – Akom Apr 10 '18 at 14:48
-
10
-
1@c4sh Do it on the SCM side. I used GitHub WebHook to connect to Jenkins. So I disabled the GitHub WebHook. See if that helps you. – Chaitanya Bapat Mar 09 '20 at 09:36
-
1If you want to disable the trigger for all the branches you can just use the "Add Property" button under "Property strategy", there you have the "Suppress automatic SCM triggering" option. – Hemaolle Mar 12 '20 at 09:21
-
1@c4sh , I've run some tests, and it looks to me like the property seems to successfully suppress triggering from branch indexing, despite being marked as "deprecated". Have not tested it with SCM changes. Your mileage may vary. – cowlinator Feb 11 '21 at 02:31
For declarative pipelines, use the when
directive with a triggeredBy
condition, e.g.
when { triggeredBy 'TimerTrigger' }

- 26,937
- 7
- 52
- 74
-
5Is there any way to do this for an entire pipeline, vs using `when` in every stage? – Max Cascone Sep 17 '20 at 02:02
With the multibranch pipeline, I could not figure out a way to prevent the next build being triggered. As a workaround, I added the following code to my Jenkinsfile (using scripted syntax), to abort the following build if the only changes contain "[ci-skip]" in the commit message:
def abortBuildIfTriggeredBySkippableCommit() {
def changeSetCount = 0;
def ciSkipCount = 0;
if (currentBuild.changeSets != null) {
for (changeSetList in currentBuild.changeSets) {
for (changeSet in changeSetList) {
changeSetCount++;
if (changeSet.msg.contains('[ci-skip]')) {
ciSkipCount++;
}
}
}
}
if (changeSetCount > 0 && changeSetCount == ciSkipCount) {
currentBuild.result = 'NOT_BUILT'
error("Stopping to prevent auto trigger. All commits contained [ci-skip]")
}
}
Note that this code assumes you are using the git plugin, and that the objects in currentBuild.changeSets
will be GitChangeSetList.

- 61
- 2
In a jenkins job you can navigate to advanced source code management
- Select behavior
Dont trigger build on commit notification
This disables the Started by an SCM change

- 323
- 2
- 7
For people still looking for a solution, go to configuration for the multi branch pipeline, under Property Strategy, choose "Suppress Automatic SCM Triggering".
Note: This is available on cloudbees version of Jenkins. I am not sure, if it matters. https://support.cloudbees.com/hc/en-us/articles/360026953651-Preventing-builds-from-getting-triggered-when-creating-a-new-multibranch-Pipeline-or-Organization-Folder?page=29

- 40
- 6
For declarative pipelines, there is a much more simple answer now. From the docs:
overrideIndexTriggers
Allows overriding default treatment of branch indexing triggers. If branch indexing triggers are disabled at the multibranch or organization label,
options { overrideIndexTriggers(true) }
will enable them for this job only. Otherwise,options { overrideIndexTriggers(false) }
will disable branch indexing triggers for this job only.
It's a little backwards conceptually, but assuming your jobs are triggering off github webhooks by default, you set overrideIndexTriggers(false)
to disable the automatic triggering.

- 648
- 9
- 25
This is what I came up with. I was hoping for something less messy, but this does seem to work:
I have this as the build's properties:
properties([
pipelineTriggers([cron('H H 7 * *')])
])
I then have this function that defines the source of the build:
// check if the job was started by a timer
@NonCPS
def jobStartedByWhat() {
def startedByWhat = ''
try {
def buildCauses = currentBuild.rawBuild.getCauses()
for ( buildCause in buildCauses ) {
if (buildCause != null) {
def causeDescription = buildCause.getShortDescription()
echo "shortDescription: ${causeDescription}"
if (causeDescription.contains("Started by timer")) {
startedByWhat = 'timer'
}
if (causeDescription.contains("Started by user")) {
startedByWhat = 'user'
}
}
}
} catch(theError) {
echo "Error getting build cause: ${theError}"
}
return startedByWhat
}
def startedByWhat = jobStartedByWhat()
I can then evaluate the function at runtime so that if a build gets triggered because of a merge to master, it will not actually run:
node {
try {
checkout scm
if (env.BRANCH_NAME == 'master') {
if (startedByWhat == 'timer' || startedByWhat == 'user') {
..... RUN THE BUILD .....
} else {
.... EXIT WITHOUT RUNNING THE BUILD ....

- 4,673
- 5
- 31
- 44
I stumbled upon this as well. IMO an acceptable solution would be a filter for commit messages when checking out source code - this feature exists for regular Jobs but is missing for multibranch pipelines, see https://issues.jenkins-ci.org/browse/JENKINS-48296.
For those not using the git plugin, this method is a workaround for scripted pipelines (inspired by scarswell's answer):
def abortBuildIfTriggeredBySkippableCommit() {
lastCommitMessage = sh(
script: "${gitBinary} --no-pager log -1 --pretty=%B",
returnStdout: true
)
if (lastCommitMessage != null &&
lastCommitMessage.toString().contains('[maven-release-plugin]')) {
currentBuild.result = 'ABORTED'
error("Stopping build, it was triggered by the maven release plugin")
}
}

- 11
- 2
-
One problem with this is that as long as your last commit message is the one that causes the build to abort, also all manually triggered builds will fail. Workaround for that is to check the build trigger from currentBuild.rawBuild.getCauses() and make sure the short description contain "Push event to branch" before aborting. – diidu Apr 23 '18 at 11:31
-
If you are using Pipeline script from SCM then comment out the triggers section(either SCMPoll/BuildPeriodically option ) in Jenkins file as shown below.
//triggers {cron ('H/15 * * * *')} //pipelineTriggers([pollSCM('H/15 * * * *')])
If you are using Pipeline script then disable the PollSCM/Build periodically(whichever is used) option.

- 319
- 1
- 3
One could disable the scm build trigger by disabling the webhook notification from git.

- 1,640
- 1
- 17
- 26
-
where to "disable webhook notification from git"? where is this option? on jenkins? on github webhook? – Chaitanya Bapat Mar 08 '20 at 03:24
-
1Typically, a GitHub repository is configured with a series of webhooks. They respond to push events, merge events, pull request events. They would be sent to REST API endpoints in Jenkins. Your task would be to find where they are configured in GitHub with your source repository settings and disable it. – macetw Mar 09 '20 at 05:48
-
Correct. I use GitHub WebHook to connect to Jenkins. I wonder if I can de-select specific event and keep WebHook active. Is it pull-request event that's responsible for build trigger? – Chaitanya Bapat Mar 09 '20 at 09:37
-
I have a similar issue. I can see the webhooks in Github, however, they are managed by the github Jenkins plugin, so I assume if I start removing them, they'll just be put back by the Jenkins plugin. So the answer must lie on the Jenkins side. I'd like to see the solution for doing this in the Jenkinsfile. I was also assuming there must be a way to set the pipelineTriggers property to give the desired behavior. – spozun Jun 24 '20 at 01:28
-
Actually if you want to do this, in the ORG Config in Jenkins you can supply a regex here to tell it which jobs to trigger from SCM: "Automatic branch project triggering" – spozun Jun 24 '20 at 01:38
if (currentBuild.getBuildCauses().toString().contains('BranchIndexingCause') || currentBuild.getBuildCauses().toString().contains('Branch event')) {
print "INFO: Build skipped due to trigger being Branch Indexing"
currentBuild.result = 'ABORTED' // optional, gives a better hint to the user that it's been skipped, rather than the default which shows it's successful
return
}
timestamps{
node
{
stage('Getting Build Parameters')
{
print('build job')
}
}
}
if (currentBuild.getBuildCauses().toString().contains('BranchIndexingCause') || currentBuild.getBuildCauses().toString().contains('Branch event')) {
print "INFO: Build skipped due to trigger being Branch Indexing"
currentBuild.result = 'ABORTED' // optional, gives a better hint to the user that it's been skipped, rather than the default which shows it's successful
return
}
if (currentBuild.getBuildCauses().toString().contains('BranchIndexingCause') || currentBuild.getBuildCauses().toString().contains('Branch event')) { print "INFO: Build skipped due to trigger being Branch Indexing" currentBuild.result = 'ABORTED' // optional, gives a better hint to the user that it's been skipped, rather than the default which shows it's successful return }

- 21
- 5