Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define multiple containers in declarative pipeline?

I was using kuberntes-plugin. In its README it has given how to write scripted pipeline with multiple container images, like

podTemplate(label: 'mypod', containers: [
    containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
    containerTemplate(name: 'golang', image: 'golang:1.8.0', ttyEnabled: true, command: 'cat')
  ]) {
    node('mypod') { 

I tried the following for declarative pipeline.

pipeline {
  agent {
    kubernetes {
      //cloud 'kubernetes'
      label 'mypod'
      containerTemplate {
        name 'maven'
        image 'maven:3.3.9-jdk-8-alpine'
        ttyEnabled true
        command 'cat'
      }
      containerTemplate {
        name 'containtertwo'
        image 'someimage'
        ttyEnabled true

      }
    }
  }

It creates a pod with only one container.

how to use multiple containerTemplates with declarative pipeline?

like image 204
chandan Avatar asked Nov 24 '17 09:11

chandan


1 Answers

This isnt a solution to your problem, but is some information I found after looking.

The KubernetesDeclarativeAgent only has a single containerTemplate. Whichever containerTemplate is at the bottom of your collection of containers will be the one that is used.

In your example it will be containtertwo.

You cant have multiple top level agents, and you cant have multiple kubernetes within an agent. And now you cant have multiple containers. I would prefer if an error or warning of some kind was thrown for this.

There are 2 work arounds I can think of. If you must use declarative, then you can add an agent to your stage, but this can lead to its own issues. The other is the scripted pipeline, which is what I am going to do.

The documentation on this leaves much to be desired.

like image 168
maffo Avatar answered Nov 15 '22 23:11

maffo