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

泄露秘密 提交于 2021-01-28 07:51:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!