DevOps Classroom Series – 05/Dec/2021

Some Jenkins Options

  • General:
    • Execute Concurrent Builds if necessary: Enables the option of scheduling concurrent builds of same project
  • Build Environment:
    • Delete Workspace before build starts
    • Abort the build if it’s stuck
  • Post build Actions:
    • Delete Workspace after build is done

Jenkins Node Management

  • Number of Executors on the node decide how many builds can be performed on a particular node
  • Built-in Node (Master)
  • Node 1=> Lets change the number of executors to 3

Jenkins 2

  • Introduced the concept of pipeline-as-code i.e. Build configurations can be created as some script/code which can be version controlled

  • Lets create a sample hello-pipelines project to understand how the things work

  • Refer Here for the sample jenkins file created and Refer Here for the history

  • In Jenkins2 instead of filling in web forms to define jobs, users can now write pipelines using Jenkins DSL and Groovy to define their pipelines & other tasks

  • DSL (Domain Specific Language) the programming language for Jenkins.

  • This DSL is Groovy based and it includes terms and constructs that include jenkins specific functionality

  • Groovy links

  • In Jenkins2 Pipelines are two categories

    • Scripted Pipelines:
      • When Jenkins introduced pipeline as a code, the code was primarily a Groovy script with Jenkins-Specific DSL Steps inserted
      • Program’s flow was managed by Groovy constructs
      • Error reporting and checking were based on Groovy program execution rather than what you are attempting to do with Jenkins
      • This model is now represented as Scripted Pipeline
    • Declarative Pipelines:
      • CloudBees, the enterprise company that is majorly contributing under to the Jenkins Project in 2016 & 2017, introduced and enhanced programming syntax for pipelines-as-code called as Declarative Pipeline
      • This syntax adds a clear, expected structure to pipelines as well as enhanced DSL Elements and constructs
      • So instead of having to scan through Groovy trace backs when an error occures, the user is presented with direct error message in most of the cases

Scripted pipeline vs Declarative pipelines

  • Example of Scripted pipeline
//Scripted Pipeline
node('node1-label') {
    stage('Source') { //Get the code
        git 'https://github.com/GitPracticeRepo/java11-examples.git'
    }
    stage('Build') {//Compile the code and run unit tests and create package
        sh 'mvn clean package'
    }
}
  • Refer Here for the documentation of scripted pipelines
  • Declarative pipeline
pipeline {
    agent { label 'node1-label' }
    stages {
        stage('Source') {
            steps {
                // get the code
                git 'https://github.com/GitPracticeRepo/java11-examples.git'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
    }
}
  • Advantages of Scripted Pipeline
    • Generally fewer sections
    • Capacity to use more procedural code
    • More like creating a program
    • More flexible to do custom operations as we can include groovy directly
    • Able to model more complex build workflow
  • Disadvantages of Scripted Pipeline
    • More programming is required
    • Syntax checking limited to Groovy environment
    • Away from jenkins traditional mode
  • Advantages of Declarative Pipeline
    • More structured: closer to traditional jenkins approach
    • More readable
    • Can be generated from Blue Ocean graphical interface
    • Contains sections that map to famililar Jenkins concepts
    • Better Syntax checking
  • Disadvantages of Declarative Pipeline
    • Still evolving
    • Currently not well suited for complex build workflow

Working with Jenkins DSL for Scripted Pipelines

  • node:
    • This is the new term for master or agent which are configured in Manage Jenkins -> Manage Nodes
    node(<label>)
    
    • This defines where to execute the jenkins job
    • We can leverage multiple labes on a node and use the logiical operators || for or and && for and
    node("jdk-11 && mvn && linux")
    
    • The braces {} here is known as a Groovy closure & it simple means start and end of the block of code associated with this node as part of pipeline
    • Refer Here to know more about groovy closures
  • stage:
    • Within a node definition, a stage closure allows us to group together individual setting, DSL Commands
    • A stage is required to have a name
  • steps:
    • In the Stage closure, we can actually call Jenkins DSL commands which are referred as steps in Jenkins Terminology.
    • Step is an individual functionality that can be executed
    • Refer Here for the list of steps
    • Step syntax: by taking help of a git step Refer Here for git steps documentation
    git([url: 'https://github.com/wakaleo/game-of-life.git',
      branch: 'continuous-delivery'
    ])
    
  • Relation between node, stage and step
  • Syntax of Scripted Pipelines visually
  • Scripted Pipeline Sample
node('jdk11-mvn3.8.4') {
    stage('git') {
        git 'https://github.com/GitPracticeRepo/java11-examples.git'
    }
    stage('build') {
        sh '''
            echo "PATH=${PATH}"
            echo "M2_HOME=${M2_HOME}"

        '''
        sh '/usr/local/apache-maven-3.8.4/bin/mvn clean package'
    }
    stage('archive') {
        archiveArtifacts artifacts: 'target/*.jar', followSymlinks: false
    }
    stage('publish test reports') {
        junit '**/TEST-*.xml'
    }
}
  • Side-by-Side Comparision

  • To generate the scripted pipelines, Jenkins will have inbuild Snippet Generator

  • Replay:

    • Creating pipelines is more involved and some times, there will be failures and you might want to correct.
    • Jenkins 2 includes a functionaliyt called Replay for such cases
  • Pipeline Steps:

    • Screen Shots:

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Social Media Widget Powered by Acurax Web Development Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%