我正在使用Jenkins 2来编译Java项目,我想从pom.xml中读取该版本,我遵循这个例子:
https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md
这个例子表明:
似乎访问文件系统存在一些安全问题,但我无法弄清楚它给出了什么(或为什么)这个问题:
我只是做了一个与示例有点不同的事情:
def version() {
String path = pwd();
def matcher = readFile("${path}/pom.xml") =~ '<version>(.+)</version>'
return matcher ? matcher[0][1] : null
}
Run Code Online (Sandbox Code Playgroud)
我在运行'version'方法时遇到的错误:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object (org.codehaus.groovy.runtime.GStringImpl call org.codehaus.groovy.runtime.GStringImpl)
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectMethod(StaticWhitelist.java:165)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:117)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:103)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:149)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:146)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:15)
at WorkflowScript.run(WorkflowScript:71)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:55)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:106)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:79)
at sun.reflect.GeneratedMethodAccessor408.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:100)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:79)
at sun.reflect.GeneratedMethodAccessor408.invoke(Unknown Source) …Run Code Online (Sandbox Code Playgroud) 想象一下Jenkins工作A需要1分钟才能运行,而工作B需要5分钟.
如果我们将作业A配置为触发作业B,则作业B正在运行作业A可以在B完成之前运行5次.但是,Jenkins没有为作业B的队列添加5个构建,这很好,因为否则快速的作业A将为不良的慢作业B创建不断增长的构建积压.
但是,现在我们希望使用参数化触发器插件将作业A触发器B作为参数化作业.参数化作业确实排队积压,这意味着作业A很乐意为作业B创建大量构建,这可能无法跟上.
每次触发时向队列添加新的参数化构建都是有意义的,因为参数可能不同.Jenkins不应该总是假设新的参数化构建不需要先前排队的构建.
但是,在我们的例子中,我们实际上是这样的.作业A构建并打包我们的应用程序,然后作业B将其部署到类似生产的环境中并运行更多的集成测试.我们还有一个构建C,它可以部署到另一个环境并进行更多测试,因此这对我们来说是一个不断升级的模式.
我们希望参数化作业B的队列只保留添加的最后一个构建; 每个新构建都将替换当前队列中的任何作业.
有没有很好的方法来实现这一目标?
我有一个声明詹金斯管道用stage1,stage2,stage3等。stage2如果stage1设置生成不稳定/失败,我想停止运行。
我知道我可以停止的步骤stage1使用运行return时版本是不是成功,但无法找到一种方法,我可以只退出管道不运行下面的阶段stage1
这是我所拥有的:
stage('stage1') {
steps {
script{
//somesteps
if ("${stdout}" == "1"){
currentBuild.result = 'UNSTABLE'
return
} //if
//somesteps
} //script
} //steps
} //stage
// run only when stage1 is success
stage('stage2'){
when {
expression {
params.name ==~ /x|y/
}
}
steps {
script{
//stage2 steps
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果params.name ==~ /z/将执行阶段3,则跳过阶段2
注意:我不能在stage1的stage2 / 3 / ..中包括这些步骤。应该是那样的。根据构建参数,stage2 / 3/4 ...将在stage1之后调用
Jenkins管道项目配置为Jenkinsfile从Git仓库获取它:
如果我更改参数列表,例如,从:
properties([
parameters([
string(name: 'FOO', description: 'Choose foo')
])
])
Run Code Online (Sandbox Code Playgroud)
至:
properties([
parameters([
string(name: 'FOO', description: 'Choose foo'),
string(name: 'BAR', description: 'Choose bar')
])
])
Run Code Online (Sandbox Code Playgroud)
并运行构建,第一次运行不显示新添加的BAR参数:
由于更新的Jenkins文件需要存在BAR参数,这会导致更改后的第一个构建失败,因为没有向用户显示输入此值的输入.
有办法防止这种情况吗?Jenkinsfile在显示参数输入页面之前确保它是最新的?
就像在主题中一样-是否有任何方法可以验证当前版本是否是使用“重播”按钮的效果?