Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 779
wanderors Avatar asked Sep 17 '18 17:09

wanderors


People also ask

What does DIR do in Jenkins pipeline?

dir : Change current directory Change current directory. Any step inside the dir block will use this directory as current and any relative path will use it as base path.

How do I change groovy directory?

def cmd=new CommandShell() println cmd. execute("cd /d c:\\") println cmd. execute("dir") // Will be the dir of c:\ I wrote a groovy class like this once, it's a lot of experimenting and your instance can be trashed by commands like "exit" but it's possible.

How do I create a folder in Jenkins pipeline?

Step #1: From within the desired folder, click “New Item” to create a new item (job/folder). In this case we're using the “Finance” folder we created in the previous task. Step #2: Enter a name in the textbox and select “Freestyle project” and click “OK” to create the job.


2 Answers

You can use the dir step, example:

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

The folder can be relative or absolute path.

like image 116
tsl0922 Avatar answered Sep 20 '22 12:09

tsl0922


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" }  
like image 20
Gonzalo Robert Díaz Avatar answered Sep 18 '22 12:09

Gonzalo Robert Díaz