shalini gupta
shalini gupta

Reputation: 51

How can i read Jenkins pipeline console output during execution using Python Script?

I have a requirement to read the console output of a previous stage(of the pipeline) during the pipeline execution. The console output from previous stage will be input to a python script, which i am running in the next stage. Please suggest

Upvotes: 1

Views: 2008

Answers (1)

Dibakar Aditya
Dibakar Aditya

Reputation: 4203

If you use Blue Ocean Plugin, you can retrieve stage-wise console output using Blue Ocean REST API.

A stage in Blue Ocean URL is denoted by a node number when you click that stage. For example, in the URL .../blue/organizations/jenkins/<job_name>/detail/<job_name>/<build_number>/pipeline/24, the node number is 24. This value remains constant for a specific stage in a given pipeline.

Assuming your job has anonymous read permissions, in your downstream pipeline stage you can call a Python script to read the console output of any previous stage.

Sample Python script:

import os
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

current_build_num = os.environ['BUILD_NUMBER']
stage_url = 'https://<jenkins_base_url>/cdf/blue/rest/organizations/jenkins/pipelines/<job_name>/runs/{0}/nodes/<node_number>/log/'.format(current_build_num)
stage_log = requests.get(stage_url, verify=False).content
print(stage_log)

Upvotes: 1

Related Questions