Automating Jenkins Job Information Retrieval with Bash Script

Introduction

Jenkins is a widely used automation server that allows the automation of building, testing, and deploying software. One of the powerful features of Jenkins is its API, which enables users to interact programmatically with Jenkins to retrieve information about jobs and builds.

In this blog post, we’ll explore a Bash script that leverages the Jenkins API to fetch job details such as build numbers, timestamps, and statuses for jobs within specified folders.

Prerequisites

Before using the script, ensure you have:

  • Jenkins instance accessible over the internet.
  • API token or username/password for authentication.

The Script

#!/bin/bash

# Set your Jenkins URL and API token or username/password
JENKINS_URL="http://your_jenkins_url/view/all"
API_TOKEN="your_api_token_or_username:your_api_token_or_password"

API_ENDPOINT="${JENKINS_URL}/api/json"

# Function to get job details for a specific folder
function get_job_details_for_folder() {
    local folder_name=$1
    local job_list_endpoint="${JENKINS_URL}/job/${folder_name}/api/json"
    local json_response=$(curl --silent --user "${API_TOKEN}" "${job_list_endpoint}")
    local job_details=$(echo "${json_response}" | jq -r '.jobs[] | { name: .name, last_build_number: .lastBuild.number, last_build_timestamp: .lastBuild.timestamp, last_build_status: .lastBuild.result }')
    echo "${job_details}"
}

# Function to get job details for a specific job inside a folder
function get_job_details_for_job() {
    local folder_name=$1
    local job_name=$2
    local job_details_endpoint="${JENKINS_URL}/job/${folder_name}/job/${job_name}/api/json"
    local json_response=$(curl --silent --user "${API_TOKEN}" "${job_details_endpoint}")
    local job_details=$(echo "${json_response}" | jq -r '{ name: .name, last_build_number: .lastBuild.number, last_build_timestamp: .lastBuild.timestamp, last_build_status: .lastBuild.result }')
    echo "${job_details}"
}

# Main script logic
json_response=$(curl --silent --user "${API_TOKEN}" "${API_ENDPOINT}")

folder_names=$(echo "${json_response}" | jq -r '.jobs[] | select(._class == "com.cloudbees.hudson.plugins.folder.Folder") | .name')

echo "List of Folder Names:"
echo "${folder_names}"

echo "Job Details in each Folder:"
for folder_name in ${folder_names}; do
    echo "Folder: ${folder_name}"
    jobs=$(get_job_names_for_folder "${folder_name}")
    echo "${jobs}"

    for job_name in ${jobs}; do
        job_details=$(get_job_details_for_job "${folder_name}" "${job_name}")
        echo "${job_details}"
    done

    echo "-------------------------"
done

Usage

  1. Replace your_jenkins_url, your_api_token_or_username, and your_api_token_or_password with your Jenkins URL, API token, or username/password.
  2. Save the script to a file, e.g., jenkins_job_info.sh.
  3. Make the script executable: chmod +x jenkins_job_info.sh.
  4. Run the script: ./jenkins_job_info.sh.

The script will display a list of folder names and the details (name, last build number, timestamp, and status) for each job within those folders.

Conclusion

Automating the retrieval of Jenkins job information using the Jenkins API and a Bash script can save time and provide valuable insights into the status of your automation pipeline. Feel free to customize the script based on your specific requirements.

Happy automating! 🚀