如何在声明式管道中定义多个容器?

问题描述 投票:0回答:3

我正在使用kuberntes-plugin。在其自述文件中,它给出了如何使用多个容器映像编写脚本化管道,例如

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。

如何通过声明性管道使用多个containerTemplates?

jenkins kubernetes jenkins-pipeline jnlp
3个回答
2
投票

这不是解决你的问题,而是我查看后发现的一些信息。

KubernetesDeclarativeAgent 只有一个

containerTemplate
。无论您收集的容器底部有哪个
containerTemplate
,都将使用该容器。

在您的示例中,它将是

containtertwo

您不能有多个顶级

agents
,并且代理内不能有多个
kubernetes
。现在你不能拥有多个容器。我希望如果为此抛出某种错误或警告。

我能想到两种解决方法。如果必须使用声明性,则可以将

agent
添加到
stage
中,但这可能会导致其自身的问题。另一个是脚本化管道,这就是我要做的。

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


0
投票

您可以借助 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'
    }
  }
}

您还可以借助

yaml
选项(而不是
yamlFile
)在 Jenkinsfile 中指定模板,只需在那里使用多行字符串即可。


0
投票

是的,我无法使用多个containerTemplate。

我看到有一种叫做“containerTemplates”的东西,我什至看到它只允许其中有一个containerTemplate。

© www.soinside.com 2019 - 2024. All rights reserved.