Jenkins pipeline how to change to another folder

JenkinsGroovyJenkins Pipeline

Jenkins Problem Overview


Currently i am using Jenkins pipeline script.

For running one command, I need to access a folder outside its workspace directory.

I tried sh "cd $workspace/", but it returned current workspace folder.

How I can change to root workspace directory and then cd to another folder. Please help.

Jenkins Solutions


Solution 1 - Jenkins

You can use the dir step, example:

dir("folder") {
    sh "pwd"
}

The folder can be relative or absolute path.

Solution 2 - Jenkins

The dir wrapper can wrap, any other step, and it all works inside a steps block, for example:

steps {
    sh "pwd"
    dir('your-sub-directory') {
      sh "pwd"
    }
    sh "pwd"
} 

Solution 3 - Jenkins

Use WORKSPACE environment variable to change workspace directory.

If doing using Jenkinsfile, use following code :

dir("${env.WORKSPACE}/aQA"){
    sh "pwd"
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwanderorsView Question on Stackoverflow
Solution 1 - Jenkinstsl0922View Answer on Stackoverflow
Solution 2 - JenkinsGonzalo Robert DíazView Answer on Stackoverflow
Solution 3 - JenkinsRaj BangarwaView Answer on Stackoverflow