具有动态矩阵轴值的声明式管道
Declarative Pipeline with dynamic matrix axis values
嗨,我正在尝试让 Jenknis-Declarative-Pipeline-Job 工作。
用例应该非常简单:
我想使用相同的 Jenkins 管道构建多个插件。为此,我编写了一个 "JenkinsLibrary",其中包含插件可用于参数的接口。其中一个参数是轴值。
我遇到的问题与this reddist post非常相似。
我想从一个变量设置矩阵构建的 "axis" 的 "values"。
我没有想法,这可能吗?
所以这是我的例子:
一个 "JenkinsLibrary" 和一个文件 my_library.groovy
def call(Map i_options)
{
// later I will parse the options to set the values, but currently the step before already crashes ...
def axis_1_values = "axis_1_value_A"
// already tried the following alternatives ...
// def axis_1_values = 'axis_1_value_A'
// def axis_1_values = '''axis_1_value_A'''
// def axis_1_values = ["axis_1_value_A", "axis_2_value_A"]
pipeline
{
agent any
stages { stage("stage A") { matrix {
axes {
axis {
name "axis_1"
// values "axis_1_value_A" // <- of course this works ...
// but I want to read it from a variable
values "${axis_1_values}"
// I already tried more variants
// values ${axis_1_values}
// values axis_1_values
}
axis {
name "axis_2"
values "axis_2_value_A", "axis_2_value_B"
}
}
stages {
stage("another stage") { steps {
echo "hello world from ${axis_1} && ${axis_2}"
} }
}
} } }
}
}
带有文件的插件 "Jenkinsfile":
@Library("JenkinsLibrary") _
def options = [
axis_values: "a_axis_value"
]
my_library(options)
我收到以下错误:
[...] Expected string literal but got "${axis_1_values}"
完整日志:
19: Expected string literal but got "${axis_1_values}" @ line 19, column 28.
values "${axis_1_values}"
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:254)
at groovy.lang.GroovyClassLoader.recompile(GroovyClassLoader.java:761)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:718)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:775)
at org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable.getValue(UserDefinedGlobalVariable.java:57)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:113)
at sun.reflect.GeneratedMethodAccessor729.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:160)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:142)
at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:158)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:162)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
at WorkflowScript.run(WorkflowScript:21)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
at sun.reflect.GeneratedMethodAccessor500.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28)
at com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55)
at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access[=13=]1(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:405)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:317)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:281)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE
我的系统:
Jenkins-Version:詹金斯版本。 2.190.1
声明式插件版本 1.5.0
声明代理 API:1.1.1
声明性扩展点 API:1.5.0
(很多其他插件,我认为这是重要的)
更新
我仍然没有解决方案。但是当我像这样定义一个变量时,我明白我试图向它插入一个 "GStringImpl" 。但我无法将其转换为 java-string.
def _str_1 = "a string"
def _str_2 = "${_str_1}"
println _str_1.class // -> class java.lang.String
println _str_2.class // -> class org.codehaus.groovy.runtime.GStringImpl
def _str_3 = "${_str_1}" as java.lang.String
println _str_3.class // -> class java.lang.String
但我仍然不知道如何添加单个值,因为如果我这样做
values "${axis_1_values}" as as java.lang.String // error-msg: Expected string literal but got ${as}
values ("${axis_1_values}" as java.lang.String) // error-msg: Expected string literal but got ${"${axis_1_values}"
values axis_1_values.toString() // error-msg: Method calls on objects not allowed outside "script" blocks.
values "${axis_1_values}".toString() // error-msg: Expected a symbol @ line 26, column 28.
这种转换可能适用于单个值,但我希望能够添加一个值列表,就像在默认示例中可行的那样...
values "axis_2_value_A", "axis_2_value_B"
我想知道,这在声明性管道的 "docker-agent" 部分有效。
agent {
docker {
image "${_image}:latest"
label "${_label}"
}
}
但是我在代码中,发现docker-label的解析是单独实现的...
我仍然不知道如何在声明性管道的矩阵/轴功能中实现该目标。
(有趣:即使这样做的主要原因之一是脚本管道的蓝海视图中的可视化仍然有问题,而且这个问题似乎被忽略了......
https://issues.jenkins-ci.org/browse/JENKINS-53751)
自插件 v1.6.0 起,声明式管道不支持动态轴值。错误消息说它需要一个 "literal string" 值。也许消息还应该说,"Template strings, arrays, variables, or function calls are not allowed."
这是设计使然,旨在防止管道作者被管道引擎的某些内部假设所困扰。可能会发生变化,但需要仔细规划和测试。
请在 https://issues.jenkins.io/ and/or join the Jenkins Pipeline Authoring SIG 提交 JIRA 并帮助实现此功能。
嗨,我正在尝试让 Jenknis-Declarative-Pipeline-Job 工作。
用例应该非常简单:
我想使用相同的 Jenkins 管道构建多个插件。为此,我编写了一个 "JenkinsLibrary",其中包含插件可用于参数的接口。其中一个参数是轴值。
我遇到的问题与this reddist post非常相似。
我想从一个变量设置矩阵构建的 "axis" 的 "values"。 我没有想法,这可能吗?
所以这是我的例子:
一个 "JenkinsLibrary" 和一个文件 my_library.groovy
def call(Map i_options)
{
// later I will parse the options to set the values, but currently the step before already crashes ...
def axis_1_values = "axis_1_value_A"
// already tried the following alternatives ...
// def axis_1_values = 'axis_1_value_A'
// def axis_1_values = '''axis_1_value_A'''
// def axis_1_values = ["axis_1_value_A", "axis_2_value_A"]
pipeline
{
agent any
stages { stage("stage A") { matrix {
axes {
axis {
name "axis_1"
// values "axis_1_value_A" // <- of course this works ...
// but I want to read it from a variable
values "${axis_1_values}"
// I already tried more variants
// values ${axis_1_values}
// values axis_1_values
}
axis {
name "axis_2"
values "axis_2_value_A", "axis_2_value_B"
}
}
stages {
stage("another stage") { steps {
echo "hello world from ${axis_1} && ${axis_2}"
} }
}
} } }
}
}
带有文件的插件 "Jenkinsfile":
@Library("JenkinsLibrary") _
def options = [
axis_values: "a_axis_value"
]
my_library(options)
我收到以下错误:
[...] Expected string literal but got "${axis_1_values}"
完整日志:
19: Expected string literal but got "${axis_1_values}" @ line 19, column 28.
values "${axis_1_values}"
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:254)
at groovy.lang.GroovyClassLoader.recompile(GroovyClassLoader.java:761)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:718)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:775)
at org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable.getValue(UserDefinedGlobalVariable.java:57)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:113)
at sun.reflect.GeneratedMethodAccessor729.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:160)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:142)
at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:158)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:162)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
at WorkflowScript.run(WorkflowScript:21)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
at sun.reflect.GeneratedMethodAccessor500.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28)
at com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55)
at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access[=13=]1(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:405)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:317)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:281)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE
我的系统:
Jenkins-Version:詹金斯版本。 2.190.1
声明式插件版本 1.5.0
声明代理 API:1.1.1
声明性扩展点 API:1.5.0
(很多其他插件,我认为这是重要的)
更新
我仍然没有解决方案。但是当我像这样定义一个变量时,我明白我试图向它插入一个 "GStringImpl" 。但我无法将其转换为 java-string.
def _str_1 = "a string"
def _str_2 = "${_str_1}"
println _str_1.class // -> class java.lang.String
println _str_2.class // -> class org.codehaus.groovy.runtime.GStringImpl
def _str_3 = "${_str_1}" as java.lang.String
println _str_3.class // -> class java.lang.String
但我仍然不知道如何添加单个值,因为如果我这样做
values "${axis_1_values}" as as java.lang.String // error-msg: Expected string literal but got ${as}
values ("${axis_1_values}" as java.lang.String) // error-msg: Expected string literal but got ${"${axis_1_values}"
values axis_1_values.toString() // error-msg: Method calls on objects not allowed outside "script" blocks.
values "${axis_1_values}".toString() // error-msg: Expected a symbol @ line 26, column 28.
这种转换可能适用于单个值,但我希望能够添加一个值列表,就像在默认示例中可行的那样...
values "axis_2_value_A", "axis_2_value_B"
我想知道,这在声明性管道的 "docker-agent" 部分有效。
agent {
docker {
image "${_image}:latest"
label "${_label}"
}
}
但是我在代码中,发现docker-label的解析是单独实现的...
我仍然不知道如何在声明性管道的矩阵/轴功能中实现该目标。
(有趣:即使这样做的主要原因之一是脚本管道的蓝海视图中的可视化仍然有问题,而且这个问题似乎被忽略了......
https://issues.jenkins-ci.org/browse/JENKINS-53751)
自插件 v1.6.0 起,声明式管道不支持动态轴值。错误消息说它需要一个 "literal string" 值。也许消息还应该说,"Template strings, arrays, variables, or function calls are not allowed."
这是设计使然,旨在防止管道作者被管道引擎的某些内部假设所困扰。可能会发生变化,但需要仔细规划和测试。
请在 https://issues.jenkins.io/ and/or join the Jenkins Pipeline Authoring SIG 提交 JIRA 并帮助实现此功能。