Cannot access mvn command from bash script - bash

I want to run the following script as:
bash ./scripts/startapp.sh
Here is the script:
#!/bin/bash
str=$HOSTNAME
PREV_IFS=$IFS
IFS=. components=(${str})
ORIGINAL_DIRECTORY=`pwd`
REP_DIRECTORY=$ORIGINAL_DIRECTORY/src/main/resources
for part in "${components[#]}"
do
PATH=$REP_DIRECTORY/"$part"
REP_DIRECTORY=$PATH
done
IFS=$PREV_IFS
CONFIG_PATH=$REP_DIRECTORY/application.yaml
# Below is the final command I want to run from the terminal
`SPRING_CONFIG_LOCATION=$CONFIG_PATH mvn spring-boot:run`
I am getting
mvn: command not found
Without starting the script, I can use mvn spring-boot:run without any problem.

Make sure you have defined:
M2_HOME pointing to the base directory of your Maven installation
PATH must include $M2_HOME/bin
In your script you're overwriting the value of PATH on every iteration. You should change it to:
PATH=$PATH:$REP_DIRECTORY/"$part"

Related

Jenkins - Execute bash script to deploy application

I have one bash script which deploys the application inside JBoss9.
It's working fine When I runs bash script from ubuntu terminal using command ./jbctl -c restart tail -f nohup.out;
But when I calls same bash script inside Jenkins - Build - Execute shell, it shows me various errors like
./jbctl: line 123: cat: command not found
./jbctl: line 123: grep: command not found
./jbctl: line 123: cut: command not found
./jbctl: line 123: sed: command not found
./jbctl: line 124: cat: command not found
/tmp/jenkins2547454576675677717.sh: line 10: tail: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Without this jbctl.sh file, application can't be initialised. As there are bash commands inside this ./jbctl.sh file, these are not executed inside Jenkins - Build - Execute shell.
Is there any other way to run this jbctl.sh script inside Jenkins? Actually this is bash script.
There are two approaches to your issue:
Adjust Jenkins' environment variables (prefered) as e.g. described at wiki.jenkins.io. In short: Navigate to Manage Jenkins > Global Properties > Environment variables. For more, see also SO: "modify PATH variable in jenkins master"
Quick'n'dirty: Instead of cat use the full path of your command, e.g. /usr/bin/cat. You can determine this full path with entering which cat in your bash.

change Jenkins shell for only one pipeline

How can I make Jenkins use Bash rather than its native shell for just one Jenkins pipeline/Jenkinsfile? Does the "agent" help me to do this?
I wrote a shell script for deployment but some of the parameters contain whitespace which messes up the resulting command I generate by losing some args. I've found how to avoid this problem by globally configuring Jenkins shell type to be Bash. But when I change the global shell type, my other Jenkins pipelines that use the Jenkins docker plugin syntax get broken when they use the 'sh' command within a docker container. My workaround is to ping pong the global setting for shell type depending on which Jenkins build I want to run. Its a royal PITA.
I'm embarrassed to say all I needed was a shebang.
My Jenkinsfile runs a custom (bash) shell script, using Jenkin's sh command, like in the following:
sh "./deploy.sh \"arg1\" \"arg 2\" \"arg3\""
In order to force deploy.sh to run within Bash, the contents of deploy.sh need to include #!/bin/bash on the first line, as in:
#!/bin/bash
echo "deploy args: $#"
Regardless, I think there should be a way to tell a Jenkins pipeline that it should run with specific settings, like sh=bash.

Crontab: zip: command not found

I have the following script that runs perfectly when I execute it:
#!/bin/bash
zip logs.zip -r *
However when I run it from crontab, i get
/home/ubuntu/script.sh: line 2: zip: command not found
Why is that? Do I need to set up some sort of path? I have no luck searching for it.
Thanks!
Your login shell will have zip in the PATH, however cron will not run commands in a login shell. You can specify environment variables in your crontab file, you should probably set the PATH to the same as your login shell and possibly add some more variables too. I have:
SHELL=/usr/local/bin/bash
MAILTO="my#email.address"
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
So that cron runs bash, sends emails to a working address and uses a decent PATH
Use the following command to find full path of zip
which zip
then use full path is your script... ie:
/usr/bin/zip logs.zip -r *

what is the "ant" equivalent for shell command "basename" to get the last dir name in a path

I am working on an ant script and I have a property set to:
MW_HOME=/path/to/scripts
In shell, I can just use below command to get the base directory name i.e. "scripts" in this case ->
dir_name=$(basename $MW_HOME)
Is there any such handy command in ant or do I need to process that property to filter out the same? Any suggestion to make it easy?
https://ant.apache.org/manual/Tasks/basename.html is the equivalent command in ant.
https://ant.apache.org/manual/Tasks/dirname.html is a similar command.

OpenVPN Source vars not working on debian

I have to create a script to setup an OpenVPN server automatically.
In this script I need to source the vars file in /etc/openvpn/easy-rsa/
But when I'm executing the following script in the /etc/openvpn/easy-rsa/ folder (with chmod 775 on the script and vars file) it says "xxxx.sh: 3: xxxx.sh: source: not found:"
#!/bin/bash
source ./vars
When I write . ./vars, it works, but then if I want to do a ./clean-all it says :
Please source the vars script first (i.e. "source ./vars")
Make sure you have edited it to reflect your configuration.
When I do the ./clean-all in the same script than the . ./vars, it works.
Thanks for your help (and sorry for my bad english :/)
When you source (or .) a file, all the commands inside it are read and executed - this includes variable assignments. However, when a variable assignment takes place, it takes place only for the current shell. When you run a script, a subshell is created - so any variables inside the script are only visible within the subshell, not the parent (calling) shell. This is why it works when you have run source and clean-all within the same script, it should also work if you do both from the command line, ie:
$ . /etc/openvpn/easy-rsa/vars
$ /etc/openvpn/easy-rsa/clean-all

Resources