在奴隶上运行jenkins DSL脚本

时间:2015-11-01 09:19:31

标签: jenkins master-slave jenkins-build-flow

我有一个Build Flow项目,它有一个DSL脚本来配置流程。

这是我的剧本:

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) 
  }
)

在我的工作配置中,我检查了Restrict where this project can be run并给出了正确的从属标签。

我的工作失败并出现以下错误:

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

我发现DSL脚本正在主节点而不是从节点上运行。

如何在奴隶上运行DSL? (或者至少在奴隶上执行脚本)

由于

1 个答案:

答案 0 :(得分:1)

您应该为当前频道创建FilePath并创建启动器以启动您的脚本。以下内容适用于您。

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()