2

I'm wondering if it's possible to specify a branch to build when triggering a Jenkins job remotely. I'm writing a Python script which involves running a specific Jenkins build.

I'm able to run the build from my script successfully, but I'm wondering if there's a way to make the build run against a specific branch.

The builds are running on Jenkins and I'm using TFS for my source code management.

This is how I'm invoking the job - I tried to add parameters but this didn't do anything.

crumb=CrumbRequester(username="admin", password="2fa99c1e2aa61671d6b4334c8f0e77af", baseurl='http://localhost:8080/')
a = Jenkins('http://localhost:8081/', username="admin", password="2fa99c1e2aa61671d6b4334c8f0e77af", requester=crumb)
job = a.get_job("Project PR_DataCollectionWeb")
job.invoke(build_params={'GIT_BRANCH': git_branch})

I cannot specify a branch to be built when this job runs as the branch that needs to be run cannot be pre-determined.

kicksticks
  • 239
  • 3
  • 14

3 Answers3

4

Add a string parameter called BRANCH

Add the the variable ${BRANCH} to the 'Branches to build' under Source Code Management session (I am referring git here, no necessary changes for TFS)

use the following url to trigger

http://<jenkins url>/job/<job name>/buildWithParameters?token=<your token>&BRANCH=<your branch>
canhazbits
  • 1,664
  • 1
  • 14
  • 19
Jomin
  • 113
  • 2
  • 8
0

Easy-peasy, use build parameters for that. For example, lets say you have the following job definition in Jenkins (using Groovy-alike pseudo-code):

[Project PR_DataCollectionWeb]
    parameters: { GIT_BRANCH: typeof(String), defaultValue: 'master' }

    sourceCodeManagement: {
        repositoryUrl: 'https://github.com/.../awesome.git',
        branchSpecifier: '*/$GIT_BRANCH'
    }

Which gives you ability to either use the default value or the one provided by the script that requests the build with parameters.

The point is, you can easily reference your build parameters 'virtually' anywhere in Jenkins job configuration page by using $VARIABLE_NAME or ${VARIABLE_NAME} syntax.

Hope that helps.

P.S. Also, here is the brief guide on Parametrized Build plugin in Jenkins.

pabloduo
  • 147
  • 8
  • thanks! When I use the code in my original post and I check the build parameters, I don't see it. So then it never really gets registered as a parameter for my build and I can't use it anywhere. I installed the parameterized build and I've got a default value so that's the only one that runs. – kicksticks Jul 19 '17 at 20:17
  • That is because build parameters could not travel in the query string - only in the body of the request in the form-encoded format, eq. `GIT_BRANCH=feature-a&PARAM_A=xyz&PARAM_B=xoxo&...` and it MUST to be a POST request. – pabloduo Jul 19 '17 at 21:21
0

You can do it by updating your job = a.get_job("Project PR_DataCollectionWeb") line to something like job = a["{}/{}".format(project, branch)].

Here is a full example:

from jenkinsapi.jenkins import Jenkins
from jenkinsapi.utils.crumb_requester import CrumbRequester

crumb_requester = CrumbRequester(username=USERNAME, password=PASSWORD, baseurl=URL)
jenkins = Jenkins(URL, username=USERNAME, password=PASSWORD, requester=crumb_requester)
job = jenkins["{}/{}".format(project, branch)]
queue_item = job.invoke(build_params={'build_type': build_type})
Daniel
  • 20,420
  • 10
  • 92
  • 149