3. DevOps : Jenkins

3. DevOps : Jenkins

Hey Guys,

So far, we have looked into Basics of DevOps and Git. Today, let's take a step ahead and start learning about a very important and quite useful tool : Jenkins


Jenkins is a popular open-source automation server used primarily for continuous integration (CI) and continuous delivery (CD). Jenkins automates the building, testing, and deployment of software projects, making it easier for developers to integrate changes and deliver software efficiently. These tasks can be automated by configuring Jenkins through its UI or by using a file called Jenkinsfile to define the steps, which Jenkins will then execute.

In this article we are going to look into what is Jenkins files, how to write one and certain good practices. Here I am assuming that Jenkins installation and setup is done. Lets get started.


What is Jenkinsfile?

As mentioned above, in Jenkinsfile, we write down the step to perform like, build and package, creating docker image, uploading to docker hub or nexus , deploy on server or any container platform and test. Once you have written these steps and pushed the code, you can navigate to Jenkins where your project will be listed. There will be a 'Build Now' button to start the build process.


How to write Jenkinsfile?

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
    }
}        

This is the example of Declarative Pipeline which is easier to understand and read. Note that you can achieve same result by configuring the Jenkins using its UI. Given that pipelines can get big and might need constant modifications, modifying a file is always easier. Also the fact that we can reuse same file. In this example, we got :

  • Pipeline block: Encapsulate everything
  • Agent block: States where the pipeline will actually run. Each step can have independent agent here it mean, that this pipeline can run anywhere.
  • Stage block: Defines a phase or a task like 'Build and Package'
  • Step block : Contains individual command or actions
  • Command/Action: Here you can simply write your code in groovy (JVM based language) or write scripts like shell command 'echo'. Since groovy is JVM based language its syntax is like java. You can use conditional statement and loops and method calls just like in java.


Most Useful Plugins:

There are over 1900+ plugins for Jenkins. Below are few most commonly used:

  • Git Plugin : Mostly use to check out the source code
  • Pipeline Plugin: Enables use of Jenkinsfile to define build process
  • Blue Ocean Plugin: Enables user-friendly interface
  • JUnit Plugin: Process and Display JUnit test result
  • Email Extension Plugin: Enables customizable email alerts
  • Docker Pipeline Plugin: Integrates Docker with Jenkins pipeline
  • Maven Integration Plugin: Allows you to build and manage maven projects.
  • SonarQube Plugin: Integrates Jenkins with SonarQube to perform code quality and security analysis.
  • Kubernetes Plugin: Enhances the ability to define and manage Kubernetes-based pipelines directly within Jenkins. This plugin allows Jenkins pipelines to run inside Kubernetes pods. Define pods templates that specify how Jenkins agents should be created and configured inside a YAML file. In this case 'agent' uses Kubernetes pod templates directly within the pipeline script.


Useful Global Variable/Object/Methods:

While writing pipeline code, decisions about executing certain steps are often made at runtime using conditional statements. These conditions can be based on useful global variables provided by Jenkins.

currentBuild: Provides detailed information and control over the currently running build

  • Example: currentBuild.result, currentBuild.changeSets, currentBuild.durationString etc.

env: Provides access to environment variables available to the build process

  • Example: env.BUILD_ID, env.BUILD_URL, env.GIT_BRANCH, env.USER etc.

scm: Provides access to source code management (SCM) details for the current build

  • Example: scm.getBranches(), scm.getCommitId(), scm.getGitRepo(), checkout scm.

param: Used to access parameters passed to the Jenkins job or pipeline

  • Example: param.YOUR_FIRST_PARAMETER_NAME like this param variable is used.

manager: Interact with Jenkins and perform various tasks related to logging, build actions etc.

  • Example: manager.logContains(message), manager.getLog(), manager.getCurrentBuild() etc.

trigger: Start the execution of a Jenkins job or pipeline based on certain events or schedules.

  • Example: pollSCM(), cron() are commonly used triggers.


After reviewing the details, it's clear that we can draft an effective Jenkinsfile using the information provided. As Jenkins continues to evolve and expand, this article has only scratched the surface. I hope it has offered valuable insights into leveraging Jenkins more effectively and gets you started with it.

If you have any further questions or need additional clarification, please feel free to reach out. I've included links to my previous two articles in the first section for additional context.

Thank you for reading!

要查看或添加评论,请登录

Gopal Katyarmal的更多文章

  • 2. DevOps: Git

    2. DevOps: Git

    Git is by far the most used and reliable tool for source code and version control. Git integrates with services like…

  • 1. DevOps : Basic Concept

    1. DevOps : Basic Concept

    DevOps is a combination of development and operations practices aimed at improving collaboration, efficiency, and…

    2 条评论

社区洞察

其他会员也浏览了