我有以下管道,但我不知道为什么它在第一行代码中失败:
pipeline {
agent any
environment {
def mypods = []
}
stages {
stage('Getting pods') {
steps {
script {
withKubeConfig(caCertificate: '.....', credentialsId: '.....', serverUrl: '.....') {
env.mypods = sh "kubectl get pod | grep Running | awk '{print \$1}'"
}
}
}
}
stage('Print pods') {
steps {
script {
mypods.each {
println "Item: $it"
}
}
}
}
}
}
我需要使用一个列表,因为kubectl get pods命令返回一个Pod列表,因此我必须在阶段中保存和使用它们。 如何在声明式管道上创建列表? 预先感谢。
这是错误:
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 4: Environment variable values must either be single quoted, double quoted, or function calls. @ line 4, column 22.
def mypods = []
^
WorkflowScript: 3: No variables specified for environment @ line 3, column 5.
environment {
^
2 errors
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.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:131)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:125)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:320)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
答案 0 :(得分:2)
您的环境应该在代理之后
pipeline {
agent any
environment {
def mypods = []
}
stages {
stage('Getting pods') {
steps {
script {
withKubeConfig(caCertificate: '.....', credentialsId: '.....', serverUrl: '.....') {
env.mypods = sh "kubectl get pod | grep Running | awk '{print \$1}'"
}
}
}
}
stage('Print pods') {
steps {
script {
mypods.each {
println "Item: $it"
}
}
}
}
}
}
答案 1 :(得分:2)
说明性管道在语法方面有一些限制。您会看到此错误,因为在environment
块中,您只能分配两种类型的表达式:
但是,您需要知道环境变量仅存储 字符串值,因此,如果您从函数调用中返回数组(或数组中的任何其他类型),它将自动转换为其toString()
表示形式。
pipeline {
agent any
environment {
MYPODS = getPods()
}
stages {
stage("Test") {
steps {
script {
println "My pods = ${env.MYPODS}"
}
}
}
}
}
def getPods() {
return ['pod1', 'pod2']
}
控制台输出:
[Pipeline] node
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] script (hide)
[Pipeline] {
[Pipeline] echo
<java.lang.String@ed6c7b35 value=[pod1, pod2] hash=-311657675>
[Pipeline] echo
MYPODS = [pod1, pod2]
[Pipeline] echo
Item: [
[Pipeline] echo
Item: p
[Pipeline] echo
Item: o
[Pipeline] echo
Item: d
[Pipeline] echo
Item: 1
[Pipeline] echo
Item: ,
[Pipeline] echo
Item:
[Pipeline] echo
Item: p
[Pipeline] echo
Item: o
[Pipeline] echo
Item: d
[Pipeline] echo
Item: 2
[Pipeline] echo
Item: ]
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
如果要存储字符串值列表,则可以将其定义为以,
字符分隔的单个值字符串。在这种情况下,您可以简单地将其标记为值列表。考虑以下示例:
pipeline {
agent any
environment {
MYPODS = 'pod1,pod2,pod3'
}
stages {
stage("Test") {
steps {
script {
MYPODS.tokenize(',').each {
println "Item: ${it}"
}
}
}
}
}
}
输出:
[Pipeline] node
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Item: pod1
[Pipeline] echo
Item: pod2
[Pipeline] echo
Item: pod3
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS