Jenkins Automation using Python.

Abhishek ManeAbhishek Mane
3 min read

Jenkins-

An open-source automation server that is used to automate various stages of the software delivery process, such as building, testing, and deploying software. It is widely used in DevOps practices for continuous integration and continuous delivery.

Python-

Python is a popular high-level programming language that is widely used for web development, scientific computing, data analysis, artificial intelligence, and more. It is known for its simplicity, readability, and flexibility.

What can we automate in Jenkins using Python?

  1. Create, View, Copy, Update, Disable, and Delete Jobs.

  2. Start, Stop, View, Delete A Build.

  3. Create, View, Update, Delete Jenkins View.

  4. Create, View, Update, and Delete Jenkins Nodes.

  5. Get any info about any resource and much more!

  6. For a comprehensive understanding of the product's features and functions, we recommend checking out the official documentation.

Python libraries to automate Jenkins

Python-Jenkins

  1. JenkinsAPI

  2. Python-Jenkins

  3. api4Jenkins

  4. aiojenkins

Create and Activate the virtual environment and install the Python library.

  1. Activate virtual environment

    python -m venv venv

    . venv/bin/activate

  2. Install the library using pip

    pip install python-jenkins

  3. Save dependencies in requirements.txt

    pip freeze > requirements.txt

  4. To install dependencies next time:

    pip install -r requirements.txt

Configure Jenkins Client in Python.

import jenkins server = jenkins.Jenkins('http://localhost:8080',

username='your-username', password='token-or-your-password')

(Creating tokens in Jenkins- Click on your username→Configure→under API token click 'Add new token'→Give a name and 'Generate token'→copy the code and paste in the password.)

Test API access by fetching Jenkins Username and Version

user = server.get_whoami()

version = server.get_version()

print('Hello %s Jenkins %s' % (user['fullName'], version))

Jobs

  • Getting a .xml file.

    Create a job(echo "Hello World")→Run it→Click on REST API on the bottom right corner of the page→Scroll down and click on "Fetch/Update config.xml"→Click on "same URL"→save the .xml file.

    You can use this file later for job-building purposes.

  • To create an empty job

    server.create_job("job1", jenkins.EMPTY_CONFIG_XML)

  • To create a Pre-configured Job

    job2_xml = open("job2.xml", mode= 'r', encoding='utf-8').read()

    server.create_job("job2", job2_xml)

  • To view Jobs

    jobs = server.get_jobs() print(jobs)

  • To copy jobs

    server.copy_job('job2' , 'job4')

  • To update jobs

    updated_job_3 = open("job_3_updated.xml", mode= 'r',encoding='utf-8').read() server.reconfig_job('job3', updated_job_3)

  • To disable a job

    server.delete_job('job1')

  • Files for performing automation tasks.

    job2.xml

    job3.xml job_3_updated.xml

Build

  • To start a build

    server.build_job(job3)

  • To get the Build Number

    last_build_number = server.get_job_info('job3') ['lastCompletedBuild']['number'] print("Build Number", last_build_number)

  • To get Build Info.

    build_info = server.get_build_info('job3', build_number)

    print("build info", build_info)

  • To stop a build

    server.stop_build('job3', build_number)

  • To delete a build

    server.delete_build('job3', build_number)

Views

(Presenting the job list/Categorising them)

  • Getting a .xml file of the list.

    Click on the (+) →Name the list→Select the list type (List view)→Click on "create"→Enter the description→Add jobs to the list→OK→Click on REST API on the bottom right corner of the page→Scroll down and click on "Fetch/Update config.xml"→Click on "same URL"→save the .xml file.

  • To create a view

    view_config = open("jobs_view.xml", mode= 'r',encoding='utf-8').read()

    server.create_view("Job List", view_config)

  • To get the list of views.

    views = server.get_views() print(views)

  • To update a view.

    updated_view_config = open("jobs_view_updated.xml",mode= 'r' ,

    encoding='utf-8').read()

    server.reconfig_view("Job List", updated_view_config)

  • To delete a view

    server.delete_view("Job List")

  • .xml files for automation of the views.

    jobs_views.xml jobs_views_updated.xml

In conclusion, the blog serves as a valuable resource for automating Jenkins tasks using Python. It covers essential steps, such as library requirements, virtual environment creation, and configuring the Jenkins client in Python. The code snippets and .xml files provided make it easier to streamline jobs, build, and view tasks, enabling developers and DevOps professionals to optimize their software delivery pipelines.

Thanks for reading, and we hope you found this blog helpful!

Thanks alot Sandip Das

4
Subscribe to my newsletter

Read articles from Abhishek Mane directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhishek Mane
Abhishek Mane