-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparallel-stages.groovy
77 lines (66 loc) · 2.92 KB
/
parallel-stages.groovy
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Here is an example of a Jenkinsfile with parallel stages. This example runs the "Test" and "Build" stages in parallel, then proceeds to the "Deploy" stage.
Jenkinsfile with Parallel Stages
```groovy */
pipeline {
agent any
stages {
stage('Parallel Stages') {
parallel {
stage('Build') {
agent {
label 'build-agent'
}
steps {
echo 'Building...'
// Use the appropriate build tool here, e.g., Maven, Gradle, npm, etc.
sh 'mvn clean package'
}
}
stage('Test') {
agent {
label 'test-agent'
}
steps {
echo 'Testing...'
// Run your tests here
sh 'mvn test'
}
}
}
}
stage('Deploy') {
agent {
label 'deploy-agent'
}
steps {
echo 'Deploying...'
// Deploy the application, e.g., using Kubernetes, Docker, etc.
sh 'kubectl apply -f deployment.yaml'
}
}
}
}
```
/* Explanation
1. Global Agent Block:
- `agent any`: Specifies that any available agent can be used for this pipeline. This can be overridden at the stage level.
2. Parallel Stages Block:
- `stage('Parallel Stages')`: Defines a stage that will contain parallel sub-stages.
- `parallel { ... }`: Within this block, stages defined will run in parallel.
3. Build Stage:
- `stage('Build')`: A sub-stage within the parallel block.
- `agent { label 'build-agent' }`: Specifies the agent labeled 'build-agent' for the "Build" stage.
- `steps { ... }`: Contains the steps to be executed in the "Build" stage, such as running a Maven build command.
4. Test Stage:
- `stage('Test')`: Another sub-stage within the parallel block.
- `agent { label 'test-agent' }`: Specifies the agent labeled 'test-agent' for the "Test" stage.
- `steps { ... }`: Contains the steps to be executed in the "Test" stage, such as running tests.
5. Deploy Stage:
- `stage('Deploy')`: Defines a stage named "Deploy".
- `agent { label 'deploy-agent' }`: Specifies the agent labeled 'deploy-agent' for the "Deploy" stage.
- `steps { ... }`: Contains the steps to be executed in the "Deploy" stage, such as using `kubectl` to apply a Kubernetes deployment configuration.
Summary
In this Jenkinsfile:
- The "Build" and "Test" stages are defined within a parallel block and will run concurrently.
- Once both parallel stages are completed, the pipeline proceeds to the "Deploy" stage.
- Each stage can have its own specific agent, optimizing resource utilization and ensuring the appropriate environment is used for each task. */