Skip to content

Instantly share code, notes, and snippets.

@ojacques
Last active July 5, 2021 17:53
Show Gist options
  • Save ojacques/ad57715c962ff4863cbfe65775bd658f to your computer and use it in GitHub Desktop.
Save ojacques/ad57715c962ff4863cbfe65775bd658f to your computer and use it in GitHub Desktop.
Jenkins: find build details executed on a given agent, at a given time

Scenario

Let's say that you have an issue on a given Jenkins agent at a given time, but you don't know which build can cause it. Here is how to find out which builds where running on that agent, at that time.

In this case, the agent is an AWS EC2 VM, with a well known instance ID (in this case, i-02d41abc1a375089d).

This works with Jenkins organization folders.

1. find all builds executed on the agent

from $JENKINSHOME/jobs:

find . -name "build.xml" -exec grep "i-02d41abc1a375089d" {} \; -exec ./deslash.sh {} ";"

with deslash.sh:

#!/bin/bash
cp "$1" /tmp/"${1//\//_}"

The result is a list of build.xml files, in your /tmp directory.

2. Find all builds running during the event

Then, from /tmp, run: find . -name "*build.xml" -exec ./check.sh {} ";"

with exec.sh:

#!/bin/bash

EVENTTIME=1625489553
STARTTIMEMS=$(grep -oPm1 "(?<=<startTime>)[^<]+" "$1")
DURATIONMS=$(grep -oPm1 "(?<=<duration>)[^<]+" "$1")

let STARTTIME=STARTTIMEMS/1000
let DURATION=DURATIONMS/1000
let ENDTIME=$(( $STARTTIME + $DURATION ))

if [ "$STARTTIME" -lt "$EVENTTIME" ] && [ "$ENDTIME" -gt "$EVENTTIME" ]; then
  echo "Start: $STARTTIME - end: $ENDTIME: Job occured during the event: $1";
fi

Note: 1625489553 here is the timestamp of the event (Epoch time).

Result:

Start: 1625489504 - end: 1625492344, duration: 2840: Job occured during the event: ./._ABC-Automation_jobs_datahub-documentation.edfgeb_branches_master_builds_580_build.xml
Start: 1625485473 - end: 1625496933, duration: 11460: Job occured during the event: ./._tyu-policy-art_jobs_policy_branches_master_builds_229_build.xml
Start: 1625483101 - end: 1625492458, duration: 9357: Job occured during the event: ./._CustomerEngagement_jobs_uiiuaz_branches_master_builds_4500_build.xml
Start: 1625467385 - end: 1625489851, duration: 22466: Job occured during the event: ./._aozia_jobs_test-database-upgrade_branches_master_builds_512_build.xml
Start: 1625489112 - end: 1625490185, duration: 1073: Job occured during the event: ./._ABC-Automation_jobs_metadata-configurator.omdm4g_branches_master_builds_867_build.xml
Start: 1625487048 - end: 1625496789, duration: 9741: Job occured during the event: ./._tyu-policy-art_jobs_policy-db-load_branches_master_builds_294_build.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment