How to write a Jenkinsfile

In Jenkins pipeline, we have two way of writing Jenkinsfile,

From Official Documentation:

A Jenkinsfile can be written using two types of syntax - Declarative and Scripted.

Declarative and Scripted Pipelines are constructed fundamentally differently.

Declarative Pipeline is a more recent feature of Jenkins Pipeline which:

  --> provides richer syntactical features over Scripted Pipeline syntax, and

  --> is designed to make writing and reading Pipeline code easier.

We will see only how to write Jenkinsfile using Declarative way

In Declarative pipeline, the Jenkinsfile start with pipeline block (Mandatory)

pipeline {

}
pipeline {
    agent any

    stages {
        stage ('Print') {
            steps {
                echo "Hello Devops Engineers"
            }
        }
    }
}

In the above Jenkinsfile, we have created the Print stage which uses the echo step to print Hello DevOps Engineers

post block in Jenkinsfile

Official documentation

The post block is inside a pipeline block.

pipeline {
    agent any

    stages {
        stage ('Print') {
            steps {
                echo "Hello Devops Engineers"
            }
        }
    }
    post {
        always { 
            echo 'I will always say Hello again!'
        }
        success {
            echo 'I will say Hello only if job is success'
        }
        failure {
            echo 'I will say Hello only if job is failure'
        }
    }
}

Even if some stages failed, post block will be executed always. In post block we have three important blocks always, success, failure

triggers block in Jenkinsfile

Official documentation

The triggers block is inside a pipeline block.

triggers block is used to re-trigger the job based on the defined triggers, it can be cron, pollSCM and upstream.

pipeline {
    agent any

    triggers {
        cron('H/15 * * * *')
    }

    stages {
        stage ('Print') {
            steps {
                echo "Hello Devops Engineers"
            }
        }
    }

    post {
        always { 
            echo 'I will always say Hello again!'
        }
        success {
            echo 'I will say Hello only if job is success'
        }
        failure {
            echo 'I will say Hello only if job is failure'
        }
    }
}

parameters block in Jenkinsfile

Official documentation

The parameters block is inside a pipeline block.

The parameters block is used to pass dynamic paramerts/variables to the job. It has the following types

pipeline {
    agent any

    triggers {
        cron('H/15 * * * *')
    }

    parameters {
        choice(name: 'environment', choices: ['dev', 'uat', 'prod'], description: 'Select environment to deploy')
    }

    stages {
        stage ('Print') {
            steps {
                echo "Hello Devops Engineers"
            }
        }
    }

    post {
        always { 
            echo 'I will always say Hello again!'
        }
        success {
            echo 'I will say Hello only if job is success'
        }
        failure {
            echo 'I will say Hello only if job is failure'
        }
    }
}

environment block in Jenkinsfile

Official documentation

The environment block specifies a sequence of key-value pairs which will be defined as environment variables for all steps, or stage-specific steps, depending on where the environment block is located within the Pipeline or within the stage.

The environment block can be inside the pipeline block or inside the stage block

pipeline {
    agent any

    environment { 
        NAME = 'vignesh'
    }

    triggers {
        cron('H/15 * * * *')
    }

    parameters {
        choice(name: 'environment', choices: ['dev', 'uat', 'prod'], description: 'Select environment to deploy')
    }

    stages {
        stage ('Print') {
            environment { 
                MESSAGE = 'Hello Devops Engineers'
            }
            steps {
                echo "$MESSAGE"
            }
        }
    }

    post {
        always { 
            echo 'I will always say Hello again!'
        }
        success {
            echo 'I will say Hello only if job is success'
        }
        failure {
            echo 'I will say Hello only if job is failure'
        }
    }
}

If environment block is defined within the stage block, then those environment variables will be accessible only within that stage.

Previous Topic

How to create pipeline job in Jenkins

Next Topic