Get absolute path of the script directory that is being processed by Job DSL

夙愿已清 提交于 2019-12-11 05:00:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!