Jenkins queue script

Below is a Bash Shell script that creates a queue and batch operation by importing a list from .csv file. The data from the .csv file is imported and parsed into an array and is iterated through. The script provides a skeleton for creating batched queued operations. This example kicks off Jenkins jobs and waits for the job to complete.

#!/bin/bash
 runInstances=()
 runningBatch=()
 
 #Function used to parse .csv file data row into array. (stores each line) 
 function GetRunInstances(){
     fileContent=`cat file_name.csv|sed '1d'|tr "\r" " "`
     for dataRow in $fileContent
     do
        # File validation goes here (if any)
        runInstances+=("$dataRow")
    done
 }
 #Function to create queue/batch, size of the queue is set by QUEUE_SIZE
 function CreateBatches(){
    index=0
    currInstance=0
    runningInstance=0
    while [ ${#runInstances[@]} -gt 0 ] || [ $runningInstance -gt 0 ]; do
        PollStatuses
        if [ $runningInstance -lt $QUEUE_SIZE ] && [ ${#runInstances[@]} -ge 1 ]; then
            ProcessBatch ${runInstances[$currInstance]}
            unset runInstances[$currInstance]
            currInstance=$((currInstance+1))
            runningInstance=$((runningInstance+1))
            echo "Number of instances remaining:${#runInstances[@]}"
        else
            echo "Sleeping for 240 sec ..."
            sleep 240
        fi
        echo "Number of running instances=$runningInstance"
    done
 }
 #Processes the batch can be replaced to do something else, in this example we start another Jenkins job
 function ProcessBatch(){
    IFS=', ' read -r -a instance <<< "$1"
    _COL1="${instance[0]}"
    _COL2="${instance[1]}"
    _COL3="${instance[2]}"
    echo "Processing instance:$_COL1-$_COL2"
    BASE_BUILDURL="https://jenkins.job.url"
    URL="$BASE_BUILDURL/buildWithParameters?$PARAM1=$_COL1&PARAM2=$_COL2&PARAM3=$_COL3"
    echo $URL
    
    # Kick off a build
    curl -s --data '' --user jenkins_usernm:jenkins_auth_tkn $URL
    sleep 20
    latestBuildUrl="$BASE_BUILDURL/lastBuild/api/json"
    buildNumber=$(curl -s --user jenkins_usernm:jenkins_auth_tkn $latestBuildUrl | jq -r '.number')
    echo "Starting Buildnumber=$buildNumber"
    runningBatch+=($buildNumber)   
 }
 #Optional function use to query the status of another Jenkins job
 function PollStatuses(){
    echo "Checking statuses of running batch..."
    for i in "${!runningBatch[@]}"
    do
        if [ $i ]; then
            queryURL="$BASE_BUILDURL/${runningBatch[$i]}/api/json"
            queryResult=$(curl -s --user jenkins_usernm:jenkins_auth_tkn $queryURL)
            isBuilding=$(echo $queryResult | jq -r '.building')
            result=$(echo $queryResult | jq -r '.result')
            
            if [ $isBuilding == "false" ]; then
                dt=$(date '+%d/%m/%Y %H:%M:%S');
                if [ $result == "SUCCESS" ]; then
                    echo "[$dt] Buildnumber=${runningBatch[$i]} completed."
                else
                    echo "[$dt] Buildnumber=${runningBatch[$i]} failed."
            fi
            
            # remove build from runInstance queue so that a new build can start.
            if [ $runningInstance -ge 1 ]; then
                runningInstance=$((runningInstance-1))
            fi
            # remove build from the running instance batch since build has stopped.
                unset runningBatch[$i]
            else
                echo "Build is still in progress: $BASE_BUILDURL/${runningBatch[$i]}"
            fi
        fi
    done
 }
 #Start of the script...
 QUEUE_SIZE=3
 echo "Max size of queue:$QUEUE_SIZE"
 GetRunInstances
 echo "Number of instances:${#runInstances[@]}"
  
 if [ ${#runInstances[@]} -gt 0 ]; then
    CreateBatches
 fi

Leave a Reply

Your email address will not be published. Required fields are marked *