Run Jenkins Pipeline on Agent

Run Jenkins Pipeline on Agent

Implementing Pipelines in a Master-Slave Setup

Here's how you can leverage the master-slave architecture for Jenkins pipelines:

  1. Node Labeling: When configuring slave nodes, it's a good idea to label them based on capabilities. For instance, if a slave node is configured specifically for Java-based projects, label it as 'Java'. This enables more precise targeting in pipelines.
node('Java') {
    // Your pipeline tasks for Java projects
}
  1. Resource Isolation: Some projects might require specific tools, environments, or configurations. By assigning specific tasks to dedicated slaves, you can isolate resource usage and ensure that every job runs in its optimal environment.

  2. Parallel Execution: Jenkins pipelines can harness the power of multiple slaves to execute tasks in parallel. This not only speeds up the overall build process but also allows for simultaneous testing in different environments.

parallel {
    stage('Test on Linux') {
        agent { label 'linux' }
        steps {
            // Testing steps - Linux
        }
    }
    stage('Test on Windows') {
        agent { label 'windows' }
        steps {
            // Testing steps - windows
        }
    }
}

Let's understand Jenkins Agent Pipeline using the following example. It is straight-forward!

I'm creating new pipeline project and writing its description as mentioned below.

In the pipeline, select Definition - Pipeline Script, Write script where we mention agent with label as agent {label 'agent'} and write the script as usual.

pipeline {
agent {label 'agent'}

stages {
    stage('Hello') {
        steps {
            echo 'Hello World'
        }
    }
  }
}

Click on OK and Build this pipeline. In the console output it is clearly mentioned that Running on jenkins-agent

Now, Let's try this in real scenario. In the Github repository https://github.com/kshitijabartakke/calculategrowth.git if you see properly in below image agent any.

I will change it to agent {label 'agent'} and commit changes.

My pipeline look like this

pipeline {
    agent {label 'agent'}
        stages{
        stage("Git Clone"){
            steps{
                git url: "https://github.com/kshitijabartakke/calculategrowth.git", branch: "main"
            }
        }
        stage("Create Docker Image"){
            steps{
        echo "Build and Test"
        withCredentials([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
        sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                sh "docker build -t kshitibartakke/calculategrowth ."
        }
            }
        }
         stage("Push DockerImage to DockrHub"){
            steps{
                withCredentials([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                sh "docker images"
        sh "docker push kshitibartakke/calculategrowth:latest"
        }
        }
    }
        stage("Deploy Using DOcker Compose"){
            steps{   
         sh "docker-compose down && docker-compose up -d"           
                }
            }
        }
    }

When we run the pipeline, see it is showing 'Running on jenkins-agent' ๐Ÿ˜€

That's all you need to know!

Thanks for Reading! ๐Ÿ™

ย