Jobs

Jobs

Interact with a job

jobs.Jobs.search()

Search for a job within Jenkins.

Parameters:

job_path (str) – The path of the job to search for.

Returns:

The job object.

Return type:

jenkins_pysdk.jobs.Job

Raises:

JenkinsNotFound: If the job wasn’t found.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("new_freestyle"))

The above code will output:

<jenkins_pysdk.jobs.Job object at 0x000001E515CD4A90>

List all jobs

jobs.Jobs.list()

Retrieve a list of jobs from Jenkins.

Parameters:
  • folder (str or None) – (Optional) The folder from which to retrieve jobs.

  • _paginate (int) – (Optional) The number of jobs to retrieve per paginated request. Set to 0 to disable pagination.

Returns:

A list of Job objects representing the jobs in the specified folder.

Return type:

List[jenkins_pysdk.jobs.Job]

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.list())

The above code will output:

[<jenkins_pysdk.jobs.Job object at 0x000002B4E528A710>, <jenkins_pysdk.jobs.Job object at 0x000002B4E5119780>, <jenkins_pysdk.jobs.Job object at 0x000002B4E5118580>, <jenkins_pysdk.jobs.Job object at 0x000002B4E511BC10>, <jenkins_pysdk.jobs.Job object at 0x000002B4E5119870>]

Iterate all jobs

jobs.Jobs.iter()

Iterate through jobs in the Jenkins instance.

Parameters:
  • folder (str, optional) – The folder to iterate through. If None, iterate through all jobs.

  • _paginate (int, optional) – Pagination flag. Defaults to 0 (disabled).

Returns:

Generator yielding Job objects.

Return type:

Generator[jenkins_pysdk.jobs.Job]

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.iter())

The above code will output:

[<jenkins_pysdk.jobs.Job object at 0x000002B4E528A710>, <jenkins_pysdk.jobs.Job object at 0x000002B4E5119780>, <jenkins_pysdk.jobs.Job object at 0x000002B4E5118580>, <jenkins_pysdk.jobs.Job object at 0x000002B4E511BC10>, <jenkins_pysdk.jobs.Job object at 0x000002B4E5119870>]

Create a freestyle job

jobs.Jobs.create()

Create a job on the Jenkins instance.

Parameters:
  • job_path (str) – The path where the job should be created.

  • xml (str) – XML configuration for the job.

  • job_type (jenkins_pysdk.objects.Jobs) – Additional parameters for job creation.

Returns:

Object representing the result of the creation action.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
freestyle = """<project>
                <description>My description goes here</description>
                <keepDependencies>false</keepDependencies>
                <properties/>
                <scm class="hudson.scm.NullSCM"/>
                <canRoam>true</canRoam>
                <disabled>false</disabled>
                <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
                <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
                <triggers/>
                <concurrentBuild>false</concurrentBuild>
                <builders/>
                <publishers/>
                <buildWrappers/>
               </project>"""
print(jenkins.jobs.create("freestyle_created", freestyle, jenkins.FreeStyle))

The above code will output:

request=<Request object at 2205281481040> content='[200] Successfully created freestyle_created.' status_code=200

Check if the path is a job

jobs.Jobs.is_job()

Checks if the path corresponds to a job in Jenkins.

Parameters:

path (str) – The path of the job to check.

Returns:

True if the path corresponds to a job, False otherwise.

Return type:

bool

Raises:
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.is_job("folder3/freestyle_4"))

The above code will output:

True

Job

Disable a job

jobs.Job.disable()

Disable the job.

Returns:

Result of the request to disable the job.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("folder3/freestyle_4").disable())

The above code will output:

request=<Request object at 2523890810240> content='[200] Successfully disabled folder3/freestyle_4.' status_code=200

Get a job URL

jobs.Job.url()

Get the URL of the job.

Returns:

The URL of the job.

Return type:

str

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("folder3/freestyle_4").url)

The above code will output:

https://JenkinsDNS/job/folder3/job/freestyle_4

Get a job path

jobs.Job.path()

Get the path of the job.

Returns:

The path of the job.

Return type:

str

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("folder3/freestyle_4").path)

The above code will output:

folder3/freestyle_4

Get job URL

jobs.Job.enable()

Enable the job.

Returns:

The outcome of enabling the job.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("folder3/freestyle_4").enable())

The above code will output:

request=<Request object at 1986068844192> content='[200] Successfully enabled folder3/freestyle_4.' status_code=200

Reconfigure a job

jobs.Job.reconfig()

Reconfigure the job.

Parameters:

xml (str, optional) – The XML configuration to use for reconfiguration.

Returns:

The outcome of reconfiguring the job.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
from jenkins_pysdk.builders import Builder
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
config = Builder._Templates.Freestyle.format(description="New desc", disabled=True)
print(jenkins.jobs.search("freestyle_created").reconfig(config))

The above code will output:

request=<Request object at 1772165197280> content='[200] Successfully reconfigured freestyle_created.' status_code=200

Delete a job

jobs.Job.delete()

Delete the folder.

Returns:

Result of the delete operation.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("freestyle_created").delete())

The above code will output:

request=<Request object at 1721615969440> content='[204] Successfully deleted job.' status_code=204

Get job config

jobs.Job.config()

Get the XML configuration of the job.

Returns:

The XML configuration of the job.

Return type:

str

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("new_freestyle").config)

The above code will output:

<?xml version='1.1' encoding='UTF-8'?>
<project>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties/>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders/>
  <publishers/>
  <buildWrappers/>
</project>

Get a job’s builds

jobs.Job.builds()

Access the builds associated with this job.

Returns:

Builds associated with this job.

Return type:

jenkins_pysdk.builds.Builds

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("new_freestyle").builds)

The above code will output:

<jenkins_pysdk.builds.Builds object at 0x000001EF9C34A860>

Interact with the job workspace

jobs.Job.workspace()
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("freestyle_created").workspace)

The above code will output:

<jenkins_pysdk.workspace.Workspace object at 0x00000153D2E27580>