【问题标题】:How to define multiple containers in declarative pipeline?如何在声明性管道中定义多个容器?
【发布时间】:2018-05-08 07:19:08
【问题描述】:

我使用的是kuberntes-plugin。在其 README 中,它给出了如何使用多个容器映像编写脚本管道,例如

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') { 

我尝试了以下声明性管道。

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

      }
    }
  }

它创建一个只有一个容器的 pod。

如何在声明式管道中使用多个容器模板?

【问题讨论】:

  • 您找到解决方案了吗?
  • 使用脚本化管道。

标签: jenkins kubernetes jenkins-pipeline jnlp


【解决方案1】:

这不是解决您问题的方法,而是我查找后发现的一些信息。

KubernetesDeclarativeAgent 只有一个 containerTemplate。无论containerTemplate 位于容器集合底部的哪一个都将被使用。

在您的示例中,它将是 containtertwo

您不能有多个顶级agents,并且您不能在一个代理中拥有多个kubernetes。现在你不能有多个容器。如果为此引发某种错误或警告,我更愿意。

我能想到两种解决方法。如果您必须使用声明式,那么您可以将agent 添加到您的stage,但这可能会导致其自身的问题。另一个是脚本化的管道,这是我要做的。

这方面的文档还有很多不足之处。

【讨论】:

    【解决方案2】:

    您可以在 pod 模板文件的帮助下实现这一点。我使用以下一个在 kubernetes 上部署我的应用程序:

    apiVersion: v1
    kind: Pod
    metadata:
      labels:
        label: docker
    spec:
      containers:
      - name: docker
        image: jenkins/jnlp-agent-docker
        command:
        - cat
        tty: true
        volumeMounts:
        - mountPath: '/var/run/docker.sock'
          name: docker-socket
      - name: kubectl
        image: bitnami/kubectl
        command:
        - cat
        tty: true
      volumes:
      - name: docker-socket
        hostPath:
          path: '/var/run/docker.sock'
      securityContext:
        runAsUser: 0
    

    然后在声明式管道中使用它:

    stage('Deploy') {
      when {
        anyOf { branch 'master'; tag '' }
      }
      agent {
        kubernetes {
          defaultContainer 'kubectl' // All `steps` instructions will be executed by this container
          yamlFile 'path/to/pod/template.yaml'
        }
      }
      steps {
        container('docker') {
          sh 'echo This is executed in the docker container'
        }
      }
    }
    

    您也可以在 Jenkinsfile 中使用 yaml 选项而不是 yamlFile 指定模板,您只需要在此处使用多行字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-01
      • 2017-11-02
      • 2022-06-20
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      相关资源
      最近更新 更多