Jenkins Declarative Pipeline - how to get path to Jenkinsfile in use?

jenkins groovy get workspace path
jenkins get path
jenkins pipeline get cwd
jenkins get absolute path
jenkins pipeline get root directory
jenkins get working dir
jenkins pipeline wait
jenkins groovy get cwd

We have a series of Jenkinsfile scripts which are identical, except each configures an environment item to point to the directory in the SCM checkout that holds the Jenkinsfile. That is used to reference a file in the same directory. The SCM repo has all of these Jenkinsfile scripts in different directories. I see an easy opening to make the pipeline script identical in each case if I could only retrieve the path of the directory containing the Jenkinsfile.

I tried several different things like steps containing

script {
    println __FILE__


script { 
    scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent
    println scriptDir

Neither of which ran (gave non-existent variable in __FILE__ case and permission violation in the second case). I tried "${__FILE__}" and other variants.

I need to use the directory in a steps-sh block so I believe it needs to be in an environment item.

Now, the Jenkins job configuration gives the path to the Jenkins file, but I don't want to have to repeat that to create another environment variable at that level.

Already consulted:

  • Get absolute path of the script directory that is being processed by Job DSL
  • Get absolute path to workspace directory in Jenkins Pipeline plugin
  • How can I reference the Jenkinsfile directory, with Pipeline?
  • as well as many more Google hits and other sites.

Help is appreciated.

If you really need this, you can fetch "${JOB_URL}config.xml" and parse scriptPath.

But that's not a clean solution obviously.

Jenkins pipeline script - become self aware, There are lots of ways to do this. Here are two ways I can think of off the top of my head: steps { println(WORKSPACE) }. or steps { def foo = sh(script: 'pwd',  Pipeline supports two syntaxes, Declarative (introduced in Pipeline 2.5) and Scripted Pipeline.Both of which support building continuous delivery pipelines. Both may be used to define a Pipeline in either the web UI or with a Jenkinsfile, though it’s generally considered a best practice to create a Jenkinsfile and check the file into the source control repository.

Found at another thread:

def currentScriptPath = new File(currentBuild.rawBuild.parent.definition.scriptPath).parent

But it requires to approve a lot of methods.

Getting started with Pipeline, Snippet Generator; Global Variable Reference; Declarative Directive Generator This section describes how to get started with creating your Pipeline project in Jenkins the classic UI is stored by Jenkins itself (within the Jenkins home directory). To configure your Pipeline project to use a Jenkinsfile from source control:. Jenkins Pipeline is the workflow that implements Continuous Delivery pipeline with the Jenkins features, tools and plugins. There are two different ways to create Jenkins pipeline. One is Declarative Pipeline, and another is Scripted Pipeline. In this article, we will see about how to create Jenkins Declarative pipeline.

The simplest way is to put the Jenkinsfile at the root of your repository and get the value of the WORKSPACE environment variable set by Jenkins.

Given that Groovy is just basically Java, you can get the absolute path like so:

new File("{env.WORKSPACE}").absolutePath

Pipeline: Basic Steps, Guided Tour · Jenkins Pipeline · Using Build Tools This step is most useful when used in Declarative Pipeline or with the options to set the stage Note that the build result can only get worse, so you cannot change the result to SUCCESS To delete a specific directory of a workspace wrap the deleteDir step in a dir step. Scroll down and click Pipeline, then click OK at the end of the page to open the Pipeline configuration page (whose General tab is selected). Click the Pipeline tab at the top of the page to scroll down to the Pipeline section. Note: If instead you are defining your Jenkinsfile in source control, follow the instructions in In SCM below.

Declarative Pipeline With Jenkins - DZone, . The steps to build, test, and deliver each application become part of the application itself, stored in a Jenkinsfile. To start though, let’s get familiar with the basic structure of a Declarative Pipeline by creating a simple Pipeline for a Maven-based Java project - the Jenkins JUnit plugin. We’ll create a minimal Declarative Pipeline, add the settings needed to install Maven and the JDK, and finally we’ll actually run Maven to build the plugin.

Using a Jenkinsfile, Not all Pipelines will have these same three stages, but it is a good starting point to syntax highlighting, create a new Jenkinsfile in the root directory of the project. The Declarative Pipeline example above contains the minimum necessary structure to If your job is configured to use a specific JDK, this variable is set to the  As it is a fully-featured programming environment, Scripted Pipeline offers a tremendous amount of flexibility and extensibility to Jenkins users. The Groovy learning-curve isn’t typically desirable for all members of a given team, so Declarative Pipeline was created to offer a simpler and more opinionated syntax for authoring Jenkins Pipeline.

Pipeline: Declarative, validateDeclarativePipeline : Validate a file containing a Declarative Pipeline. Checks if the given file (as relative path to current directory) contains a valid  A Pipeline is a user-defined model of a CD pipeline. A Pipeline’s code defines your entire build process, which typically includes stages for building an application, testing it and then delivering it. Also, a pipeline block is a key part of Declarative Pipeline syntax. A node is a machine which is part of the Jenkins environment and is

  • Always keep your Jenkinsfile at the root of your directory. Then use 'branch' conditions (when { branch 'develop' }) to get your configuration per environment.
  • That doesn't work. One SCM repo contains numerous independent pipelines. I'm not going to fracture the repo into 100 different ones just so I can get an identical Jenkinsfiles in each case. It is really surprising that Jenkins Pipeline doesn't give simple access to this information.
  • sparse checkout?
  • @KevinBuchs I'm in the same situation. Did the find a solution to you question?
  • @keda - no. I hope to get back to this, because there must be a way to do it, but I have to dig into the Java objects, etc.
  • This looks promising. I can see the job config is properly referenced via JOB_URL. I can see the ScriptPath in the Config when viewing from an authenticated browser session. I can't http get the file from a Pipeline script, because we have authentication on our Jenkins. I don't know how to get that file from within the Pipeline groovy script. I need to determine how get at that information another way. Given the job configuration data structure, I have to believe that that information must be loaded into the JVM in memory, and with loading the right objects in Groovy, I can get at this.
  • Not in my case. We have one SCM repo with numerous Jenkinsfiles in different directories. The location of the Jenkinsfile is given when the Jenkins pipeline is created.
  • I would suggest restructuring your setup if you can.
  • @KevinBuchs how is the location given to Jenkins ??
  • When a pipeline job is created, with the script (Jenkinsfile) coming from SCM, you specify the script path as part of the job configuration. Screen shot:!AgKA2HL-SveIgYJEJIfTqbccHIBFFw
  • Highly recommend making life easier for yourself and just specify Jenkinsfile without the path and then you can get the absolute path by accessing the environment variable Jenkins sets for all job {env.WORKSPACE}.