Abdullah
Abdullah

Reputation: 151

Why is Jenkins Build Step which executes Shell Script marking build as failure?

I am running PMD in a Jenkins job. My Shell Script Build step is as follows.

alias pmd="/root/pmd-bin-6.11.0/bin/run.sh pmd"
pmd -d "/var/jenkins_home/workspace/Warnings" -R rulesets/java/quickstart.xml -f xml > results.xml

The results.xml file is generated perfectly fine. But, in my console log, it says.

"Build step 'Execute shell' marked build as failure".

I am new to Bash/Linux/Shell scripting so please help me out. Thanks.

Console Logs

15:04:11 Started by user admin
15:04:11 [EnvInject] - Loading node environment variables.
15:04:11 Building in workspace /var/jenkins_home/workspace/Warnings
15:04:11 [WS-CLEANUP] Deleting project workspace...
15:04:11 [WS-CLEANUP] Deferred wipeout is used...
15:04:11 [WS-CLEANUP] Done
15:04:11 using credential creds
15:04:11 Cloning the remote Git repository
15:04:11 Cloning repository https://github.com/at/coder
15:04:11  > git init /var/jenkins_home/workspace/Warnings # timeout=10
15:04:11 Fetching upstream changes from https://github.com/at/coder
15:04:11  > git --version # timeout=10
15:04:26 [Warnings] $ /bin/sh /tmp/jenkins5909715001648669215.sh
15:04:29 Feb 08, 2019 9:34:29 AM net.sourceforge.pmd.RuleSetFactory parseRuleReferenceNode
15:04:29 WARNING: Discontinue using Rule name category/java/multithreading.xml/UnsynchronizedStaticDateFormatter as it is scheduled for removal from PMD. PMD 7.0.0 will remove support for this Rule.
15:04:29 Feb 08, 2019 9:34:29 AM net.sourceforge.pmd.PMD processFiles
15:04:29 WARNING: This analysis could be faster, please consider using Incremental Analysis: https://pmd.github.io/pmd-6.11.0/pmd_userdocs_incremental_analysis.html
15:04:49 Build step 'Execute shell' marked build as failure
15:04:49 using credential creds
15:04:49 [PMD] Searching for all files in '/var/jenkins_home/workspace/Warnings' that match the pattern '**/results.xml'
15:04:49 [PMD] -> found 1 file
15:04:49 [PMD] Successfully parsed file /var/jenkins_home/workspace/Warnings/results.xml
15:04:49 [PMD] -> found 1967 issues (skipped 0 duplicates)
15:04:49  > git rev-parse e84e3873745f2413bd8e807847552011f5cb2c2c^{commit} # timeout=10
15:05:15 [PMD] Post processing issues on 'Master' with encoding 'UTF-8'
15:05:15 [PMD] Resolving absolute file names for all issues in workspace '/var/jenkins_home/workspace/Warnings'
15:05:15 [PMD] -> 0 resolved, 0 unresolved, 426 already resolved
15:05:15 [PMD] Copying affected files to Jenkins' build folder '/var/jenkins_home/jobs/Warnings/builds/42/files-with-issues'
15:05:15 [PMD] -> 426 copied, 0 not in workspace, 0 not-found, 0 with I/O error
15:05:15 [PMD] Resolving module names from module definitions (build.xml, pom.xml, or Manifest.mf files)
15:05:15 [PMD] -> resolved module names for 1967 issues
15:05:15 [PMD] Resolving package names (or namespaces) by parsing the affected files
15:05:15 [PMD] -> all affected files already have a valid package name
15:05:15 [PMD] No filter has been set, publishing all 1967 issues
15:05:15 [PMD] Creating fingerprints for all affected code blocks to track issues over different builds
15:05:15 [PMD] -> created fingerprints for 1967 issues
15:05:15 [PMD] Invoking Git blamer to create author and commit information for all affected files
15:05:15 [PMD] GIT_COMMIT env = 'e84e3873745f2413bd8e807847552011f5cb2c2c'
15:05:15 [PMD] Git working tree = '/var/jenkins_home/workspace/Warnings'
15:05:15 [PMD] Git commit ID = 'e84e3873745f2413bd8e807847552011f5cb2c2c'
15:05:15 [PMD] Job workspace = '/var/jenkins_home/workspace/Warnings'
15:05:15 [PMD] Created blame requests for 426 files - invoking Git blame on agent for each of the requests
15:05:15 [PMD] -> blamed authors of issues in 426 files
15:05:15 [Static Analysis] Attaching ResultAction with ID 'analysis' to run 'Warnings #42'.
15:05:15 [Static Analysis] Using reference build 'Warnings #41' to compute new, fixed, and outstanding issues
15:05:15 [Static Analysis] Issues delta (vs. reference build): outstanding: 1967, new: 0, fixed: 0
15:05:15 [Static Analysis] No quality gates have been set - skipping
15:05:15 [Static Analysis] Health report is disabled - skipping
15:05:16 [Static Analysis] Created analysis result for 1967 issues (found 0 new issues, fixed 0 issues)
15:05:16 Sending e-mails to: [email protected]
15:05:17 Finished: FAILURE

Upvotes: 0

Views: 2198

Answers (2)

Michael Kemmerzell
Michael Kemmerzell

Reputation: 5256

Jenkins interprets every return status != 0 as a failure of your batch command. In most cases this is very annyoing. Ff you use robocopy for example, a successful copying of a file returns a status != 0 - even if the copy was a success - so Jenkins marks the build as failed.

You can supress the status code - or let the batch command always return 0 - so Jenkins will mark your build as a success. The post-fix ^& exit 0 will do this for you.

bat <execute your command here> ^& exit 0

Upvotes: 0

Amit Nanaware
Amit Nanaware

Reputation: 3346

The PMD will return non zero code if there are any violation found while build. you can disable this and get the exit 0 at the end of analysis which will mark your build as successful.

See below example:

pmd -d "/var/jenkins_home/workspace/Warnings" -R rulesets/java/quickstart.xml -f xml -failOnViolation false> results.xml

For more information check this document.

Upvotes: 1

Related Questions