简体   繁体   English

Jenkins Kube.netes pod 没有指定container('name')时使用什么容器?

[英]Jenkins Kubernetes pod what container is used when no container('name') is specified?

I have installed Jenkins from here https://charts.jenkins.io , reading Kube.netes Plugin page https://plugins.jenkins.io/kube.netes/ you can define a Jenkinsfile as我已经从这里安装了 Jenkins https://charts.jenkins.io ,阅读 Kube.netes 插件页面https://plugins.jenkins.io/kube.netes/你可以定义为一个 Jenkins 文件

podTemplate(yaml: '''
    apiVersion: v1
    kind: Pod
    spec:
      containers:
      - name: maven
        image: maven:3.8.1-jdk-8
        command:
        - sleep
        args:
        - 99d
      - name: golang
        image: golang:1.16.5
        command:
        - sleep
        args:
        - 99d
''') {
  node(POD_LABEL) {
    stage('Get a Maven project') {
      git 'https://github.com/jenkinsci/kubernetes-plugin.git'
      container('maven') {
        stage('Build a Maven project') {
          sh 'mvn -B -ntp clean install'
        }
      }
    }

    stage('Get a Golang project') {
      git url: 'https://github.com/hashicorp/terraform-provider-google.git', branch: 'main'
      container('golang') {
        stage('Build a Go project') {
          sh '''
            mkdir -p /go/src/github.com/hashicorp
            ln -s `pwd` /go/src/github.com/hashicorp/terraform
            cd /go/src/github.com/hashicorp/terraform && make
          '''
        }
      }
    }

  }
}

The question is, if you enclose instructions inside a container('name') the instruction will be run with that container.问题是,如果您将指令包含在容器 ('name') 中,指令将与该容器一起运行。 But, if you do not specify a container, it works.但是,如果您不指定容器,它就可以工作。 I suppose that a random container is chosen to run the sh command?我假设选择了一个随机容器来运行 sh 命令? For example:例如:

    stage('example stage') {
          sh 'echo "hello world"'
    }

Thanks谢谢

With declarative pipelines you can declare the pod definition and use defaultContainer to set the default.使用声明式管道,您可以声明 pod 定义并使用defaultContainer设置默认值。 The Jenkins Kube.netes plugin will also inject a jnlp container in all pod definitions that runs the Jenkins agent node, and that is the default container. Jenkins Kube.netes 插件还将在所有运行 Jenkins 代理节点的 pod 定义中注入一个jnlp容器,这是默认容器。

You can check on the console log of jobs using the K8s plugin what is the final pod definition that is requested and instantiated by the cluster.您可以使用 K8s 插件检查作业的控制台日志,集群请求和实例化的最终 Pod 定义是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM