问题
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
回答1:
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)
来源:https://stackoverflow.com/questions/57751844/how-can-i-read-jenkins-pipeline-console-output-during-execution-using-python-scr