Hands-on Lab

#Lab: Jenkins Declarative Pipeline

Build CI/CD pipelines with Jenkins.

#🎯 Objectives

  • Create declarative Jenkinsfile
  • Implement multi-stage pipeline
  • Use shared libraries
  • Configure parallel stages

#📋 Prerequisites

  • Jenkins server running
  • Pipeline plugin installed

#⏱️ Duration: 45 minutes


#Task 1: Basic Jenkinsfile (10 min)

Create Jenkinsfile:

groovy
1pipeline {
2    agent any
3    
4    environment {
5        APP_NAME = 'myapp'
6        DOCKER_REGISTRY = 'docker.io'
7    }
8    
9    stages {
10        stage('Checkout') {
11            steps {
12                checkout scm
13            }
14        }
15        
16        stage('Build') {
17            steps {
18                sh 'npm ci'
19                sh 'npm run build'
20            }
21        }
22        
23        stage('Test') {
24            steps {
25                sh 'npm test'
26            }
27            post {
28                always {
29                    junit 'test-results/*.xml'
30                }
31            }
32        }
33        
34        stage('Deploy') {
35            when {
36                branch 'main'
37            }
38            steps {
39                sh 'echo "Deploying..."'
40            }
41        }
42    }
43    
44    post {
45        success {
46            echo 'Pipeline succeeded!'
47        }
48        failure {
49            echo 'Pipeline failed!'
50        }
51    }
52}

#Task 2: Docker Pipeline (15 min)

groovy
1pipeline {
2    agent any
3    
4    environment {
5        DOCKER_IMAGE = "${DOCKER_REGISTRY}/${APP_NAME}:${BUILD_NUMBER}"
6        DOCKER_CREDENTIALS = credentials('docker-hub')
7    }
8    
9    stages {
10        stage('Build Docker Image') {
11            steps {
12                script {
13                    docker.build(DOCKER_IMAGE)
14                }
15            }
16        }
17        
18        stage('Test Image') {
19            steps {
20                script {
21                    docker.image(DOCKER_IMAGE).inside {
22                        sh 'npm test'
23                    }
24                }
25            }
26        }
27        
28        stage('Push Image') {
29            when {
30                branch 'main'
31            }
32            steps {
33                script {
34                    docker.withRegistry('https://docker.io', 'docker-hub') {
35                        docker.image(DOCKER_IMAGE).push()
36                        docker.image(DOCKER_IMAGE).push('latest')
37                    }
38                }
39            }
40        }
41    }
42}

#Task 3: Parallel Stages (10 min)

groovy
1pipeline {
2    agent any
3    
4    stages {
5        stage('Build') {
6            steps {
7                sh 'npm ci'
8            }
9        }
10        
11        stage('Quality Gates') {
12            parallel {
13                stage('Unit Tests') {
14                    steps {
15                        sh 'npm run test:unit'
16                    }
17                }
18                stage('Integration Tests') {
19                    steps {
20                        sh 'npm run test:integration'
21                    }
22                }
23                stage('Lint') {
24                    steps {
25                        sh 'npm run lint'
26                    }
27                }
28                stage('Security Scan') {
29                    steps {
30                        sh 'npm audit'
31                    }
32                }
33            }
34        }
35        
36        stage('Deploy') {
37            steps {
38                sh 'echo "Deploying..."'
39            }
40        }
41    }
42}

#Task 4: Environment Deployments (10 min)

groovy
1pipeline {
2    agent any
3    
4    stages {
5        stage('Build') {
6            steps {
7                sh 'npm ci && npm run build'
8            }
9        }
10        
11        stage('Deploy to Staging') {
12            steps {
13                script {
14                    deploy('staging')
15                }
16            }
17        }
18        
19        stage('Approval') {
20            when {
21                branch 'main'
22            }
23            steps {
24                input message: 'Deploy to production?', ok: 'Deploy'
25            }
26        }
27        
28        stage('Deploy to Production') {
29            when {
30                branch 'main'
31            }
32            steps {
33                script {
34                    deploy('production')
35                }
36            }
37        }
38    }
39}
40
41def deploy(environment) {
42    echo "Deploying to ${environment}"
43    // Add deployment logic here
44}

#✅ Success Criteria

  • Basic Jenkinsfile created
  • Docker build pipeline working
  • Parallel stages configured
  • Manual approval gate implemented