8

How to get the absolute path of a file (from workspace) in Jenkins pipeline script (in windows environment)

File locations (The files are checked-out from Git and Jenkinsfile2.nprd will have the groovy pipeline script):

C:/Program Files (x86)/Jenkins/workspace/dev-my-api/my-data-api/my-data-api/pom.xml C:/Program Files (x86)/Jenkins/workspace/dev-my-api/my-data-api/Jenkinsfile2.nprd

Script:

 stages {
        stage('Setup')  {
            steps {
                script {
                  pomPath = findFiles(glob: "**/pom.xml")[0].path
                  env.WORKSPACE = pwd()
                  pomDir = bat(script: "for %%F in ($pomPath) do set dirname=%%~dpF", returnStdout: true).trim()
                  echo "env.WORKSPACE:" + env.WORKSPACE
                  echo "pom file path:" + pomPath
                  echo "pom directory****:" + pomDir
                }
            }
        }
}

Output:

env.WORKSPACE:C:\Program Files (x86)\Jenkins\workspace\dev-my-api
pom file path:my-data-api\pom.xml
pom directory****:C:\Program Files (x86)\Jenkins\workspace\my-data-api>for %F in (my-data-api\pom.xml) do set dirname=%~dpF 

Requried path:

C:/Program Files (x86)/Jenkins/workspace/dev-my-api/my-data-api

How to get the above required path in Jenkins pipeline script without hard coding?

3 Answers 3

0
pomPath = findFiles(glob: "**/$pomFile")[0].path
env.WORKSPACE = pwd()
def projectName = new File(pomPath).parent
baseDir = "${env.WORKSPACE}/$projectName"

I am able to get the required path. But looking for a cleaner solution.

Sign up to request clarification or add additional context in comments.

Comments

0

Try out the following (find pom.xml relative path & get the full path)

 def pomPath = findFiles(glob: "**/pom.xml")[0].path
 echo new File(env.WORKSPACE, pomPath).getParent() +"\pom.xml"

2 Comments

Shouldn't that require script approvals? new File() and File.getParent() both? pomPath will be a FileWrapper instance.
beware of using File object. The CPS groovy executes some portions on master which won't have the file on it if you run on a node. It can be useful for constructing and decomposing paths but beyond that it can yield failures
0

Modified Pipeline Script

pipeline {
    agent any 
    stages {
        stage('Setup')  {
            steps {
                script {
                    // Find the pom.xml file and get its relative path
                    def pomPath = findFiles(glob: "**/pom.xml")[0].path
                    
                    // Get the absolute workspace path
                    def workspacePath = env.WORKSPACE
                    
                    // Combine workspace path and pomPath to get the absolute path
                    def fullPomPath = "${workspacePath}/${pomPath}".replace('\\', '/')
                    
                    // Use the full absolute path to get the directory
                    def pomDir = bat(script: "for %F in (\"${fullPomPath}\") do @echo %~dpF", returnStdout: true).trim()
                    
                    // Remove any trailing new lines and whitespace
                    pomDir = pomDir.replace('\\', '/').trim()
                    
                    echo "Workspace: ${workspacePath}"
                    echo "Pom file path: ${pomPath}"
                    echo "Pom directory: ${pomDir}"
                }
            }
        }
    }
}

The output should now correctly

Workspace: C:/Program Files (x86)/Jenkins/workspace/dev-my-api
Pom file path: my-data-api/pom.xml
Pom directory: C:/Program Files (x86)/Jenkins/workspace/dev-my-api/my-data-api

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.