Jenkins Script Console: delete workspace folder including space in folder name - jenkins

here is my problem:
I deleted a project with the name "test_job 2".
The name includes a space.
Now the project is gone but the workspace folder is still there.
So usually I go to Jenkins Script Console and run:
println "ls -la /var/jenkins_home/workspace/".execute().text
println "rm -rf /var/jenkins_home/workspace/<folder name>".execute().text
println "ls -la /var/jenkins_home/workspace/".execute().text
But it doesn't work when the folder name includes space.
All the usual suspects also don't work and I also don't get any error messages.
Here is what I've tried so far without success in Jenkins Script Console.
If I connect to my local test jenkins via terminal it works.
"rm -rf '/var/jenkins_home/workspace/test_job 2'".execute()

Related

What is the location of .sh file of the jenkins job?

I am looking for the location of .sh file which is generated my jenkins job and can be found in console output. This .sh files starts with jenkins word.
I am using AWS instance for jenkins.
i was looking for /temp folder, but didn't get into /tmp folder.
sample of the .sh file name:
[first_gitbuild] $ /bin/sh -xe /tmp/jenkins8581903656897699418.sh
These files are deleted when job finishes.
I was able to get a copy of this .sh files with these steps:
Set a sleep in my script ( 5 mins )
Build the job
Go to /tmo folder and copy paste the script : /tmp/jenkins......sh

How to change directory inside bash script from Jenkinsfile?

In my Jenkinsfile I have cloned one git repo using Git SCM and for relativeTargetDir I added for example "myGit".
So it is saved in directory "myGit".
After that I need to run my bash script, and inside after executing some lines of codes, I need to go inside myGit folder to be able to run some tests.
But I'm always getting cd: myGit: No such file or directory.
In my bash script I have tried like this :
some code...
cd myGit
run some tests inside myGit directory...
But when I try in Jenkinsfile like this :
dir('myGit') {
run some tests inside myGit directory...
}
it's working.
But I need to do that in my bash script.
Expected result : to go inside myGit directory
Actual result : throws cd: myGit: No such file or directory.
you can always do a
sh 'cd myGit && run some tests inside myGit directory

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

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/?
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.
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.
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.

Executing custom shell script to build in jenkins

I have setup a jenkins job to build my project. I have a jake.sh file in my project and the code is pulled from github. I want "npm install" command to be executed and then jake.sh to be executed once the the code is checked out.
How can I configure this in jenkins? I have tried givin ./jake.sh and jake.sh in Build->Execute Shell section
According what you tell I think the problem can be
The script is not marked as a executable. In this case add in Build -> Execute Shell (in case you have linux) sudo chmod 777 path_to_script/jake.sh.
The script is not in the base directory. Remembeber that when you execute a bash script, the current directory is /path_to_job/workspace. So you have first to move to the script folder (cd path_to_script) or specify the path when running it: ./path_to_script/jake.sh.
I hope this solves your problem.
A workaround for shell scripts can be to run the script as
bash ./jake.sh
instead of
./jake.sh
Then you don't have to do chmod. Useful when you wipe the workspace before every build.
In the same manner, if you have a nodejs shell script or python script, you can run node myscript.js / python myscript.py.

Jenkins how to rename war file

I have deployed a war file in a remote machine using Jenkins. Now I want to rename the war file through jenkins before it extracts the work folder? How can this be done? I tried post deployment action -> execute shell and mv file.war to new-file.war but it returns an error saying : mv: cannot stat `file.war': No such file or directory.
Suppose there was something wrong with my path it would not even have gone to remote location. but for me, after scp' ing it to remote location thru jenkins, and when i try to do a mv, it fails.. What could the reason be??
Adding additional Step of Execute shell during Add build Step or Add post-build action stage, normal renaming shell command mv can be used to rename artifacts.
Note: Make sure use the correct path(Relative to project/workspace root)
Your mv command is probably executed in another directory than the one you are expecting.
To know the directory your script is running in without reading the jenkins / plugin documentation add
echo "pwd of script is: " `pwd`
to your shell script and inspect the output of the jenkins build - that way you can be sure about the directory the script is run in.

Resources