Directive recap
| Directive | Job |
|---|---|
agent | Where stages run |
environment | Env vars / credential bindings |
stages / stage | Logical phases |
steps | Shell / Groovy actions |
post | Cleanup and notifications |
when | Conditional stage execution |
Sequential vs parallel
Default is sequential — each stage waits for the previous one.
stages {
stage('Install') { steps { sh 'npm ci' } }
stage('Test') { steps { sh 'npm test' } }
stage('Build') { steps { sh 'npm run build' } }
}Parallel for independent work:
stage('Quality') {
parallel {
stage('Lint') { steps { sh 'npm run lint' } }
stage('Unit') { steps { sh 'npm test' } }
}
}Docker agent
Run a stage (or whole pipeline) inside a container image:
stage('Install & Test') {
agent {
docker { image 'node:18-alpine' }
}
steps {
sh 'npm install'
sh 'npm test'
}
}Benefits:
- Exact Node version every build
- No permanent
apt install nodejsinside Jenkins - Clean workspace environment
Docker agents need the Jenkins host (or agent) to speak to a Docker daemon. Day 3 Project 3 covers the Docker-in-Docker socket mount when Jenkins itself is a container.
when directive
Skip stages unless a condition matches:
when { branch 'master' }
when { branch 'main' }
when { not { branch 'main' } }
when { anyOf { branch 'main'; branch 'staging' } }
when { environment name: 'DEPLOY_TO', value: 'prod' }
when { expression { return params.RUN_DEPLOY == true } }Day 4 Multibranch uses when { branch 'master' } so only production branches build and push Docker images.
Mental model for Project 3
GitHub
↓
Jenkins
↓
Checkout
↓
Install & Test (often on a Node Docker agent)
↓
Build Docker Image
↓
Push to Docker Hub
↓
Deploy Application