Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-40997

Add way to get locked resource name in pipeline

    XMLWordPrintable

Details

    Description

      https://issues.jenkins-ci.org/browse/JENKINS-34268 was closed implementing the possibility to lock a resources using a label in pipelines.
      But there is no syntax for knowing what the actually locked resource name is when using a pipeline and locking on a label.
      Specifically the proposed syntax from https://github.com/jenkinsci/lockable-resources-plugin/pull/36 : ", variable: 'MY_VAR'" is missing.

      Our use case is that we wan't to specify a label containing node names for legacy builders that can't run the slave.jar (java7) anymore. We want to lock a resource (host name) from the label and then ssh to that machine and execute some build steps. This works with the FreestyleJob functionality of "Reserved resources variable name".

      Solution

      It is now possible to access the locked resource via an configurable environment variable.

      lock(label: 'some_resource', variable: 'MY_VAR') {
        echo env.MY_VAR
      }
      

       

       

      Attachments

        Issue Links

          Activity

            f_julian Julian F. added a comment - Implemented with https://github.com/jenkinsci/lockable-resources-plugin/pull/49

            Released as 2.2

            amuniz Antonio Muñiz added a comment - Released as 2.2
            avidviewer Brandon Saunders added a comment - - edited

            I'm appearing to hit an issue where a released resource does not get an update on the environment variable once the lock is released.  See the following code (with only two resources in the automation-accounts label)

             

            node {
              parallel (
                "p1": {
                  lock(label: 'automation-accounts', variable: 'ACCOUNTS_VAR', quantity: 1) {
                    echo "A $env.ACCOUNTS_VAR"
                    sleep 4
                         }
                },
                "p2": {
                  lock(label: 'automation-accounts', variable: 'ACCOUNTS_VAR', quantity: 1) {
                    echo "B $env.ACCOUNTS_VAR"
                    sleep 2
                         }
                },
                "p3": {
                  lock(label: 'automation-accounts', variable: 'ACCOUNTS_VAR', quantity: 1) {
                    echo "C $env.ACCOUNTS_VAR"
                         }
                     }
              )
            }

             

            In the log I get the following (note "C null"):
            [Pipeline] {
            [Pipeline] parallel
            [Pipeline] [p1] { (Branch: p1)
            [Pipeline] [p2] { (Branch: p2)
            [Pipeline] [p3] { (Branch: p3)
            [Pipeline] [p1] lock
            [p1] Trying to acquire lock on [Label: automation-accounts, Quantity: 1]
            [p1] Lock acquired on [Label: automation-accounts, Quantity: 1]
            [Pipeline] [p1] {
            [Pipeline] [p2] lock
            [p2] Trying to acquire lock on [Label: automation-accounts, Quantity: 1]
            [p2] Lock acquired on [Label: automation-accounts, Quantity: 1]
            [Pipeline] [p2] {
            [Pipeline] [p3] lock
            [p3] Trying to acquire lock on [Label: automation-accounts, Quantity: 1]
            [p3] Found 0 available resource(s). Waiting for correct amount: 1.
            [p3] [Label: automation-accounts, Quantity: 1] is locked, waiting...
            [Pipeline] [p1] echo
            [p1] A <Account1>
            [Pipeline] [p1] sleep
            [p1] Sleeping for 4 sec
            [Pipeline] [p2] echo
            [p2] B <Account2>
            [Pipeline] [p2] sleep
            [p2] Sleeping for 2 sec
            [p3] Lock acquired on [Label: automation-accounts, Quantity: 1]
            [Pipeline] [p2] }
            [p2] Lock released on resource [Label: automation-accounts, Quantity: 1]
            [Pipeline] [p3] {
            [Pipeline] [p2] // lock
            [Pipeline] [p2] }
            [Pipeline] [p3] echo
            [p3] C null
            [Pipeline] [p3] }
            [p3] Lock released on resource [Label: automation-accounts, Quantity: 1]
            [Pipeline] [p3] // lock
            [Pipeline] [p3] }
            [Pipeline] [p1] }
            [p1] Lock released on resource [Label: automation-accounts, Quantity: 1]
            [Pipeline] [p1] // lock
            [Pipeline] [p1] }
            [Pipeline] // parallel
            [Pipeline] }
            [Pipeline] // node
            [Pipeline] End of Pipeline

            Expected Result:
            I should get "C <Account2>

            Perhaps I'm calling it wrong though?

            avidviewer Brandon Saunders added a comment - - edited I'm appearing to hit an issue where a released resource does not get an update on the environment variable once the lock is released.  See the following code (with only two resources in the automation-accounts label)   node {   parallel (     "p1": {       lock(label: 'automation-accounts', variable: 'ACCOUNTS_VAR', quantity: 1) {         echo "A $env.ACCOUNTS_VAR"         sleep 4              }     },     "p2": {       lock(label: 'automation-accounts', variable: 'ACCOUNTS_VAR', quantity: 1) {         echo "B $env.ACCOUNTS_VAR"         sleep 2              }     },     "p3": {       lock(label: 'automation-accounts', variable: 'ACCOUNTS_VAR', quantity: 1) {          echo "C $env.ACCOUNTS_VAR"              }          }   ) }   In the log I get the following (note "C null"): [Pipeline] { [Pipeline] parallel [Pipeline] [p1] { (Branch: p1) [Pipeline] [p2] { (Branch: p2) [Pipeline] [p3] { (Branch: p3) [Pipeline] [p1] lock [p1] Trying to acquire lock on [Label: automation-accounts, Quantity: 1] [p1] Lock acquired on [Label: automation-accounts, Quantity: 1] [Pipeline] [p1] { [Pipeline] [p2] lock [p2] Trying to acquire lock on [Label: automation-accounts, Quantity: 1] [p2] Lock acquired on [Label: automation-accounts, Quantity: 1] [Pipeline] [p2] { [Pipeline] [p3] lock [p3] Trying to acquire lock on [Label: automation-accounts, Quantity: 1] [p3] Found 0 available resource(s). Waiting for correct amount: 1. [p3] [Label: automation-accounts, Quantity: 1] is locked, waiting... [Pipeline] [p1] echo [p1] A <Account1> [Pipeline] [p1] sleep [p1] Sleeping for 4 sec [Pipeline] [p2] echo [p2] B <Account2> [Pipeline] [p2] sleep [p2] Sleeping for 2 sec [p3] Lock acquired on [Label: automation-accounts, Quantity: 1] [Pipeline] [p2] } [p2] Lock released on resource [Label: automation-accounts, Quantity: 1] [Pipeline] [p3] { [Pipeline] [p2] // lock [Pipeline] [p2] } [Pipeline] [p3] echo [p3] C null [Pipeline] [p3] } [p3] Lock released on resource [Label: automation-accounts, Quantity: 1] [Pipeline] [p3] // lock [Pipeline] [p3] } [Pipeline] [p1] } [p1] Lock released on resource [Label: automation-accounts, Quantity: 1] [Pipeline] [p1] // lock [Pipeline] [p1] } [Pipeline] // parallel [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Expected Result : I should get "C <Account2> Perhaps I'm calling it wrong though?

            Hi All,

            Same behavior as Brandon's happens to me:

            I have several jobs requiring a specific resource, and I only have 4 of them property labeled.

            If at the moment of lock there is no resources available pipeline will wait until resource gets free

            Once is available, pipeline resumes and effectively reserves the resource but variable does not get filled. (Label value), instead gets null value.

            Regards

            Alfredo

            acastrocuartero Alfredo Castro added a comment - Hi All, Same behavior as Brandon's happens to me: I have several jobs requiring a specific resource, and I only have 4 of them property labeled. If at the moment of lock there is no resources available pipeline will wait until resource gets free Once is available, pipeline resumes and effectively reserves the resource but variable does not get filled. (Label value), instead gets null value. Regards Alfredo
            jbogers Jeroen Bogers added a comment - - edited

            EDIT: See my comment on JENKINS-50176

            jbogers Jeroen Bogers added a comment - - edited EDIT: See my comment on JENKINS-50176

            People

              f_julian Julian F.
              fkykko Staffan Forsell
              Votes:
              24 Vote for this issue
              Watchers:
              27 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: