mvn command is not getting executed in cron - shell

I want to execute maven command (mvn) through a shell script to be executed through cron.
My shell script
echo "setting the variables"
export M2_HOME=/Users/<XXXXX>/Downloads/apache-maven-3.5.2
export M2=/Users/<XXXXX>/Downloads/apache-maven-3.5.2/bin
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre
export PATH=$PATH:/Users/<XXXXX>/google-cloud-sdk/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/<XXXXX>/Documents/Software/neo-java-web-sdk-3.54.23/tools:/Users/<XXXXX>/Downloads/apache-maven-3.5.2/bin:/usr/local/go/bin:/Applications/Privileges.app/Contents/Resources:JAVA_HOME/bin
echo "variables set"
{
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/bin/java -version > /Users/<XXXXX>/git/SimpleTests/org.saurav.simpletests/java.log
} || {
echo "java command failed"
}
{
/Users/<XXXXX>/Downloads/apache-maven-3.5.2/bin/mvn -version
} || {
echo "mvn command failed"
}
echo "Tests Executed"
output.log always print
setting the variables
variables set
mvn command failed
Tests Executed
So, it seems the mvn command execution fails.
Output of java command execution has been redirected to java.log but that prints empty. But it seems the java command execution is happening since the fallback echo statement is not printed here.
Best Regards,
Saurav

It seems the mvn file execution is a protected file execution.
With Mac Mojave the programs accessing the protected files have to be given full disk access
Please check here
https://apple.stackexchange.com/questions/378553/crontab-operation-not-permitted
https://support.intego.com/hc/en-us/articles/360016683471-Enable-Full-Disk-Access-in-macOS
After i followed the steps mentioned in the link 1, it started working.
Best Regards,
Saurav

Related

Jenkins execute shell output mystery

I'm tyring to run some build script in MacOS 10.11 but get no information about errors occurred during it's running. Even I try
<hudson.tasks.Shell>
<command>echo "fucking Jenkins!!!"
#!/bin/bash -l
echo "fucking Jenkins!!!"
/bin/sh -e
echo "fucking Jenkins!!!"
./build.sh $BUILD_NUMBER "Jenkins test build"
echo "fucking Jenkins!!!"</command>
</hudson.tasks.Shell>
I get in the console output
git rev-list ...
and then failure or success depending I set -e choice or not. Even echo command writes nothing. But the fact of failure doesn't help at all if there's no more information. No more options I could find over the Web. What is the secret of the execute shell plugin to make it write something to the console output.

How to find if a Job fails in jenkins

I am really new to Jenkins and shell scripts, I just want to know if I have a set of commands which are configured in a build, is there any simple way to know if the job failed or the set of commands terminated to due some reason?
For example as under configure job if we have a set of shell commands as below can i embed them in on if statement as below :
if [ ${jenkinsHome}/doThis.sh > andThis.sh
${jenkinsHome}/nowDOThis.sh > ${workpsace}/commit.sh
${workpsace}/test.sh test
]
then
echo "build passed"
else
echo "failed"
fi
Can i do something like the above. please suggest.

How can I run new gradle task?

I have created a new gradle task in build.gradle:
task callCL(type: Exec) {
println "hello"
commandLine './rerun.sh'
}
Which suppose to run rerun.sh:
#!/bin/bash
cucumber -f rerun --out rerun.txt
file="rerun.txt"
if [ -f "$file" ] then
cucumber #rerun.txt
rm $file
fi
I'm using IntelliJ as an IDE. How can I run this task?
I have tried to run in the zshell console and got this error:
gradle callCL
zsh: command not found: gradle
But in the IDE I use gradle all the time so it must be installed.
How can I fix this? And is my writing ok?
Try this:
1. Make sure GRADLE_HOME, GRADLE_OPTS are set.
2. Make sure $PATH has GRADLE_HOME/bin in it.
3. which gradle should return you a valid output.
4. then, see below, if this works on command prompt, then your IDE setting just need to know where's is GRADLE_HOME aka its installed / executable (either gradle or gradle.bat)
NOTE: I have used my own dummy rerun.sh file, you can you use build.gradle (as shown below).
$ cat rerun.sh
#!/bin/bash
echo Im re-running a command echo
echo something
echo ...
echo
$ cat build.gradle
task callCL(type: Exec) {
println "-----"
println "hello"
println "-----"
executable "bash"
args "-c", "bash ./rerun.sh"
//The following will do as well as magic number in bash is already set to #!/bin/bash
//args "-c", "./rerun.sh"
}
$ /cygdrive/c/gradle-2.0/bin/gradle callCL
-----
hello
-----
:callCL
Im re-running a command echo
something
...
BUILD SUCCESSFUL
Total time: 2.006 secs
This looks like problem with gradle not being found on path (in your shell).
You may use GVM to easily install gradle so that its available on your PATH.

Executing Maven task from shell script and getting error codes

I'm executing a Maven deploy task from a bash script however even if the Maven task fails the script will continue and complete without errors.
I have tried the -e flag but that causes the deploy to fail. I also tried the following (pseudo code)
result_code= mvn deploy
if [$result_code -gt 0];then
exit 1
Any suggestions how i can identify if the deploy was successful?
result_code=mvn deploy is not the way to get return status
you can try e.g. :
#!/bin/bash
mvn deploy
STATUS=$?
if [ $STATUS -eq 0 ]; then
echo "Deployment Successful"
else
echo "Deployment Failed"
fi
In addition if anyone using Windows 10, here is the example which I use:
mvn deploy
if not %ERROR_CODE%==0 goto error
echo SUCCESS
goto end
:error
echo FAILED
:end
Just one other possible reason a person's mvn task may return 0 despite failing: be careful about piping the output of maven to other programs. For example, I'm using grcat (which grc is build on top of), which will always return exit code 0;
\mvn compile | grcat ~/conf.mvn
I'm not sure how to retain my nice color coding of the output. There is color_maven.sh out there but that has other issues.

Bash script to output function result

I have a bash shell script executed by Jenkins / Hudson. For some reason when I create a function, the output of that function is not logged back to the console. This is an example of my code:
EDIT!!! - this culprit here is SSH... Is there a way to force its output back to console?
perform_task()
{
ssh jenkins#192.168.100.5 pwd
}
echo "Starting a task";
perform_task || { echo "The Task Failed"; exit 1; }
The output in the console is:
"Starting a task"
if I move the commands outside of the function, I can see their output.
Help on this would be greatly appreciated!
I partially solved the problem by getting the script to echo the command:
#!/bin/bash -ex
This still hides the output of that command, but at least I have insights about what's being run
I just copied your function, added #!/bin/bash -e to the beginning of and pasted it into the "Execute Shell" of Jenkins.
It worked fine!
Is your bash executable actually located at /bin? What OS are you running the script on?
Building remotely on slave_1
[test] $ /bin/bash -xe /tmp/hudson1206345540964396738.sh
+ echo 'Starting a task'
Starting a task
+ perform_task
+ ssh slave_2 pwd
/home/jenkins
Finished: SUCCESS
Not sure about Jenkins or all that, but this worked for me:
perform_task()
{
ls -al
ps aux
pwd
}
echo "Starting a task"
perform_task || ( echo "The Task Failed" ; exit 1 )
add #!/bin/bash as first line of the file
The reason is the basic bourne shell(/bin/sh) does not support some of the features you use.

Resources