Last active
May 14, 2020 07:39
-
-
Save brujoand/905a84defc17ff79f3f9504236e4f80c to your computer and use it in GitHub Desktop.
Crawl jenkins project structure through the api to find all builds started after timestamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# To find all builds started within three hours of current time | |
# ./recentBuilds.sh "$(( $(date +%s) - ( 60 * 60 * 3) ))" | |
cutoff=$1 | |
if [[ -z $cutoff ]]; then | |
echo "Please provide a unix timestamp as a cutoff time" | |
exit 1 | |
fi | |
cutoff="${cutoff}000" | |
api_url='http://your.jenkins.com' | |
job_api_filter='/api/json?tree=duration,timestamp,building,url' | |
folder_api_filter='/api/json?tree=jobs\[url\]' | |
jq_get_job='if .building == "true" then "\(.url)" elif .duration + .timestamp >= '"$cutoff"' then "\(.url)" else "null" end' | |
jq_get_job_urls='.jobs | map(select(._class|test(".*Job")) | .url ) | .[]' | |
jq_get_folder_urls='.jobs | map ( select(._class|test("(Project$|Folder$)+")) | .url) | .[]' | |
check_job() { | |
local job_url=$1 | |
#echo "Checking job ${job_url}/lastBuild${job_api_filter}" | |
json_response=$(curl --fail -s "${job_url}/lastBuild${job_api_filter}") | |
[[ $? -ne 0 ]] && return | |
jq -r "$jq_get_job" <<< "$json_response" 2>/dev/null | |
} | |
check_folder() { | |
local folder_url=$1 | |
#echo "checking folder ${folder_url}${folder_api_filter}" | |
json_response=$(curl -s "${folder_url}${folder_api_filter}") | |
while IFS= read -r job_url; do | |
[[ -z "$job_url" ]] && continue | |
check_job "$job_url" | |
done <<< "$(jq -r "$jq_get_job_urls" <<< "$json_response" 2>/dev/null)" | grep -v "^null$" | |
while IFS= read -r folder_url; do | |
[[ -z "$folder_url" ]] && continue | |
check_folder "$folder_url" | |
done <<< "$(jq -r "$jq_get_folder_urls" <<< "$json_response" 2>/dev/null )" | |
} | |
check_folder "$api_url" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment