What's the working directory in Jenkins after executing a shell command box?
Asked Answered
A

3

15

I'm looking at a Jenkins job and trying to understand it.

I have an Execute shell command box in my Build section:

> mkdir mydir 
> cd mydir
> 
> svn export --force https://example.com/repo/mydir .

When Jenkins is done executing that command, and moves on to the next build step, what is its working directory? workspece-root/ or workspace-root/mydir ?

As the next step, I have Invoke top-level Maven targets (still in the Build section).

What I really want to know is: why does that execute successfully?
Is it because Jenkins automatically moves back to the workspace-root/ folder after executing a shell command box, or is it because the next job is a "top-level" job, and Jenkins therefore changes back to the workspace-root/?

Alishiaalisia answered 25/6, 2014 at 15:10 Comment(0)
C
25

Each build step is a separate process that Jenkins spawns off. They don't share anything, neither current directory, nor environment variables set/changed within the build step. Each new build step starts by spawning a new process off the parent process (the one running Jenkins)

It's not that Jenkins "move back" to $WORKSPACE. It's that Jenkins discards the previous session.

Cervelat answered 25/6, 2014 at 19:38 Comment(2)
Does it always start in $WORKSPACE, then? And could you maybe point me to a section in the official docs explaining that, please? :)Alishiaalisia
Yes, always $WORKSPACE. Most plugins will always be relative to $WORKSPACE too. If you click on the question mark icon to the right of execute shell build step, the pop up help mentions that it executes relative to $WORKSPACECervelat
D
0

I lately saw that if you print the CWD , I would get the Project_NAME. E.g D:\jenkins\workspace\My_Project

Any script you might be running wont be found. Hence we can do a "CD path" before we start out scripts.

Decapolis answered 12/5, 2016 at 11:5 Comment(0)
U
0

Slav's explanation is very good and I thought of complementing it by providing a real world example that shows how multiple Windows batch commands look like even if they work in the same directory:

Command 1

REM #ensures that all npm packages are downloaded
cd "%WORKSPACE%"
npm install

Command 2

REM #performs a prod-mode build of the project
cd "%WORKSPACE%"
ng build --prod --aot=true -environment=pp

So, each one ensure that current working directory points to the current project directory.

Unpolitic answered 4/1, 2019 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.