pipeline并行任务配置

静态并行任务配置

声明式

  1. stage中可以通过parallel块来嵌套多个stage实现并行运行
  2. parallel块中的stage除了不能再次嵌套parallel外和普通stage一样,也可以通过stages包含一些列顺序执行的stage
  3. 每个stage中有且只能有一个stepsstages或者parallel
  4. 所有包含parallelstage都不能包含agenttools

EXAMPLE-1 stage级别并行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pipeline {
agent any
options {
timestamps()
}
stages {
stage('init') {
steps {
echo "init start"
sleep 5
echo 'init end'
}
}
stage('build') {
parallel {
stage('x86 build') {
steps {
echo 'x86 build start'
sleep 5
echo 'x86 build end'
}
}
stage('arm build') {
steps {
echo 'arm build start'
sleep 3
echo 'arm build end'
}
}
}
}
}
}

blueOcean如下图所示
example_1

EXAMPLE-2 并行stage中多个stage串行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
pipeline {
agent any
options {
timestamps()
}
stages {
stage('init') {
steps {
echo "init start"
sleep 5
echo 'init end'
}
}
stage('build') {
parallel {
stage('x86 build') {
agent {
label 'master'
}
steps {
echo 'x86 build start'
sleep 5
echo 'x86 build end'
}
}
stage('arm build') {
stages {
stage('arm-master build') {
steps {
echo 'arm master build start'
sleep 3
echo 'arm master build end'
}
}
stage('arm develop build') {
steps {
echo 'arm develop build start'
sleep 3
echo 'arm develop build end'
}
}
}
}
}
}
}
}

blueOcean如下图所示
example_2

EXAMPLE-4 step级别并行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
pipeline {
agent any
options {
timestamps()
}
stages {
stage('init') {
steps {
echo "init start"
sleep 5
echo 'init end'
}
}
stage('build') {
steps {
parallel 'x86 build': {
echo 'x86 build start'
sleep 3
echo 'x86 build end'
}, 'arm build': {
echo 'arm build start'
sleep 3
echo 'arm build end'
}
}
}
}
}

blueOcean如下图所示
example_4

脚本式

EXAMPLE-3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
script {
node('master') {
stage('init') {
echo 'init'
}
stage('build') {
parallel 'build x86': {
stage('build x86 step1') {
echo 'build x86 step1 start'
sleep 5
echo 'build x86 step1 end'
}
stage('build x86 step2') {
echo 'build x86 step 2 start'
sleep 5
echo 'build x86 step 2 end'
}
}, 'build arm': {
echo 'build arm start'
sleep 6
echo 'build arm end'
}
}
}
}

blueOcean如下图所示
example_3

动态创建并行任务

EXAMPLE-5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def jobs = ['jobA', 'jobB', 'jobC']

def parallelStagesMap = jobs.collectEntries { def jobName ->
["${jobName}", generateJobStage(jobName)]
}

def generateJobStage(String jobName) {
return {
node('master') {
stage("stage: ${jobName}") {
echo "${jobName} start"
sleep 5
echo "job end"
}
}
}
}

pipeline {
agent any
options {
timestamps()
}
stages {
stage('non-parallel stage') {
steps {
echo 'this is non-parallel stage'
}
}
stage('parallel stage') {
steps {
script {
parallel parallelStagesMap
}
}
}
}
}