6

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.

Datz
  • 3,156
  • 3
  • 22
  • 50
  • can you explain your requirement a bit? Is your purpose to access this file for build or access it as a string? – Sagar Apr 25 '18 at 07:11
  • @Sagar as I wrote: "I want to get the string..." I need it to make some decisions in executing the pipeline. – Datz Apr 25 '18 at 09:53

3 Answers3

6

You can get a script path this way

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

Note that you have to approve all these methods.

Vitalii Vitrenko
  • 9,763
  • 4
  • 43
  • 62
1

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

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • I selected `@Vitalii Vitrenko` answer as he was first. +1 for explaining how to access build parameters. – Datz Apr 27 '18 at 06:04
0

Here my working alternative solution:

def currentAbsoluteDir = sh(returnStdout: true, script: "pwd")

if (currentAbsoluteDir) {
  currentAbsoluteDir = currentAbsoluteDir.replaceAll("\\n", "");
}
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74