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

External jars in shared libraries are always executed on master

    Details

    • Similar Issues:

      Description

      We are using a shared library to hold some pipeline logic. Recently, we needed this logic in some other place as well, so we decided to create a Java library with the common code. In the shared library, we are using this library via @Grab (the artifact is hosted on an internal repo).

      Now we encountered a weird bug in our pipeline. In our Java library, we are doing some file modification. This works perfectly on master, however does not seem to work when the code is executed on a slave node. We confirmed that the library code is always executed on master (we executed a simple ifconfig command -> always showed IP of the master).

      Our pipeline code is structured like this:

      ...
      foo() // Executed on master
      node ('node-name') {
          bar() // Executed on node
          libraryCall() // Executed on master
          baz() // Executed on node
      }
      ...

      This is obviously very problematic and we did not find a solution for this kind of behaviour.

        Attachments

          Activity

          Hide
          abayer Andrew Bayer added a comment -

          Yes, this is as designed - Pipeline Groovy execution is all within the master JVM. Steps like sh run within a node block do things like run a process on the indicated node, but the Groovy execution is still on the master. If you want to run Groovy code on the agent, you'd be best off doing something like the below, with your Groovy script in the shared library at resources/foo.groovy (rather than vars/foo.groovy):

          node {
            writeFile(file: "script.groovy", libraryResource("foo.groovy"))
            sh "groovy script.groovy"
          }
          
          Show
          abayer Andrew Bayer added a comment - Yes, this is as designed - Pipeline Groovy execution is all within the master JVM. Steps like sh run within a node block do things like run a process on the indicated node , but the Groovy execution is still on the master. If you want to run Groovy code on the agent, you'd be best off doing something like the below, with your Groovy script in the shared library at resources/foo.groovy (rather than vars/foo.groovy ): node { writeFile(file: "script.groovy" , libraryResource( "foo.groovy" )) sh "groovy script.groovy" }
          Hide
          kbg Kishore Kumar added a comment -

          Tried this and this approve doesn't seem to work. Is there any specific setup that need to done prior to trying this approach.

          Show
          kbg Kishore Kumar added a comment - Tried this and this approve doesn't seem to work. Is there any specific setup that need to done prior to trying this approach.
          Hide
          pukseli Akseli Arvaja added a comment - - edited

          Writefile needs both parameters as named. Try: 

          writeFile(file:"foo.groovy", text: libraryResource("bar.groovy"))
          
          sh "groovy foo.groovy"

           Worked for me

          Show
          pukseli Akseli Arvaja added a comment - - edited Writefile needs both parameters as named. Try:  writeFile(file: "foo.groovy" , text: libraryResource( "bar.groovy" )) sh "groovy foo.groovy"  Worked for me

            People

            • Assignee:
              Unassigned
              Reporter:
              sis Simon Schiller
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: