wanderors
wanderors

Reputation: 2292

Jenkins pipeline how to change to another folder

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.

Upvotes: 154

Views: 293239

Answers (4)

dugu YuZhiBo
dugu YuZhiBo

Reputation: 11

I think you could create a new directory for each git repository in your current working directory, you could do it like this

stage('app') {
        steps {
            dir('./project-app') {
                // This creates a directory named project-app in the current directory and initializes this directory as a git repository 
                // and as the current directory for sh commands.
                checkout scmGit(branches: [[name: "master"]], extensions: [], userRemoteConfigs: [[credentialsId: 'user', url: 'http://xxxxxx.git']])
                sh 'mvn -U -DskipTests clean package'
            }
        }
    }

Upvotes: 1

tsl0922
tsl0922

Reputation: 2855

You can use the dir step, example:

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

The folder can be relative or absolute path.

Upvotes: 265

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"
} 

Upvotes: 67

Raj Bangarwa
Raj Bangarwa

Reputation: 544

Use WORKSPACE environment variable to change workspace directory.

If doing using Jenkinsfile, use following code :

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

Upvotes: 39

Related Questions