How to write your first pipeline in Jenkinsfile

How to write your first pipeline in Jenkinsfile

Jenkinsfile:

A Jenkinsfile is a text file that contains the definition of a Jenkins pipeline. It allows you to automate the steps in your Continuous Integration and Continuous Delivery (CI/CD) process by scripting them in Groovy. The Jenkinsfile is version-controlled alongside your project, ensuring transparency and consistency in your pipeline configuration.

Declarative Pipeline Syntax:

pipeline {
    agent any
    stages {
        stage('Stage-Name') {
            steps {
                echo 'This is my first pipeline'
            }
        }
    }
}

Pipeline Structure and Components

1. pipeline Block

The top-level block wraps the entire Jenkinsfile and defines the pipeline. It specifies the environment and stages of the process.

2. agent any

  • Purpose: Specifies where the pipeline runs.

  • any: The pipeline can execute on any available agent (node) in the Jenkins setup. This provides flexibility if there are multiple agents in the environment. By default it runs on master server.


Stages

3. stage('Code')

  • Purpose: Represents the Build phase of the pipeline.

  • steps:

    • echo 'this is my first pipeline': Outputs a message to the Jenkins console log, simulating the build step.

    • In a real pipeline, this step might include commands to compile code or install dependencies.

Real-time pipelines to get Source Code from GitHub:

This pipeline is used to get the code from Github to CI server.

pipeline {
    agent any
    stages {
        stage('Code') {
            steps {
                git branch: "main", credentialsId: "github", url: 'https://github.com/devops0014/one.git'
            }
        }
    }
}

Multi Stage pipeline:

This pipeline contains multiple stages like code, build, test and deploy.

pipeline {
   agent any

   stages {
       stage('Hello') {
           steps {
               echo 'Hello World'
           }
       }
   stage('Test') {
           steps {
               echo 'Hello World test'
           }
       }
   stage('Deploy') {
           steps {
               echo 'Hello World deploy'
           }
       }
   }
}

Pipeline as a Code:'

This pipeline is used to execute the commands.


pipeline {
   agent any

   stages {
       stage('CMD') {
           steps {
               sh 'touch file1'
               sh 'pwd'       
           }
       }
   }
}

This pipeline is used to execute multiple commands at a time:

pipeline {
   agent any

   stages {
       stage('CMD') {
           steps {
               sh '''
               touch file2
               pwd
               date 
               whoami
               '''
           }
       }
   }
}

Pipeline with variables:

This pipeline is used to define the variables.

pipeline {
   agent any
   environment {
       name = 'Mustafa'
   }
   stages {
       stage('ENV') {
           steps {
               echo "My name is $name"
           }
       }
   }
}

This pipeline is used to print the build ID’s

pipeline {
   agent any

   stages {
       stage('ENV') {
           steps {
               sh 'echo "${BUILD_ID}"'
           }
       }
   }
}

In this pipeline, we can see local and global variables.

pipeline {
   agent any
   environment {
      name = "Mustafa"
   }
   stages {
       stage('one') {
           steps {
               echo "My name is $name"
           }
       }
       stage('two') {
           environment {
              course = "DevOps"
           }
           steps {
               echo "$name is a $course trainer"
           }
       }
   }
}

In the above pipeline, we can see name = mustafa is a global variable, we can access this variable on any stage in the pipeline. course = DevOps is a local variable, we can use this only on stage-2.

NOTE:

If you want to access a variable on all pipelines, go to manage jenkins » system » select variables option and pass the values.

Stay with me, on the next blog i’ll continue the basic of pipelines with more examples and concepts

If you love stories that inspire learning, growth, and productivity, consider subscribing for more! If this article added value to your journey, your support would mean the world to me — only if it’s within your means. Let’s stay connected on LinkedIn too. Thank you for reading!