How to change directory inside bash script from Jenkinsfile? - jenkins

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

Related

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

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()

Run action/script AFTER the binary executed by "bazel run" returns

I'm wondering if there's a way to run an action or script or program after a bazel run command finishes. Running such a script/program before the bazel run is (in a way) possible via --run_under.
Here's an example use-case:
bazel run //path/to/my:target
Running target generates an output file out in it's cwd (somewhere in the bazel .cache directory)
Run a script that uses the output file out
If it's not possible to get run that script afterwards, is there a way to get the cwd of the binary that was just run? Then it would also be possible to access out from "outside" without bazel.
--run_under is the correct approach to this. You can simply run the command in the script after running what is passed as args to your script that you pass to --run_under e.g.
#!/bin/bash
set -euo pipefail
./prefix_command
bash "$#"
./postfix_command

rosrun : Package not found

How to run a python script file for ros ?
I have developed python script to make a drone to fly. I have kept the code inside
tumsimulator/src/scripts/DroneFly.py. ## catkin make is done in this directory
When I run the code in tumsimulator directory, it throws an error saying that scripts directory is not found.
I gave chmod 777 permission for directory as well as the file.
Can Somebody help me to run the python script in ros ?
Just to be on the same page
you need to create a catkin workspace a catkin package. For example you create a workspace called catkin_ws
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
Create your package called tumsimulator in ~/home/catkin_ws/src
catkin_create_pkg tumsimulator rospy
Put the scripts directory in tumsimulator/ not in tumsimulator/src. Once after running catkin_make, you should be able to run the script via
Run catkin_make
cd ~/catkin_ws
catkin_make
Finally, run your script
rosrun tumsimulator DroneFly.py
As a side note, scripts should note have the py extension. You can add the python shebang line at the top of your script file #! /usr/bin/env python. If your are writing a python module, you may put these files in tumsimulator/src/tumsimulator/ next to tumsimulator/src/tumsimulator/__init__.py.

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