Basics of Jenkinsfile

A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline. It is written in a domain-specific language (DSL) that is based on the Groovy programming language.

A Jenkinsfile can be stored in a version control system along with the source code for a project, and can be used to define the entire pipeline, including the stages, steps, and other configuration details. This allows for the pipeline to be versioned and managed like any other code artifact.

A Jenkinsfile can be defined in two ways:

  1. Declarative Pipeline: This is a newer way to define a Jenkins Pipeline and is based on a declarative syntax. It provides a more structured and concise way of defining a pipeline and is recommended for most use cases.

  2. Scripted Pipeline: This is an older way of defining a Jenkins Pipeline and is based on a Groovy script. It provides a more flexible and powerful way of defining a pipeline but can also be more verbose and harder to read.

In a Jenkinsfile, a pipeline is defined using the pipeline keyword and the stages are defined using the stages keyword. Each stage can contain one or more steps that are defined using the steps keyword.

The Jenkinsfile can also define environment variables, input parameters, and other configuration details that are used throughout the pipeline. This allows for greater flexibility and customization of the pipeline.

// Define the Jenkins Pipeline
pipeline {
    // Define the agent to run the pipeline on
    agent any

    // Define the stages of the pipeline
    stages {
        // Define the first stage
        stage('Build') {
            // Define the steps in the stage
            steps {
                // Checkout the source code from the Git repository
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'git-credentials', url: 'https://github.com/example/repo.git']]])

                // Build the code using Maven
                sh 'mvn clean install'
            }
        }

        // Define the second stage
        stage('Deploy') {
            // Define the steps in the stage
            steps {
                // Deploy the built artifact to a remote server
                sh 'scp target/myapp.war user@server:/opt/tomcat/webapps'
            }
        }
    }
}
  • pipeline { }: This block of code defines the Jenkins Pipeline.

  • agent any: This line of code defines the agent that the pipeline will run on. In this case, the any keyword indicates that the pipeline can run on any available agent.

  • stages { }: This block of code defines the stages of the pipeline.

  • stage('Build') { }: This block of code defines the first stage of the pipeline and gives it the name "Build".

  • steps { }: This block of code defines the steps in the "Build" stage.

  • checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'git-credentials', url: 'https://github.com/example/repo.git']]]): This line of code uses the checkout step to check out the source code from a Git repository.

  • sh 'mvn clean install': This line of code uses the sh step to run a shell command that builds the code using Maven.

  • stage('Deploy') { }: This block of code defines the second stage of the pipeline and gives it the name "Deploy".

  • steps { }: This block of code defines the steps in the "Deploy"

Thank you for reading! Happy Learning!