Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declarative Jenkins Pipeline use existing Kubernetes Pod Template

I am using the OpenShift Jenkins image within an OpenShift Cluster. This default Jenkins image results in a Jenkins container that is preconfigured to point to my Kubernetes cluster. Additionally, the container has two Kubernetes pod templates defined, one for maven and one for nodejs.

enter image description here

enter image description here

What I would now like to do is use a declarative pipeline and reference these pods. I tried the following

  agent {
     kubernetes {
     //cloud 'kubernetes'
     label 'maven'
     }
  }

But that gives an error stating

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

WorkflowScript: 4: Missing required parameter for agent type "kubernetes": containerTemplate @ line 4, column 10.

        kubernetes {

        ^

All of the (examples) that I can find for declarative pipelines show the pod templates being defined when the agent is specified.

Is it possible to reuse already defined templates in a declarative pipeline?

like image 596
ssc327 Avatar asked Apr 23 '18 18:04

ssc327


People also ask

What is POD template in Jenkins?

Pods used to implement Jenkins pipelines. We implement CI/CD pipelines using declarative Jenkins pipelines using a Jenkinsfile in the source of each application or environment git repository.

What is Jnlp container?

The jnlp container takes care of the declarative checkout source code management (SCM) action. Unless otherwise specified, all other actions are executed in the Jenkins pipeline workspace. Figure 2 shows more of the Jenkinsfile, where we now begin to build the Java application.


1 Answers

Here is an example using a pre-defined pod template.

pipeline {
  agent {
      label "maven"
  }
  stages {
    stage('Run maven') {
      steps {
        sh 'mvn -version'
      }
    }
  }
}

Your original pipeline definition was in effect defining a brand new pod template and hence the error enforcing the requirement for containerTeamplates parameter. When using an existing template, you can simply specify the label in the agent block.

like image 145
abn Avatar answered Sep 20 '22 12:09

abn