问题
I have a DSL groovy script defining a pipeline job. I need to load Jenkinsfile from the workspace. The Jenkinsfile resides in the same folder as that of the groovy script. I am trying to get the path of the groovy script programmatically so that I can use that to figure out the path of the Jenkinsfile and load it using readFileFromWorkspace
. I tried using __FILE__
directive after going through the job-dsl-wiki. But I am getting the following error:
Processing provided DSL script
ERROR: (test_job.groovy, line 3) No such property: absolutePath for class: java.lang.String
Finished: FAILURE
Here is my DSL script
job_name = "my-pipeline-job"
job_path = "${new File(__FILE__).parent.absolutePath}"
jenkinsfile = job_path + "/Jenkinsfile"
pipelineJob(job_name){
description("Jenkins pipeline job")
parameters{
stringParam("MyTestParam", "", "a sample parameter")
}
definition {
cps {
sandbox()
script(readFileFromWorkspace(jenkinsfile))
}
}
}
Is there anything that I am doing wrong here? Really appreciate any help on this.
回答1:
The documentation in the wiki is a little misleading. Here is the working solution.
job_name = "my-pipeline-job"
println "Script: ${ __FILE__}"
println("script directory: ${new File(__FILE__).parent}")
job_path = "${new File(__FILE__).parent}"
jenkinsfile = job_path + "/Jenkinsfile"
pipelineJob(job_name){
description("Jenkins pipeline job")
parameters{
stringParam("MyTestParam", "", "a sample parameter")
}
definition {
cps {
sandbox()
script(readFileFromWorkspace(jenkinsfile))
}
}
}
来源:https://stackoverflow.com/questions/47336502/get-absolute-path-of-the-script-directory-that-is-being-processed-by-job-dsl