Unknown stage section "withKubeConfig" in Jenkins



我将使用jenkins CICD将docker映像部署到k8s集群。

我在Jenkins中安装了Kubernetes CLI和SSH Agent .

我使用了下面的代码。

stage('List pods') {
withKubeConfig([credentialsId: 'kubectl-user']) {
sh 'curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.20.5/bin/linux/amd64/kubectl"'  
sh 'chmod u+x ./kubectl'  
sh './kubectl get pods -n dev'
}
}

然后,我得到下面的错误。

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 39: Unknown stage section "withKubeConfig". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. @ line 39, column 2.
stage('List pods') {
^
WorkflowScript: 39: Expected one of "steps", "stages", or "parallel" for stage "List pods" @ line 39, column 2.
stage('List pods') {
^
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:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:571)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:523)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)

我错过什么了吗?

看起来您在声明性管道中使用脚本管道语法,因此您看到了错误。

如果你想使用声明式管道语法,你必须遵循严格的格式指南,在你的情况下,你错过了stage指令下的steps指令。
你的代码应该看起来像:

pipeline {
agent any
stages {
stage('List Pods') {
steps {
withKubeConfig([credentialsId: 'kubectl-user']) {
sh 'curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.20.5/bin/linux/amd64/kubectl"'
sh 'chmod u+x ./kubectl'
sh './kubectl get pods -n dev'
}
}
}
}
}

如果你想使用脚本化的管道语法,它看起来像:

node {
stage('List Pods') {
withKubeConfig([credentialsId: 'kubectl-user']) {
sh 'curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.20.5/bin/linux/amd64/kubectl"'
sh 'chmod u+x ./kubectl'
sh './kubectl get pods -n dev'
}
}
}

你可以在这里阅读更多关于两种管道语法差异的信息。

相关内容

  • 没有找到相关文章

最新更新