问题
Here, I need to execute both Parallel test 1
and Parallel test 2
at the same time.
When I tried to put a parallel block on top of these, it throws an error since it mentioned like this in the official site Note: that a stage must have one and only one of steps, stages, or parallel
.
pipeline {
agent any
stages {
stage('Parallel Test 1') {
parallel {
stage('Block 1 - Stage 1') {
steps {
echo "Block 1 - Stage 1"
build(job: 'jenkins_job_1')
}
}
stage('Block 1 - Stage 2') {
steps {
echo "Block 1 - Stage 2"
build(job: 'jenkins_job_2')
}
}
}
}
stage('Parallel Test 2') {
parallel {
stage('Block 2 - Stage 1') {
steps {
echo "Block 2 - Stage 1"
build(job: "jenkins_job_3")
}
}
stage('Block 2 - Stage 2') {
steps {
echo "Block 2 - Stage 2"
build(job: "jenkins_job_4")
}
}
}
}
}
}
回答1:
You don't have to put each call to a parallel-job inside a stage, so you can do it as such:
pipeline {
agent any
stages {
stage('single run') {
parallel {
stage('Parallel Test 1') {
steps {
script {
def group1 = [:]
group1["test_1"] = {
echo "test_1"
sh(script: "date -u")
build(job: 'jenkins_job_1')
}
group1["test_2"] = {
echo "test_2"
sh(script: "date -u")
build(job: 'jenkins_job_2')
}
parallel group1
}
}
}
stage('Parallel Test 2') {
steps {
script {
def group2 = [:]
group2["test_3"] = {
echo "test_3"
sh(script: "date -u")
build(job: 'jenkins_job_3')
}
group2["test_4"] = {
echo "test_4"
sh(script: "date -u")
build(job: 'jenkins_job_4')
}
parallel group2
}
}
}
}
}
}
}
来源:https://stackoverflow.com/questions/53458217/how-to-run-multi-stages-at-the-same-time-using-multi-parallel-blocks