KK99
KK99

Reputation: 1989

Jenkins cd to a folder with pattern?

Here is the folder structure I have.

Workspace folder: D:\Node\MyNode\

  1. When Jenkins build runs on a node, the files from scm gets downloaded to the following folder: D:\Node\MyNode\xx_development

I need to do cd to the folder "xx_development" and this name xx can change for different strasms (RTC) but "_development" remains same.

how can I do cd to a folder with (*development) using a pipeline script?

Edit: I am using windows Nodes for Jenkins.

Upvotes: 0

Views: 182

Answers (1)

Yaseen
Yaseen

Reputation: 259

To change the current directory to the folder with the pattern *_development, you can use the following script:

For Windows:

def folder = bat(returnStdout: true, script: 'dir /b /ad | findstr "_development"').trim()
bat "cd ${folder}"
  • dir /b /ad | findstr "_development" --> lists all directories in the current folder and filters them by the pattern _development.
  • /b --> to list only the directory names.
  • /ad --> to list only directories.
  • findstr --> to filter the output by the pattern _development.
  • The second line changes the current directory to the directory stored in the Folder variable.

For Linux:

def Folder = sh(returnStdout: true, script: 'ls -d */ | grep "_development"').trim() 

sh "cd ${Folder}"
  • ls -d */ | grep "_development" --> lists all directories in the folder and filters by the pattern _development.
  • trim() --> If there are any leading or trailing whitespaces, they are removed using this command.
  • The second line changes the current directory to the folder stored in the Folder variable.

Upvotes: 1

Related Questions