为什么执行 Shell 脚本的 Jenkins Build Step 将构建标记为失败?

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

我是 Jenkins 工作的 运行 PMD。我的 Shell 脚本构建步骤如下。

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

results.xml 文件生成得非常好。但是,在我的控制台日志中,它说。

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

我是 Bash/Linux/Shell 脚本的新手,所以请帮助我。谢谢

控制台日志

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: decker@devep.com
15:05:17 Finished: FAILURE

如果在构建时发现任何违规行为,PMD 将 return 非零代码。您可以禁用它并在分析结束时获得 exit 0,这将标记您的构建成功。

见下例:

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

有关详细信息,请查看 this 文档。

Jenkins 将每个 return 状态 != 0 解释为批处理命令失败。在大多数情况下,这是非常烦人的。例如,如果您使用 robocopy,成功复制文件 returns 状态 != 0 - 即使复制成功 - 因此 Jenkins 将构建标记为失败。

您可以抑制状态代码 - 或者让批处理命令始终 return 0 - 这样 Jenkins 会将您的构建标记为成功。 post-fix ^& exit 0 将为您完成此操作。

bat <execute your command here> ^& exit 0