Real time status of jenkins Build to endpoint

Jenkins setup:

I want to send the real time status of Jenkins build each stage wise with all details like runtime of particular stage, if failure what is reason of the failure from console output, all essential information to an endpoint, then I will use get /post method call Api and get the Real Time data.

It should be like while calling ,if mutiple jobs are running it should fetch all jobs data .One Api then get status of all jobs that are running

Hello and welcome to this community, @Tech_Tan. :wave:

To achieve this, I think you could use a combination of Jenkins Pipeline, Groovy scripting, and the Jenkins REST API. :thinking:

Here is an untested example (because I don’t have an endpoint to receive the events) of how you could maybe implement this:

  1. Create a Jenkins Pipeline Script: This script will capture the status of each stage and send the data to an endpoint.
  2. Use the Jenkins REST API: To fetch the status of all running jobs.

Step 1: Jenkins Pipeline Script

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script {
                    def startTime = System.currentTimeMillis()
                    try {
                        // Your build steps here
                        echo 'Building...'
                    } catch (Exception e) {
                        def endTime = System.currentTimeMillis()
                        def duration = endTime - startTime
                        sendStageStatus('Build', 'FAILURE', duration, e.message)
                        throw e
                    }
                    def endTime = System.currentTimeMillis()
                    def duration = endTime - startTime
                    sendStageStatus('Build', 'SUCCESS', duration, null)
                }
            }
        }
        stage('Test') {
            steps {
                script {
                    def startTime = System.currentTimeMillis()
                    try {
                        // Your test steps here
                        echo 'Testing...'
                    } catch (Exception e) {
                        def endTime = System.currentTimeMillis()
                        def duration = endTime - startTime
                        sendStageStatus('Test', 'FAILURE', duration, e.message)
                        throw e
                    }
                    def endTime = System.currentTimeMillis()
                    def duration = endTime - startTime
                    sendStageStatus('Test', 'SUCCESS', duration, null)
                }
            }
        }
    }
}

def sendStageStatus(stageName, status, duration, errorMessage) {
    def payload = [
        stageName: stageName,
        status: status,
        duration: duration,
        errorMessage: errorMessage
    ]
    def jsonPayload = new groovy.json.JsonBuilder(payload).toString()
    httpRequest(
        httpMode: 'POST',
        url: 'http://your-endpoint.com/api/status',
        contentType: 'APPLICATION_JSON',
        requestBody: jsonPayload
    )
}

You will have to accept in Scripts Approvals new groovy.json.JsonBuilder java.lang.Object and to install the HTTP Request plugin.

Step 2: Fetch Status of All Running Jobs
You should be able to use the Jenkins REST API to fetch the status of all running jobs. Here is an example of how to do this using a Groovy script:

import groovy.json.JsonSlurper

def jenkinsUrl = 'http://your-jenkins-url.com'
def apiUrl = "${jenkinsUrl}/api/json?tree=jobs[name,url,color]"

def getRunningJobs() {
    def response = new URL(apiUrl).text
    def json = new JsonSlurper().parseText(response)
    def runningJobs = json.jobs.findAll { it.color == 'blue_anime' }
    return runningJobs
}

def runningJobs = getRunningJobs()
runningJobs.each { job ->
    println "Job Name: ${job.name}"
    println "Job URL: ${job.url}"
}

You could also do that thanks to bash or python:

#!/bin/bash

JENKINS_URL="http://your-jenkins-url.com"
API_URL="${JENKINS_URL}/api/json?tree=jobs[name,url,color]"

# If your Jenkins requires authentication, use the following variables
# Replace 'your-username' and 'your-api-token' with your actual Jenkins credentials
USERNAME="****"
API_TOKEN="****"

# Fetch the JSON response from Jenkins API using wget
if [ -n "$USERNAME" ] && [ -n "$API_TOKEN" ]; then
  response=$(wget --quiet --user=$USERNAME --password=$API_TOKEN --output-document=- $API_URL)
else
  response=$(wget --quiet --output-document=- $API_URL)
fi

# Check if the response is empty
if [ -z "$response" ]; then
  echo "Failed to fetch data from Jenkins API. Please check your URL and credentials."
  exit 1
fi

# Parse the JSON response to find running jobs
echo "$response" | jq -r '.jobs[] | select(.color == "blue_anime" or .color == "blue") | "\(.name) \(.url)"'
import requests
import json

JENKINS_URL = "http://your-jenkins-url.com"
API_URL = f"{JENKINS_URL}/api/json?tree=jobs[name,url,color]"
USERNAME = "****"
API_TOKEN = "****"

# Fetch the JSON response from Jenkins API using requests
response = requests.get(API_URL, auth=(USERNAME, API_TOKEN), headers={"Accept": "application/json"})

# Check if the response is empty
if response.status_code != 200:
    print("Failed to fetch data from Jenkins API. Please check your URL and credentials.")
    exit(1)

# Parse the JSON response to find running jobs
jobs = response.json().get('jobs', [])
for job in jobs:
    if job['color'] in ['blue_anime', 'blue']:
        print(f"{job['name']} {job['url']}")
1 Like

Thank you, @poddingue, for your response.

I need assistance with retrieving the live status of triggered jobs without modifying any pipeline code. I’m looking for an API (such as a URL) that provides access to the live status of all running jobs. By calling this API in tools like Postman, I want to view real-time job data.

The solution should be automatic, meaning that as the job progresses from stage 1 to stage 2, the output should display the status of both stages in a single call (rather than calling individually for each stage). It should continuously update as the pipeline moves through stages.

Ideally, the output should resemble the pipeline console output.

1 Like

afaik there is no such api that allows to fetch the status of all jobs down to giving the stage that is currently executed.
There is an api to ask for the state of a single pipeline job, but I think it is very lowlevel and it will give you the state of all steps. So you would need to extract from there the state of the stages. And stages that haven’t started yet will not appear I think.
You might want to look at the OpenTelemetry plugin that can forward state to an external system from where you can perform your queries more easily I think.

1 Like

Hello everyone,

We have a specific API available to retrieve the console output and pipeline logs from Jenkins. By providing the job name and build number (whether currently running or from a previous execution), we can obtain the pipeline stage status for external use via this endpoint:

http://localhost:8080/job/job_name/build_number/logText/progressiveText?start=0

We need a program that, upon triggering a job, fetches the job name and build number from Jenkins and passes them to this API. To keep the API connection active, the program should continuously poll the console output(Keep the API alive).

Hi @Tech_Tan
I am new to Jenkins can you answer my few questions listing here

  1. I want to achieve parallel execution on Jenkins is that possible
  2. If yes I want to run nearly 11 agents at a time with single agent will take up to 5hrs run time
  3. Does this Jenkins support my requirement’s

You should ask that in a separate thread but the anwser is yes to all 3 questions.

2 Likes

Hi

  1. Yes, Parallel execution is possible in Jenkins
  2. if you want to run multiple number of agents check your system computational resources are suitable (CPU core,RAM,Disk ) and agent configuration are suitable before running. so that the agents take less time .
    Instead of running individually use parallel execution.

Hi,

Okay i will check my resources and try parallel execution
Thanks for the update!