[英]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.