Problem Statement
Explain advanced parallel execution patterns in Jenkins Pipeline. Include nested parallel stages, failFast, matrix builds, and resource management considerations.
Explanation
Basic parallel execution runs independent stages concurrently reducing total pipeline time. Example:
```groovy
stage('Tests') {
parallel {
stage('Unit Tests') {
steps { sh 'npm test' }
}
stage('Integration Tests') {
steps { sh 'npm run test:integration' }
}
stage('Lint') {
steps { sh 'npm run lint' }
}
}
}
```
FailFast option aborts all parallel stages if any fails, useful when one failure makes others irrelevant:
```groovy
stage('Tests') {
failFast true
parallel {
stage('Unit Tests') {
steps { sh 'npm test' }
}
stage('Integration Tests') {
steps { sh 'npm run test:integration' }
}
}
}
```
Without failFast, all parallel stages run to completion even if some fail.
Nested parallel stages enable complex concurrency patterns:
```groovy
stage('Build and Test') {
parallel {
stage('Backend') {
stages {
stage('Backend Build') {
steps { sh 'mvn package' }
}
stage('Backend Test') {
steps { sh 'mvn test' }
}
}
}
stage('Frontend') {
stages {
stage('Frontend Build') {
steps { sh 'npm run build' }
}
stage('Frontend Test') {
steps { sh 'npm test' }
}
}
}
}
}
```
Backend and Frontend run in parallel, each with sequential internal stages.
Matrix builds (Jenkins 2.22+) run same stage with different parameter combinations:
```groovy
stage('Test') {
matrix {
axes {
axis {
name 'PLATFORM'
values 'linux', 'windows', 'mac'
}
axis {
name 'BROWSER'
values 'chrome', 'firefox', 'safari'
}
}
stages {
stage('Test') {
steps {
sh "./test.sh ${PLATFORM} ${BROWSER}"
}
}
}
}
}
```
Creates 9 parallel executions testing all platform-browser combinations.
Resource management considerations: each parallel stage requires agent/executor, so parallel stages = required executors. If insufficient executors available, stages queue. Use agent labels for specific requirements:
```groovy
parallel {
stage('Linux Build') {
agent { label 'linux' }
steps { sh 'make' }
}
stage('Windows Build') {
agent { label 'windows' }
steps { bat 'build.bat' }
}
}
```
Shared resources (databases, external services) may have concurrency limits. Use lock step to synchronize access:
```groovy
parallel {
stage('Test Suite 1') {
steps {
lock(resource: 'test-db', quantity: 1) {
sh 'npm run test:suite1'
}
}
}
stage('Test Suite 2') {
steps {
lock(resource: 'test-db', quantity: 1) {
sh 'npm run test:suite2'
}
}
}
}
```
Ensures only one stage accesses test-db at a time.
Best practices: use parallel for independent tasks only (no shared state), set timeouts to prevent hanging, use failFast when appropriate, monitor executor utilization (avoid over-parallelization), consider cost (cloud agents charge per executor), implement resource locks for shared resources, use matrix for combinatorial testing. Understanding advanced parallel patterns optimizes pipeline performance while managing resources effectively.