0

I noticed JSL scripts get executed only on Jenkins Master, is it possible to run JSL script on Jenkins Agents?

I have multiple stages in my Pipeline and I wish to run those stages on different Jenkins Agent nodes.

My primary motivation for using JSL is end-to-end Pipeline testability during development with "replay", where I can modify Jenkinsfile as well as scripts from JSL.

This is a snippet of my Pipeline --

pipeline {
   agent { label 'scdc-generic-w10x64' }
   options {
      timestamps()
   }
   stages {
      stage('Log ip') {
        steps {
            script {
               bat "ipconfig -all"   // *** Gets executed on Jenkins Agent ***
               foo = jsl.foo.new(this) // foo is a Groovy class in JSL
               foo.logIpAddress()   // *** Gets executed on Jenkins Master ***
            }
         }
      }
   }
   post {
      always {
         cleanWs()
      }
   }
}
Raviraj
  • 1
  • 3

2 Answers2

0

Yes you can execute on different agents for differemt stages by using agent{label <Your agent name>}. You also need to ensure that your shared-library is present in the agent also.
Note: everything will be executed on that agent in that stage

stage('Log ip') {
agent { label "<Your agent name>" }
        steps {
            script {
               bat "ipconfig -all"  
               foo = jsl.foo.new(this) 
               foo.logIpAddress()   
            }
         }
      }
Altaf
  • 2,838
  • 1
  • 16
  • 8
  • This is possibly misleading. The Pipeline script itself, technically, always executes on the master node. But some steps actually execute an action on the agent, such as `bat`, `sh`, `readFile`, ... That's why your script should not try to read or write files on the agent using the Groovy API, but use the built-ins `readFile`, `writeFile`, for instance – Patrice M. Jul 13 '21 at 14:31
  • @Altaf, I have tried mentioning Agent within the stage, that doesn't help. – Raviraj Jul 13 '21 at 16:06
  • Thanks, @PatriceM.! Do I need to write scripts on Agent node using writeFile and then execute them? Is that the only way to go about it? – Raviraj Jul 13 '21 at 16:10
  • @Raviraj I have provided a possibility how you can execute stage on the angents..Ofcourse you can writeFile/readfile operations to suffice your needs – Altaf Jul 13 '21 at 17:27
0

I posted this question on JenkinsUser google group as well. JSL script gets executed only on Jenkins Master, that's by design.

More details on -- https://groups.google.com/g/jenkinsci-users/c/t66PLaAvcgw/m/Ruo97K2AAgAJ

Raviraj
  • 1
  • 3