Jenkins execute shell - shell

I have a jenkins job which is having 5 build scripts configured in job execute shell. Now I have a flag in script#1 if it's false then I don't want to run remaining 4 build scripts and directly exit then build results should be pass.
But now even after checking script#1 flag is false it's exiting from script#1 and executing remaining 4 build scripts then all the 4 scripts are failing due to dependency with script#1 finally my jenkins build status is failed.
I want after checking script#1 job could exit and directly jenkins job status should be "pass"
Is there anyway in jenkins itself to control this flow? without writing any code in scripts?
Jenkins error :
------------
23:37:16 Nothing to build
23:37:16 SRV_SKIP_UNNECESSARY_BUILDS is set to 1
23:37:16 Exiting now...
23:37:16 +++/usr/atria/bin/cleartool rmview -f -tag swrel_sre_icx_test_ashok_diffreportvu_20180118_233612
23:37:30 SRE_FYI: No rebuild necessary for sre_icx_test_ashok since previous full build[EnvInject] - Injecting environment variables from a build step.
23:37:30 [EnvInject] - [ERROR] - The given properties file path '/var/tmp/jenkins/workspace/Test/sre_icx_test_ashok/srv_env_strip' doesn't exist.
23:37:30 [EnvInject] - [ERROR] - Missing file path was resolved from pattern '${WORKSPACE}/srv_env_strip' .
23:37:30 [EnvInject] - [ERROR] - Problems occurs on injecting env vars as a build step: java.io.IOException: remote file operation failed: /var/tmp/jenkins/workspace/Test/sre_icx_test_ashok at hudson.remoting.Channel#1c0be696:l42-up-ecbld-01: java.io.IOException: The given properties file path '/var/tmp/jenkins/workspace/Test/sre_icx_test_ashok/srv_env_strip' doesn't exist.
23:37:30 Build step 'Inject environment variables' changed build result to FAILURE
23:37:30 Build step 'Inject environment variables' marked build as failure
23:37:30 Set build name.
23:37:30 Unrecognized macro 'SRV_BUILD_LABEL' in '#22 - ${SRV_BUILD_LABEL}'
23:37:30 Archiving artifacts
23:37:30 SSH: Current build result is [FAILURE], not going to run.
23:37:30 [description-setter] Description set: ${SRV_BUILD_DIR}
23:37:30 Notifying upstream projects of job completion
23:37:30 Finished: FAILURE

In theory, you could do so by acquiring a Jenkins plugin called "Fail The Build" and change your job so that:
The first build step writes a flagvar=value to a file which can later be sourced by another script, and then exits with a failure. The failure will skip out of running subsequent build steps and go straight to the post-build actions.
Then you can set in the post-build "Execute a set of scripts->Build steps->Execute shell". Set it to execute steps only if build fails, and then in the first step have it source the first build step's file written to see if it was an abort or an actual failed build. If a failed build, exit again with an error - otherwise let it go to the next post-build build step which calls Fail The Build to change the job status back to Success.
I have not tried this to see if it actually works.

Related

Jenkins maven projet build failed on fork during test

I'm building maven project on Jenkins (version 2.263.4, not the last but not so far).
It's a git project with pipeline configuration and webhook from bitbucket when push is done on the project.
So when a developper create a branch and another one a pull request, i have 2 build in parallel. Domain test running in about 45min.
And sometimes(not always), i have this error :
Command was /bin/sh -c cd /jenkins/workspace/a-hub_test-plantage-jenkins_PR-1/server/project-core/project-core-domain && /jenkins/tools/hudson.model.JDK/jdk11/jdk-11.0.2+9/bin/java -Xmx1500M -jar /jenkins/workspace/a-hub_test-plantage-jenkins_PR-1/server/project-core/project-core-domain/target/surefire/surefirebooter15630998754365374145.jar /jenkins/workspace/a-hub_test-plantage-jenkins_PR-1/server/project-core/project-core-domain/target/surefire 2021-05-04T12-39-54_622-jvmRun1 surefire5846464826560071036tmp surefire_13691263758446667285tmp
Error occurred in starting fork, check output in log
14:46:13 [ERROR] Process Exit Code: 1
14:46:13 [ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:669)
14:46:13 [ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:282)
14:46:13 [ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:245)
If i re run the buildin error, it work.
I'm using maven-surefire 2.22.2.
I try to configure it following many thread on stackoverfow, but nothgin work. I try with forkMode none or something else, it work but i loose coverage because jaccoco need fork to work. So i'm stuck.
What can i do to help me to debug this problem?
I don't understand why build failed when another one is building because for me Jenkins work on different working folder, no? so why the problem appear when i'have multiple build in parallel?
thanks for help
I find an hs_err_pid.log in jenkins near source code and i see out of memory! i see that my test was launch with -Xmx1500Mo and problem occured when i launched multiple branch/pr, so jenkins use all of the memory and failed all build during fork...i decrease -Xmx and all seems to work.

How to improve the build failure reporting of a prallel gradle build?

We use a parallel gradle build in our ci server. But finding the cause of the build failure is sometimes a pain, because one has to dig through the gradle output to find the real problem. I would like to improve that.
For example one of the gradle tasks uses yarn. If this task fails gradle reports the following in the jenkins console:
10:27:27 * What went wrong:
10:27:27 Execution failed for task ':foo-web:yarn'.
10:27:27 > Process 'command '/home/jenkins/workdir/workspace/foo/foo-web/.gradle/yarn/bin/yarn'' finished with non-zero exit value 1
Then one has to scroll up a lot of lines to find the output of yarn due to parallel tasks being finished, just to find out yarn could not download a dependency.
Can we change the gradle build failure reporting so that it will append the error output of the failed task at the end of the build output to the failure report? Or is there some way to get gradles failure reporting and the output of the failed task more closely together?

Maven wrapper in jenkins

I'm trying to test build a maven based project in jenkins.
https://github.com/tonsV2/Lift-Log-Backend
However I get the following error.
[Lift Log Backend] $ /bin/sh -xe /tmp/hudson4180120395829748105.sh
+ ./mvnw clean
Error: Could not find or load main class org.apache.maven.wrapper.MavenWrapperMain
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Any clues?
Seems like you have spaces in your Jenkins job name. Try to rename your job from "Lift Log Backend" to "Lift-Log-Backend"
I had the same issue, after renaming the Jenkins job, the error was gone.

Unable to execute maven command on jenkins

I have a jenkins job that checks out code from TFS and executes a test class using testng.xml
I have Maven properly installed in my machine in C:\JenkinsDependencies\apache-maven
It has mvn.bat file in its bin folder.
But when I execute my job, it fails with the following error.
Checking for pre-build
[TFS_LN_Mobile] $ cmd.exe /C '"C:\JenkinsDependencies\apache-maven\bin\mvn.bat test && exit %%ERRORLEVEL%%"'
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Build step 'Invoke top-level Maven targets' marked build as failure
Checking for post-build
Finished: FAILURE
Below is the configuration of my jenkins job:
I manually executed a batch command as follows:
cd "C:\JenkinsRoot\workspace\TFS_LN_Mobile"
mvn test -DsuiteXmlFile=testng.xml
This works fine.
mvn.bat is a batch script. you can try adding trace of it's variables after steps that might produce this error. take a look at this: Is there any way to trace through the execution of a batch file?

How does Hudson/Jenkins determine job result status?

I have a Hudson server that runs maven 3 jobs to compile Java code. In one instance, a particular job's build log indicates that the compilation and unit tests all ran successfully, and the build completed in a successful state and another chained job was called, all indicated that Hudson believed the job to be successful. The log shows:
20:44:11 [INFO] BUILD SUCCESS
20:44:11 [INFO] ------------------------------------------------------------------------
20:44:11 [INFO] Total time: 1:35:43.774s
20:44:11 [INFO] Finished at: Mon Mar 24 20:44:11 CDT 2014
20:44:40 [INFO] Final Memory: 51M/495M
20:44:40 [INFO] ------------------------------------------------------------------------
20:44:42 channel stopped
20:44:42 [locks-and-latches] Releasing all the locks
20:44:42 [locks-and-latches] All the locks released
20:44:43 Archiving artifacts
20:45:33 Updating JIRA-1234
20:45:33 Description set: 1.23.567
20:45:33 Labelling Build in Perforce using ${BUILD_TAG}
20:45:33 [jobname] $ /usr/local/bin/p4 -s label -i
20:45:33 Label 'hudson-jobname-45' successfully generated.
20:45:33 [DEBUG] Skipping watched dependency update; build not configured with trigger: jobname #45
20:45:33 Finished: SUCCESS
However, the Hudson job page shows a "red ball" and that job run is listed as "Last failed build (#45)" on the job page. When I look at the hudson#hudson:~hudson/jobs/jobname/builds/45/build.xml file, there is a line that says
<result>FAILURE</result>
Assuming this was where the final result was captured, I changed this to
<result>SUCCESS</result>
and reloaded the page, but the red ball is still showing on the job page for that instance. I have not restarted the server to attempt to re-read the info from disk.
To be fair, there were some environmental issues around this build. Some hours later, I got a message that more than one Hudson server instance was running against the same disk image and confirmed this with
ps -ef | grep hudson.war
on the server console, showing two running processes. The job page says this run "Took 0 ms", even though the log says "Total time: 1:35:43.774s". This is Hudson ver. 2.2.1 running on "CentOS release 5.4 (Final)".
My questions:
What are the criteria for each of the job statuses? (Stable, Failed, Unstable, Aborted and Disabled statuses)?
When and where is that data captured in the running server, and can it be modified?
You have too many questions in one post.
As for "What are the criteria for each of the job statuses? (Stable, Failed, Unstable, Aborted and Disabled statuses)?"
Disabled is when a project/job is disabled from configuration (done by user with permissions). This is not a run status.
The rest are Job Run statuses:
Aborted is when a run has been cancelled/aborted. This happens when a user (with permissions) clicks the red cross button to cancel a running build. I believe SCM checkout failure also causes aborted status (but not too sure about that)
Unstable is a special status that can be applied to the job run. Usually this is done by job configuration (such as Maven) or through plugins such as Text-finder plugin. I am not aware of a way to induce unstable status through command line. Maybe through a groovy script, like plugins do. Most of the times, unstable is set by the job configuration itself, indicating failed test.
Stable and Failed are the direct result of Build Step's exit code. If the build step, such as Execute Shell, exits with exit code 0, the build step is considered success. Anything other than 0 is considered failed. If during execution of a build step, the process exits with anything other than 0, it is considered failed. If there are multiple build steps (Free-style project jobs), the last Build Step's exit code marks the status of the whole run. Once again, any Build Step in between that exits with anything other than 0 will mark the build failed.
Post-build actions, such as Text-finder plugin can also change the status of the job run.
You are not supposed to be tinkering with job history files to change the status of previous jobs. If you need to change the job result, use post-build actions such as Text-finder plugin, but even then, it only allows to downgrade the result.
Here's a one liner to printing the commit SHA1 in a jenkins job using groovy post-build script:
println "git rev-parse HEAD".execute().text
You can save this to a variable or export it as a build parameter.

Resources