DevOps classroom Series – 19/Feb/2021

Jenkins 2

  • In this series we will look into the newer version of Jenkins feature that directly supports pipeline as a code
  • Jenkinsfile:
    • In Jenkins 2 we can write our own pipeline defintion using a DSL (Domain Specific Language) and save it as a text file in our source code and Jenkins will read the pipeline from the scm (git) and execute the pipeline steps
    • This textfile is referred as Jenkinsfile. It is not necessary to name the file as Jenkinsfile, but it is a standard convention and many organizations using this standard.
  • Jenkins supports two kinds of pipelines
    • Scripted Pipeline:
      • This is primarily a Groovy Script (Java Based Language)
    • Declarative Pipeline:
      • This was introduced by Cloud bees and this reduces the need to supplement the pipeline defintion with Groovy code to emulate traditional features of jenkins
  • Jenkins 2 Supports some new project types
    • Pipeline
    • Folder
    • Organization
    • Multi branch pipeline Preview

Foundations

  • Scripted vs Declarative Pipeline
    • Scripted syntax refers to the intial way of pipeline as code in jenkins
    • Scripted Syntax is imperative style and is more dependend on Groovy language and Groovy constructs
    • Declarative syntax is the newer option
    • Pipelines are defined in a declarative style in clear sections that describe the states rather than focussing on logic
  • Scripted Pipeline example
node('ltecomm') {
    stage('git'){
        git 'https://github.com/wakaleo/game-of-life.git'
    }
    stage('maven') {
        sh 'mvn clean package'
    }
    stage('archive artifacts') {
        archive 'gameoflife-web/targets/gameoflife.war'
    }
}
  • Declarative Pipeline:
pipeline {
    agent { label 'ltecomm' }
    stages {
        stage('SCM') {
            steps {
                git 'https://github.com/wakaleo/game-of-life.git'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
    }
}
  • Best way to configure pipeline is to create a Jenkinsfile in your source code

  • Refer Here this changeset

  • Now lets create a jenkins job Preview Preview

  • Now lets change the Jenkinsfile to have the declarative pipeline Refer Here for the changes

  • Basic Structure of Scripted pipeline Preview

  • Refer Here for the official docs

  • Blue Ocean Interface:

    • Navigate to Jenkins Plugins and install Blue Ocean

Leave a Reply

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

About learningthoughtsadmin