Skip to main content

Complete Jenkinsfile

Below is the complete Jenkinsfile used in this CI/CD pipeline:
pipeline {
    agent any

    environment {
        IMAGE_NAME = 'nodejs-demo-app'
    }

    stages {
        stage('Clone') {
            steps {
                echo 'Cloning the repository...'
                git branch: 'main', url: 'https://github.com/mani-6666/Jenkins-Pipeline-for-CI-CD.git'
            }
        }

        stage('Build') {
            steps {
                echo 'Building Docker image...'
                sh 'docker build -t $IMAGE_NAME:latest .'
                echo 'Docker image built successfully!'
            }
        }

        stage('Test') {
            steps {
                echo 'Running tests...'
                sh 'npm install'
                sh 'npm test || echo "No tests defined or test failed, continuing..."'
            }
        }

        stage('Deploy') {
            steps {
                echo 'Deploying Docker container...'
                sh 'docker run --name Jenkins -d -p 80:3000 $IMAGE_NAME:latest'
                echo 'App deployed on port 80!'
            }
        }
    }

    post {
        always {
            echo 'Pipeline completed!'
        }
        success {
            echo 'CI/CD pipeline ran successfully!'
        }
        failure {
            echo 'Pipeline failed. Check logs for details.'
        }
    }
}

Pipeline Block Structure

Agent Declaration

agent any
The agent any directive tells Jenkins to execute this pipeline on any available agent. For production environments, consider using specific agent labels to ensure consistent build environments.

Environment Variables

environment {
    IMAGE_NAME = 'nodejs-demo-app'
}
IMAGE_NAME
string
default:"nodejs-demo-app"
Defines the Docker image name used in the Build and Deploy stages. This variable is accessible throughout the pipeline using $IMAGE_NAME.

Stages Breakdown

stage('Clone') {
    steps {
        echo 'Cloning the repository...'
        git branch: 'main', url: 'https://github.com/mani-6666/Jenkins-Pipeline-for-CI-CD.git'
    }
}
Purpose: Clones the source code from the GitHub repository.Key Parameters:
  • branch: Specifies the main branch
  • url: GitHub repository URL
The git checkout happens in the Jenkins workspace directory automatically.

Post-Build Actions

post {
    always {
        echo 'Pipeline completed!'
    }
    success {
        echo 'CI/CD pipeline ran successfully!'
    }
    failure {
        echo 'Pipeline failed. Check logs for details.'
    }
}

Post Conditions

Executes every time the pipeline runs, regardless of success or failure.Use cases:
  • Cleanup operations
  • Notifications
  • Logging
  • Resource deallocation

Customization Options

Adding Credentials

For private repositories, add credentials:
stage('Clone') {
    steps {
        git branch: 'main', 
            url: 'https://github.com/your-org/repo.git',
            credentialsId: 'github-credentials'
    }
}

Environment-Specific Deployment

environment {
    IMAGE_NAME = 'nodejs-demo-app'
    DEPLOY_ENV = 'production'
    PORT = '80'
}

Adding Build Parameters

parameters {
    string(name: 'BRANCH_NAME', defaultValue: 'main', description: 'Branch to build')
    choice(name: 'ENVIRONMENT', choices: ['dev', 'staging', 'prod'], description: 'Target environment')
}

Stage Details

Detailed documentation of each pipeline stage

Deployment Process

Learn about Docker deployment and port mapping

Build docs developers (and LLMs) love