Search code examples
jenkinsmaster-slavejenkins-build-flow

run jenkins DSL script on slave


I have a Build Flow project that has a DSL script to configure the flow.

This is my script:

def envVars = build.getEnvironment(listener)
def revision = envVars.get("GIT_COMMIT")
def workspace = envVars.get("WORKSPACE")
println "env vars: " + envVars
println "workspace: " + workspace
println "revision: " + revision
def command = workspace+"""/scripts/my_script.sh"""
def proc = command.execute()
proc.waitFor()
println "return code: ${proc.exitValue()}"
println "stderr: ${proc.err.text}"
println "stdout: ${proc.in.text}" 

parallel (
  { 
    build("job1", git_branch: revision)
  },
  { 
    build("job2", git_branch: revision) 
  },
  { 
    build("job3", git_branch: revision) 
  }
)

In my job configuration I have checked the Restrict where this project can be run and gave the correct slave label.

My job fails with the following error:

ERROR: Failed to run DSL Script
java.io.IOException:
  Cannot run program "/home/jenkins/workspace/my-flow-job/scripts/my_script.sh":
    error=2, No such file or directory

i have found out that the DSL script is running on the master node instead of the slave node.

How can i run the DSL on the slave? (or at least execute the script on the slave)

Thanks


Solution

  • You should create a FilePath for the current channel and create a launcher to launch your script. Something like the following should work for you.

    def envVars = build.getEnvironment(listener)
    def workspace = envVars.get("WORKSPACE")
    def command = workspace+"""/scripts/my_script.sh"""
    def channel = build.workspace.channel
    def fp = build.workspace.toString()
    def workspacePath = new hudson.FilePath(channel, fp)
    
    def launcher = workspacePath.createLauncher(hudson.model.TaskListener.NULL);
    def starter = launcher.launch().pwd(workspacePath).stdout(out).stderr(out).cmdAsSingleString(command)
    starter.join()