简体   繁体   English

如何在隔离的 Pod 中为声明性 jenkins 管道运行并行阶段

[英]How to run parallel stages in isolated pods for a declarative jenkins pipeline

I'm trying to run some end-to-end tests in parallel and on DIFFERENT kubernetes pods on a declarative jenkins pipeline, jenkins however seems to attempt running the parallel stages on the SAME kubernetes pod.我正在尝试在声明性 jenkins 管道上的不同kubernetes pod 上并行运行一些端到端测试,但是 jenkins 似乎尝试在SAME kubernetes pod 上运行并行阶段。 This leads to database deadlocks since both processes try inserting/truncating/updating/querying the same tables.这会导致数据库死锁,因为两个进程都尝试插入/截断/更新/查询相同的表。 Is there a way I can spin up a different pod for each of the parallel stages?有没有办法可以为每个并行阶段启动不同的吊舱?

The kubernetes-plugin config: kubernetes-plugin 配置:

  agent {
  kubernetes {
    label 'my-label'
    defaultContainer 'jnlp'
    yaml """
apiVersion: v1
kind: Pod
metadata:
  name: dind
spec:
  containers:
    - name: < default container >
      image: < image >
      securityContext:
          privileged: true
          fsGroup: 1000
      command:
      - cat
      tty: true
      volumeMounts:
        - name: jenkins-bundle-gems
          mountPath: /usr/local/bundle


    - name: <tests-container-name>
      image: < image > 
      securityContext:
          privileged: true
          fsGroup: 1000
      volumeMounts:
        - name: jenkins-bundle-gems
          mountPath: /usr/local/bundle
      command:
      - cat
      tty: true
"""
 }
   }

The parallel stage:平行阶段:

      stage('Test'){
        parallel {
          stage("Branch 1") {
              steps {
                container('<tests-container-name>') {
                  sh "jenkins/scripts/initdb.sh"
                  sh 'bundle exec rspec --exclude-pattern "spec/features/*_spec.rb" spec'
                }
              }
            }
            stage("Branch 2") {
              steps {
                container('<tests-container-name>') {
                  sh "jenkins/scripts/initdb.sh"
                  sh "bundle exec rspec `jenkins/scripts/split_features.sh 0`"
                }
              }
            }
        }
      }

EXPECTATION: I would like for jenkins to spin up two different pods for each of the parallel stages.期望:我希望 jenkins 为每个并行阶段启动两个不同的 pod。 This would allow me to use different databases for each of the tests.这将允许我为每个测试使用不同的数据库。

ACTUAL RESULT: Jenkins runs both stages concurrently on the same pod.实际结果: Jenkins 在同一个 Pod 上同时运行两个阶段。

Try something like this尝试这样的事情

stage("Run additional parallel tests") {
        parallel( 
            "parallel stage 1": {
                [INSERT YOUR CODE HERE]
            },

            "parallel stage 2": {
                [INSERT YOUR CODE HERE]
            }
        )
    }
}

您可以为每个并行阶段设置agent {}以在每个阶段启动一个 pod。

You have__________: stage > parallel > stage > steps .你有__________: stage > parallel > stage > steps

You need to have also: stage > parallel > stage > agent .您还需要: stage > parallel > stage > agent

Do not repeat pod definition twice, it is recommended to put the pod definition in a separated file and refer to it using yamlFile instead of yaml :不要重复 pod 定义两次,建议将 pod 定义放在一个单独的文件中,并使用yamlFile而不是yaml来引用它:

  stage('Test'){
    parallel {
      stage("Branch 1") {
          agent {
             kubernetes {
               defaultContainer 'jnlp'
               yamlFile 'Jenkins.pod.yaml'
             }
          }
          steps {
            container('<tests-container-name>') {
              sh "jenkins/scripts/initdb.sh"
              sh 'bundle exec rspec --exclude-pattern "spec/features/*_spec.rb" spec'
            }
          }
        }
        stage("Branch 2") {
          agent {
             kubernetes {
               defaultContainer 'jnlp'
               yamlFile 'jenkins.pod.yaml'
             }
          }
          steps {
            container('<tests-container-name>') {
              sh "jenkins/scripts/initdb.sh"
              sh "bundle exec rspec `jenkins/scripts/split_features.sh 0`"
            }
          }
        }
    }
  }

Hint暗示

if blueocean is one of your plugins, it will help you to draw your pipeline under http://HOST/blue/organizations/jenkins/pipeline-editor/ , then you can copy the Jenkinsfile code by typing [Cmd + s]如果 blueocean 是您的插件之一,它将帮助您在http://HOST/blue/organizations/jenkins/pipeline-editor/下绘制管道,然后您可以通过键入 [Cmd + s] 复制 Jenkinsfile 代码

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

相关问题 如何在 Jenkins 声明式管道中并行运行有和没有矩阵的阶段? - How to run stages with and without matrix in parallel in Jenkins declarative-pipeline? Jenkins Declarative Pipeline并行阶段 - Jenkins Declarative Pipeline parallel stages 詹金斯:如何在詹金斯声明式管道中实现并行动态阶段 - Jenkins:How to Achieve parallel dynamic stages in jenkins declarative pipeline 如何在 Jenkins 声明式管道中循环参数化并行阶段? - How to loop parametrized parallel stages in Jenkins declarative pipeline? 如何使用声明性Jenkins管道在同一节点上运行多个阶段? - How to run multiple stages on the same node with declarative Jenkins pipeline? 如何锁定声明性 Jenkins 管道的多个阶段? - How to lock multiple stages of declarative Jenkins pipeline? 是否可以在循环中创建并行的Jenkins声明管道阶段? - Is it possible to create parallel Jenkins Declarative Pipeline stages in a loop? 在循环 Jenkins 声明式管道中创建嵌套的并行阶段 - create nested parallel stages in a loop Jenkins declarative pipeline 具有强制阶段的 Jenkins 声明式管道 - Jenkins Declarative Pipeline with Mandatory stages 使用声明性语法跨不同节点运行Jenkins并行阶段 - Run Jenkins parallel stages across different nodes using declarative syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM