#3 问题现象:文件是存在的,但是 new File(“”).exists() 返回不存在
在Jenkins中,Step会在节点上执行,但是普通的JAVA代码不会。而且Java代码的相对路径是根目录。
(之前遇到过这个问题,今天(08/19/2019)又看到这个链接了,这里记录一下。)在Pipeline中,程序代码(比如Groovy或Java代码)是在Master节点上执行的,而Pipeline的Step是在各个节点上执行的(根据Pipeline中定义的节点)。
#2 问题现象:在共享库里函数中调用 println 无效(即在 Console Ouput 中没有输出)
println in “call” method of “vars/foo.groovy” works, but not in method in class
printlns in shared library vars classes are ignored
#1 NotSerializableException GitChangeSetList
(Error:java.io.NotSerializableException: hudson.plugins.git.GitChangeSetList)
Jenkins可以保存中间执行,但是要求能够序列化。但是原始构建无法被序列化。
所以需要在函数上添加 @NonCPS 注解,形如:
@NonCPS def showChangeLogs() { def changeLogSets = currentBuild.rawBuild.changeSets for (int i = 0; i < changeLogSets.size(); i++) { def entries = changeLogSets[i].items for (int j = 0; j < entries.length; j++) { def entry = entries[j] echo "${entry.commitId} by ${entry.author} on ${new Date(entry.timestamp)}: ${entry.msg}" def files = new ArrayList(entry.affectedFiles) for (int k = 0; k < files.size(); k++) { def file = files[k] echo " ${file.editType.name} ${file.path}" } } } }
Annotation Grab cannot be used in the sandbox
问题描述:
... org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed: General error during conversion: Annotation Grab cannot be used in the sandbox. java.lang.SecurityException: Annotation Grab cannot be used in the sandbox. at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.RejectASTTransformsCustomizer$RejectASTTransformsVisitor.visitAnnotations(RejectASTTransformsCustomizer.java:112) at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitImports(ClassCodeVisitorSupport.java:73) ...
原因分析:
不管是在项目中使用的共享库,还是在 Folder 中使用的共享库,都是在 sanbox 中运行的,所以无法使用 Grab 语法。
解决方案:
在全局中引入该共享库:Manage Jenkins / Global Pipeline Libraries
# 当使用input的booleanParam选项注入变量时,变量类型是什么?
-「booleanParam in jenkins dsl」
软件版本:Jenkins ver. 2.176.1
问题描述
在下面示例中:
input { message "Should we continue?" id "AAAA" parameters { booleanParam(name: 'PERSON', defaultValue: false, description: 'Who should I say hello to?') } } when { expression { return PERSON } }
在构建时,无论选择什么参数,表达式When永远为真。
问题原因
经过测试,由input的booleanParam选项注入的变量,它为String类型。也就是说变量PERSON为String类型。根据文档中对When的描述,当类型为String时,等同于true值,这就导致When永远是通过的。
根据「booleanParam in jenkins dsl」的描述,该变量是从env中获取,所以为String类型。
解决办法
当在When语句中,需要类型转换。将expression { return PERSON }替换为expression { return PERSON.toBoolean() }即可。
# 构建时创建了过多的子进程
问题描述:
前端开发人员,不知道在NPM的构建文件里写了什么,会递归产生子进程。最后把主机的进程耗光,导致服务器宕机。
解决办法:
这个好办,因为Jenkins是以jenkins用户运行的,所以「限制允许用户创建的最大进程数」即可。修改方式因系统而异,主要是SysV init与systemd之间的差异。
当初也不知道谁把NPORC改成了不限制,这你让说什么好。而且,我发现他们特别爱用infinity或者65536这种参数值。
# exception message [Exec timed out or was interrupted after
-「Jenkins Text-finder unable to success my Build」
问题描述:
使用Publish Over SSH插件时,当命令执行结束后,不能正确退出。一直等到超时之后才退出。
解决办法:
在Publish Over SSH的高级设置(Advanced…)中,选中「Exec in pty」选项。
# 某些Job不可见
问题描述:
在Jenkins中定义的某些Job不见了
问题原因:
因为升级了Jenkins中的某些插件,但是需要插件需要更新Jenkins的版本。在插件管理中一片红。
解决办法:
升级Jenkins到新的版本。
# java.lang.NoSuchMethodError: com.google.common.io.Files.asCharSource
-「Use of beta Guava method since deleted: Iterators.skip」
-「Upgrade Guava or properly isolate core Guava dependency from plugins」
-「Hide core dependencies in plugin classpath」
问题原因:
在Pipeline的共享库中使用了「Docker Java Parser » 0.2.0」库,该库依赖了「com.google.guava » guava 23.6-jre」库,这都没啥问题。
问题的原因在于,Jenkins也使用了Guava库,版本是11。旧库的加载覆盖了新库。
解决办法:
还没找到……
# Annotation Grab cannot be used in the sandbox
-「Could not initialize class with \@Grab annotation in shared library #43」
目前还不能在Pipeline中使用@Grab注解。解决办法有两个:在共享库里使用@Grab注解来引入第三方共享库;或者换用其他的插件,使用Jenkins提供的步骤。
#「jenkinsfile doesn’t recognize \@Grab」
在Pipeline中不能使用@Grab来引入第三方库。因为在Pipeline中,Jenkins的CPS需要源码才能进行CPS转换,而@Grab加载的是二进制的库。
# Scripts not permitted to use staticMethod
-「Jenkins CI Pipeline Scripts not permitted to use method groovy.lang.GroovyObject」