Update README.md - #3
Conversation
|
f you mean how to read Jenkins CI/CD console output (consoleText) programmatically, Jenkins exposes the build log through its REST-style endpoints.
For a job: https://jenkins.example.com/job/my-job/123/consoleText Where: my-job = Jenkins job You can also use: curl -u "$JENKINS_USER:$JENKINS_API_TOKEN" For a Pipeline, the same endpoint works.
You might get: Started by user john
The important thing is that consoleText is just text. Jenkins doesn't return a structured JSON representation of each log line.
A simple approach is to look for: Finished: SUCCESS or: Finished: FAILURE But for reliable automation, don't infer build status from the log. Get the build JSON: https://jenkins.example.com/job/my-job/123/api/json For example: curl -u "$JENKINS_USER:$JENKINS_API_TOKEN" Then inspect: { result is the authoritative build result.
A common pattern is: GET /job/{job}/{build}/consoleText For example, if you're building an AI/log parser, you could classify lines into: Pipeline stages One important distinction: Jenkins also has progressiveText, which is useful when the build is still running, because you can incrementally fetch newly generated log output rather than repeatedly downloading the entire consoleText. If you tell me whether you're trying to read consoleText from Java, Python, Node.js, a Jenkins plugin, or an API, I can show the exact implementation. Yes — for your use case, I would use structured JSON to identify the failed stage, then fetch only the relevant log for that stage and send that to the LLM. Recommended flow
If you use the Jenkins Pipeline REST API: /job/my-pipeline/123/wfapi/describe You can get stage information such as: { From this, you immediately know: Failed stage: Test The important limitation is that Jenkins often stores the console output as one overall build log. So your next step is to retrieve the log associated with the failed Pipeline node/stage. Depending on your Jenkins Pipeline REST API setup, you can retrieve node details/logs, for example via endpoints similar to: /job/my-pipeline/123/execution/node/{nodeId}/wfapi/describe and then retrieve the relevant node log. The exact API availability depends on your installed Jenkins plugins and Pipeline API implementation.
I recommend sending something like this to your LLM: { Then prompt the LLM: Analyze this Jenkins CI/CD failure. Identify:
Do not guess. If the logs are insufficient, say what additional information is needed. Don't send the entire Jenkins consoleText to the LLM unless the logs are small. Instead: Use JSON/Pipeline metadata to find the failed stage. A robust architecture would be: Jenkins So yes: use JSON for "which stage failed?" and logs for "why did it fail?". That combination is much better than giving the entire raw consoleText to an LLM. can you write simple python script for this Sure. Assuming you have a Jenkins Pipeline and want a simple script that: Gets build metadata. One caveat: Jenkins' standard consoleText doesn't reliably contain stage boundaries in a machine-readable way. For a simple first version, we can use the Pipeline stage markers in the console output. import requests JENKINS_URL = "https://jenkins.example.com" USERNAME = "your-username" session = requests.Session() def get_build_info(): def get_pipeline_info(): def get_console_log(): def find_failed_stage(pipeline_info): def extract_stage_logs(console_text, stage_name): def main(): if name == "main": If Jenkins says: Build: FAILURE Stages: the script produces something like: Failed stage: Test ===== FAILED STAGE LOGS =====
FAILED tests/test_payment.py E AssertionError: Expected 200 but got 500 =========================== short test summary info =========================== Process exited with code 1 That's the sort of payload I'd then give to the LLM. One improvement I'd strongly recommend For a real implementation, don't parse stage logs from consoleText with regex. Jenkins Pipeline stages can contain nested steps, parallel stages, retries, post blocks, etc., so the simple regex will eventually break. A better implementation is: Jenkins /wfapi/describe That gives you a much cleaner architecture: { If you tell me which Jenkins version/plugins you're using, I can give you the Python version that uses the actual Pipeline node/stage API to fetch only the failed stage's logs, rather than parsing consoleText. yes nested stages is problem Exactly. In that case, don't try to determine the failed stage by parsing consoleText. Let Jenkins tell you the stage structure through the Pipeline API, then fetch the log for the corresponding Pipeline node. A good approach is: /api/json For example, a nested Pipeline: Build You want the script to identify: Failed stage: Integration rather than trying to regex the console output. One thing to be aware of: wfapi gives you Pipeline/stage structure, but fetching the exact node log is dependent on the Jenkins Pipeline/Blue Ocean-related plugins and their exposed endpoints. So I wouldn't want to give you a Python script based on an endpoint your Jenkins instance doesn't actually expose. If you run this against your Jenkins: curl -u "$JENKINS_USER:$JENKINS_API_TOKEN" and paste the JSON response here (you can remove URLs, usernames, job names, etc.), I can write the exact Python script to: recursively handle nested stages |
No description provided.