You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2017/01/18 16:44:37 UTC

svn commit: r17880 - in /dev/jclouds/jenkins: ./ cleanup-jenkins.sh

Author: nacx
Date: Wed Jan 18 16:44:37 2017
New Revision: 17880

Log:
Added Jenkins cleanup script

Added:
    dev/jclouds/jenkins/
    dev/jclouds/jenkins/cleanup-jenkins.sh   (with props)

Added: dev/jclouds/jenkins/cleanup-jenkins.sh
==============================================================================
--- dev/jclouds/jenkins/cleanup-jenkins.sh (added)
+++ dev/jclouds/jenkins/cleanup-jenkins.sh Wed Jan 18 16:44:37 2017
@@ -0,0 +1,68 @@
+#!/bin/bash
+
+# BEFORE RUNNING
+# 1. Download the Jenkins CLI from the Jenkins server. Go to Manage Jenkins > Jenkins CLI
+# 2. Add your SSH public key to your Jenkins user account.
+# 3. Install JQ (https://stedolan.github.io/jq/)
+
+function usage {
+    echo "Usage: ${0} -s <jenkins url> -u <username> -p <password> -n <# builds to keep> [-a] [-v <view1> [-v <viewN>]]"
+    exit 1
+}
+
+[[ $# -gt 0 ]] || usage
+
+while getopts "s:u:p:n:av:" OPT; do
+    case ${OPT} in
+        s) JENKINS=${OPTARG} ;;
+        u) USER=${OPTARG} ;;
+        p) PASS=${OPTARG} ;;
+        n) BUILDS_TO_KEEP=${OPTARG} ;;
+	a) ALL_JOBS=1 ;;
+	v) VIEWS+=("${OPTARG}") ;;
+        *) usage ;;
+    esac
+done
+
+[[ "${JENKINS}" == "" ]] && usage
+[[ "${USER}" == "" ]] && usage
+[[ "${PASS}" == "" ]] && usage
+[[ "${BUILDS_TO_KEEP}" == "" ]] && usage
+[[ "${ALL_JOBS}" == "" ]] && [[ "${VIEWS}" == "" ]] && usage
+
+function cleanup_job {
+    echo "Getting builds for ${1}..."
+    LAST_BUILD=`curl -s -f -u ${USER}:${PASS} ${JENKINS}/job/${1// /%20}/lastBuild/api/json 2>/dev/null`
+    if [[ $? -ne 0 ]]; then
+        echo "No builds found"
+    else
+        BUILD=`echo "${LAST_BUILD}" | jq '.number'`
+        if [[ "${BUILD}" != "" ]]; then
+            [[ ${BUILD} -le 0 ]] && BUILD=1
+            LAST=$((BUILD-BUILDS_TO_KEEP))
+            if [[ ${LAST} -le 0 ]]; then
+                echo "No builds to remove"
+            else
+                echo "Deleting builds older than #${LAST}..."
+                java -jar jenkins-cli.jar -s ${JENKINS} delete-builds "${1}" 0-${LAST}
+            fi
+        else
+            echo "No builds found"
+        fi
+    fi
+}
+
+if [[ "${ALL_JOBS}" != "" ]]; then
+    echo "Processing all jobs..."
+    java -jar jenkins-cli.jar -s ${JENKINS} list-jobs | while IFS= read -r JOB; do
+        cleanup_job ${JOB}
+    done
+else
+    for V in "${VIEWS[@]}"; do
+        echo "Processing jobs in view ${V}..."
+        java -jar jenkins-cli.jar -s ${JENKINS} list-jobs "${V}" | while IFS= read -r JOB; do
+            cleanup_job ${JOB}
+        done
+    done
+fi
+

Propchange: dev/jclouds/jenkins/cleanup-jenkins.sh
------------------------------------------------------------------------------
    svn:eol-style = native