Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

enter image description here

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

like image 988
Datz Avatar asked Apr 25 '18 07:04

Datz


2 Answers

You can get a script path this way

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

Note that you have to approve all these methods.

like image 125
Vitalii Vitrenko Avatar answered Sep 25 '22 23:09

Vitalii Vitrenko


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

parameterized build

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

like image 35
Sagar Avatar answered Sep 25 '22 23:09

Sagar