How to get the pipeline configuration field 'Script Path' when executing the Jenkinsfile?

旧巷老猫 提交于 2019-12-13 16:31:20

问题


How can I get the content of the pipeline configuration field 'Script Path' in Jenkins from the Jenkinsfile (groovy)?

In this example: I want to get the string 'Apps/mq-logger/Jenkinsfile' when executing the Jenkinsfile itself.


回答1:


You can get a script path this way

def scriptPath = currentBuild.rawBuild.parent.definition.scriptPath

Note that you have to approve all these methods.




回答2:


If you want this string to be customizable during the build time, you can create a parameterized build as follows:

When you want to access this in your pipeline script by ${FileParameter} as follows:

pipeline {
 stages {
        stage('Perform verification') {
            steps {
                script {
                   echo "${FileParameter}"
                }
            }
        }
 }

If you don't want it to be customizable, then you can use global var. But if your target is to just get string form Script Path, then you have to use @Vitalii Vitrenko answer to use:

def scriptPath = currentBuild.rawBuild.parent.definition.scriptPath

and approve necessary permissions



来源:https://stackoverflow.com/questions/50016029/how-to-get-the-pipeline-configuration-field-script-path-when-executing-the-jen

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