Build job in jenkins through python script ssl connection

When it comes to automating tasks in Jenkins using Python, one common requirement is to build a job through a Python script with an SSL connection. In this article, we will explore three different ways to achieve this goal.

Option 1: Using the Jenkins API

The Jenkins API provides a straightforward way to interact with Jenkins programmatically. To build a job with an SSL connection, we can use the requests library to send an HTTP POST request to the Jenkins API endpoint.

import requests

def build_job_with_ssl(job_name):
    url = f"https://jenkins.example.com/job/{job_name}/build"
    response = requests.post(url, verify='/path/to/ssl/certificate')
    
    if response.status_code == 201:
        print(f"Job '{job_name}' successfully triggered.")
    else:
        print(f"Failed to trigger job '{job_name}'. Error: {response.text}")

In this code snippet, we specify the Jenkins job’s URL with the job name and use the requests.post() method to send an HTTP POST request. The verify parameter is set to the path of the SSL certificate file to establish a secure connection.

Option 2: Using the Jenkins CLI

The Jenkins CLI (Command Line Interface) allows us to interact with Jenkins from the command line. We can leverage this feature to build a job with an SSL connection by executing the CLI command through a Python script.

import subprocess

def build_job_with_ssl(job_name):
    command = f"java -jar jenkins-cli.jar -s https://jenkins.example.com/ build {job_name} --ssl"
    subprocess.call(command, shell=True)

In this code snippet, we use the subprocess module to execute the CLI command. The command includes the path to the Jenkins CLI JAR file, the Jenkins server URL, the job name, and the --ssl flag to establish an SSL connection.

Option 3: Using the Jenkins Python Library

The Jenkins Python Library is a powerful tool that provides a high-level API for interacting with Jenkins. It simplifies the process of building a job with an SSL connection by abstracting the underlying HTTP requests.

from jenkinsapi.jenkins import Jenkins

def build_job_with_ssl(job_name):
    jenkins = Jenkins('https://jenkins.example.com/', ssl_verify='/path/to/ssl/certificate')
    job = jenkins[job_name]
    job.invoke()

In this code snippet, we create an instance of the Jenkins class from the Jenkins Python Library, passing the Jenkins server URL and the path to the SSL certificate. We then retrieve the desired job using its name and invoke it.

After exploring these three options, it is clear that using the Jenkins Python Library provides the most elegant and convenient solution. It abstracts the complexities of SSL connections and provides a high-level API for interacting with Jenkins. Therefore, option 3 is the recommended approach for building a job in Jenkins through a Python script with an SSL connection.

Rate this post

13 Responses

    1. Totally disagree! Option 3 is overrated. Jenkins is the real deal for continuous integration. Its flexibility and extensive plugin ecosystem cant be beaten. Dont underestimate the power of Jenkins, my friend. #JenkinsForLife

    1. I totally agree, its a tough call. Personally, Id go with Option 3 for the added flexibility. But hey, thats just my two cents. Everyones journey with Jenkins is different! #JenkinsJourney

    1. I couldnt disagree more. Python may have its strengths, but relying on a library for Jenkins sounds like an unnecessary complication. Stick to the basics and keep it simple.

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents