0

I have a following method to give available drive for windows in Shared library getAvailableDrive.groovy

#!groovy

def call(){
    def drivesInfo=bat(script: '@echo off && fsutil fsinfo drives', returnStdout: true).trim()
    usedDrives=drivesInfo.substring(8).tokenize(" ")
    def drivesList=[]
    for(alpha = 'A'; alpha <= 'Z'; ++alpha){
        drivesList.add("${alpha}:\\")
    }
    usedDrives.each{
        if(drivesList.contains("${it}"))
            drivesList.remove("${it}")
    }
    if(0==usedDrives.size())
        throw new Exception("No unused Drive/s Found!")
    return drivesList[0].take(2)
}

We have single windows node with executers 10, When multiple pipelines run at a same time, sometimes getAvailableDrive() gives same value to different pipeline. I wanted pipelines to use getAvailableDrive() in sequence. Any input on resolving this issue would be very useful

R_SS
  • 317
  • 5
  • 21

1 Answers1

1

An option might be to "promote" getAvailableDrive.groovy to a pipeline and move it to a separate Jenkins Job. This allows you to set pipeline { options { disableConcurrentBuilds() } ... } and only allow one running at a time.

To trigger the new Job, from another running pipeline, you can use the build(...) command. This does require you to save the (return) information external to the pipeline OR somewhat more "dirty": use the Jobs's Build/Run description via the return of the build(...) command (an RunWrapper Object). You can set the new Job's Build/Run Description via currentBuild.description = '...'

I am unfamiliar with the reason why you need the specific drives; but maybe another storage solution (a Repo (Nexus/Artifactory) or something like S3) would serve you better in the long run? If you need the isolation; maybe combine it with dynamic agents via Docker or Kubernetes

Sir.Chefcoq
  • 330
  • 1
  • 7