Chaitanya Bapat
Chaitanya Bapat

Reputation: 4029

Make Jenkins discover PR branch without automatically triggering build

Attempt 1 : Trigger Jenkins build using Python API

Using Jenkins Python API, I am able to trigger a jenkins build for a PR (job.invoke())

JENKINS_URL = "<url>"
JENKINS_USERNAME = "<>"
JENKINS_PASSWORD = "<>"

class DevOpsJenkins:
    def __init__(self):
        self.jenkins_server = Jenkins(JENKINS_URL, username=JENKINS_USERNAME, password=JENKINS_PASSWORD)

    def build_job(self, name, build_no=None):
        job = self.jenkins_server[name]
        job.invoke(block=False)

if __name__ == "__main__":
    NAME_OF_JOB = "<>/"
    pipelines = ['apache-centos-gpu']
    prs = [14]
    jenkins_obj = DevOpsJenkins()
    for i in range(len(prs)):
        for j in range(len(pipelines)):
            job_name = NAME_OF_JOB+pipelines[j]+"/PR-"+str(prs[i])
            jenkins_obj.build_job(job_name)

Provided, discover branch strategy is as follow discover_branch

Issue with Attempt 1 : Undesired Automatic trigger

However, the issue with this is, it discovers the PR branches and automatically triggers build on them everytime a new PR is created or a new commit is pushed to the PR branch.

Attempt 2 : Prevent branch discovery to stop automatic trigger

To solve that, I changed the discover strategy so as to not discover anything no_discover

As you can see in the empty behavior field.

Result? No branch is discovered. Not even PR branch. This prevents automatic triggering of PR builds. But this also prevents manual triggering of PRs. Basically, PR job can't be triggered anymore.

If I try the same above code with minor adjustments (of changing job name/pipeline) I get this error jenkinsapi.custom_exceptions.UnknownJob

So, the question is : how should I ensure branches are discovered without automatically triggering the PRs?

Why do I want to discover branches? So that I can manually trigger Jenkins builds.

I can't see any button which allows to enable/disable Automatic triggering for Jenkins builds.

Upvotes: 3

Views: 2667

Answers (1)

JO_
JO_

Reputation: 574

I discovered the solution to this problem in this site.

Quoting:

Go to the configuration settings of your multibranch pipeline and in the “Branch Sources” section click on the “Add property” and select the property, named “Suppress automatic SCM triggering“.

This will prevent Jenkins from triggering the builds every time it discovers new branches.

Upvotes: 2

Related Questions