Unknown stage section "withKubeConfig" in Jenkins

8/14/2021

I'm going to deploy my docker image to the k8s cluster using jenkins CICD.

I installed Kubernetes CLI and SSH Agent in Jenkins.

I used the below code.

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'
    }
  }

And, I'm getting the below error.

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 astepsblock. @ 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)

Have I missed anything?

-- HiddenFace
jenkins
jenkins-pipeline
kubernetes

1 Answer

8/15/2021

It seems like you are using scripted pipeline syntax inside a declarative pipeline and therefore you are seeing the error.

If you want to use declarative pipeline syntax you must follow the strict formatting guidelines, in your case you are missing the steps directive under the stage directive.
Your code should look something like:

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'
               }
           }
       }
    }
}

If you want to use the scripted pipeline syntax it will look something like:

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'
        }
    }
}

You can read more here about the differences between the two pipelines syntaxes.

-- Noam Helmer
Source: StackOverflow