Jenkins Interview Questions and Answers | Day 29 of 90 Days of DevOps

Ajit Fawade
11 min readSep 3, 2023

--

Hello everyone, and welcome to Day 29 of 90 Days of DevOps.

In this blog, I will share some of the important interview questions and answers related to Jenkins, one of the most popular tools for continuous integration and continuous delivery (CI/CD).

Jenkins is an open-source, Java-based automation server that helps developers build, test, and deploy software faster and more reliably.

If you are preparing for a DevOps interview or want to refresh your Jenkins knowledge, this blog will help you learn some of the key concepts and features of Jenkins. I will try to explain the answers with examples wherever possible.

What’s the difference between continuous integration, continuous delivery, and continuous deployment?

Continuous integration (CI) is a software development practice that involves integrating code changes from multiple developers into a shared repository frequently, usually several times a day. CI helps to detect and fix bugs early, improve code quality, and reduce integration conflicts.

Continuous delivery (CD) is a software development practice that ensures that the code changes are always in a releasable state. CD involves automating the testing and delivery stages of the software development lifecycle so that the software can be deployed to any environment at any time with minimal manual intervention.

Continuous deployment (CD) is a software development practice that extends continuous delivery by automatically deploying the code changes to the production environment as soon as they pass the testing and delivery stages. CD enables faster feedback from users, shorter release cycles, and lower deployment risks.

Benefits of CI/CD

Some of the benefits of CI/CD are:

  • Faster and more frequent delivery of software features and updates
  • Higher quality and reliability of software products and services
  • Improved collaboration and communication among developers and other stakeholders
  • Reduced costs and resources for software development and maintenance
  • Enhanced customer satisfaction and loyalty

What is meant by CI-CD?

CI-CD is a term that combines continuous integration and continuous delivery or deployment. It refers to a set of practices and tools that enable developers to automate the entire software development lifecycle from code integration to software delivery or deployment. CI-CD helps to achieve faster, safer, and more efficient software development and delivery.

What is Jenkins Pipeline?

Jenkins Pipeline is a feature of Jenkins that allows users to define and execute a series of steps or stages as a single continuous process using a domain-specific language (DSL) based on Groovy. A Jenkins pipeline can be written in two ways: declarative or scripted.

A declarative pipeline is a more structured and simplified way of writing a pipeline using predefined syntax and keywords. A declarative pipeline consists of a pipeline block that contains one or more stage blocks, each representing a logical unit of work. A stage block can contain one or more steps, each representing an atomic task. A declarative pipeline also supports directives such as agent, environment, options, parameters, triggers, tools, etc., that provide additional configuration for the pipeline.

A scripted pipeline is a more flexible and expressive way of writing a pipeline using Groovy code. A scripted pipeline consists of a node block that specifies the agent where the pipeline will run. Inside the node block, one or more stage blocks can be defined, each representing a logical unit of work. A stage block can contain any Groovy code or Jenkins steps. A scripted pipeline also supports parallel execution, error handling, shared libraries, etc., that provide more control over the pipeline logic.

How do you configure the job in Jenkins?

To configure a job in Jenkins, follow these steps:

  • Go to New Item > Enter a name for the job > Choose the type of the job (e.g. Freestyle project, Pipeline, etc.) > Click OK
  • Enter the general configuration for the job such as description, parameters, build triggers, etc.
  • Enter the source code management configuration for the job such as repository URL, credentials, branch specifier, etc.
  • Enter the build configuration for the job such as build steps, build environment, etc.
  • Enter the post-build configuration for the job such as post-build actions, post-build steps, etc.
  • Click Save

Where do you find errors in Jenkins?

You can find errors in Jenkins in various places such as:

  • Console output: This shows the detailed log of each build execution. You can access it by clicking on the build number in the build history or by clicking on Console Output in the left sidebar of each build page.
  • Build status: This shows the result of each build execution such as success, failure, unstable, aborted, etc. You can see it by looking at the color-coded icons in the build history or by looking at the status badge in each build page.
  • Test results: This shows the outcome of each test case that is executed during the build process. You can access it by clicking on Test Results in the left sidebar of each build page.
  • Jenkins logs: This shows the system logs of Jenkins such as startup, shutdown, plugin installation, configuration changes, etc. You can access it by going to Manage Jenkins > System Log or by going to /log on your Jenkins URL.

In Jenkins how can you find log files?

You can find log files in Jenkins in two ways:

  • Using the web interface: You can access the log files by going to Manage Jenkins > System Log or by going to /log on your Jenkins URL. You can also view the log files of each build by clicking on Console Output in the left sidebar of each build page.
  • Using the file system: You can access the log files by going to the JENKINS_HOME directory on your Jenkins server. The JENKINS_HOME directory is the location where Jenkins stores its configuration data, plugins, jobs, etc. The default location of JENKINS_HOME depends on the operating system and installation method of Jenkins. For example, on Linux, it is usually /var/lib/jenkins. The log files are stored in the logs subdirectory of JENKINS_HOME. The main log file is jenkins.log, which contains the system logs of Jenkins. The other log files are named after the jobs or plugins that generate them.

Jenkins workflow and write a script for this workflow?

A Jenkins workflow is a sequence of steps or stages that define how a software project is built, tested, and deployed using Jenkins. A Jenkins workflow can be written using a Jenkins pipeline script, which is a DSL based on Groovy. A Jenkins pipeline script can be written in two ways: declarative or scripted.

Here is an example of a Jenkins workflow and a declarative pipeline script for it:

  • The workflow consists of four stages: Build, Test, Deploy, and Notify
  • The Build stage clones the GitHub repository of the project and builds a Docker image using the Dockerfile
  • The Test stage runs the unit tests using the npm test
  • The Deploy stage pushes the Docker image to Docker Hub and deploys it to a Kubernetes cluster using kubectl
  • The Notify stage sends an email notification with the build status and the deployment URL
pipeline {
agent any // This means that the pipeline will run on any available agent
stages {
stage('Build') {
steps {
git 'https://github.com/ajitfawade/node-todo-cicd.git' // This will clone the GitHub repository to the agent's workspace
docker.build('ajitfawade/node-todo-cicd') // This will build a Docker image using Dockerfile
}
}
stage('Test') {
steps {
sh 'npm install' // This will install the dependencies using npm
sh 'npm test' // This will run the unit tests using npm
}
}
stage('Deploy') {
steps {
script {
docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') { // This will use the credentials for Docker Hub that you need to create in Jenkins
docker.image('ajitfawade/node-todo-cicd').push() // This will push the Docker image to Docker Hub
}
withCredentials([usernamePassword(credentialsId: 'kubernetes-credentials', usernameVariable: 'KUBE_USER', passwordVariable: 'KUBE_PASS')]) { // This will use the credentials for Kubernetes that you need to create in Jenkins
sh "kubectl --username=${KUBE_USER} --password=${KUBE_PASS} apply -f k8s.yaml" // This will deploy the Docker image to Kubernetes using kubectl and k8s.yaml file
}
}
}
}
stage('Notify') {
steps {
emailext ( // This will send an email notification using Email Extension Plugin that you need to install in Jenkins
subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - ${currentBuild.currentResult}",
body: """<p>${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - ${currentBuild.currentResult}</p>
<p>Check console output at <a href="${env.BUILD_URL}">${env.BUILD_URL}</a></p>
<p>Access deployed application at <a href="http://node-todo-cicd.k8s.io">http://node-todo-cicd.k8s.io</a></p>""",
to: 'ajitfawade@gmail.com'
)
}
}
}
}

How to create a continuous deployment in Jenkins?

To create a continuous deployment in Jenkins, you need to do the following:

  • Create a pipeline job that defines and executes the steps or stages for building, testing, and deploying your software project using a Jenkins pipeline script, which is a DSL based on Groovy. You can write the pipeline script in two ways: declarative or scripted. A declarative pipeline is a more structured and simplified way of writing a pipeline using predefined syntax and keywords. A scripted pipeline is a more flexible and expressive way of writing a pipeline using Groovy code.
  • Configure the build triggers for the pipeline job to run automatically whenever there is a code change in the source repository or whenever there is a manual trigger from the user. You can use various options such as SCM polling, webhook, cron expression, etc., to set up the build triggers.
  • Configure the deployment stage of the pipeline job to deploy the software to the target environment using the appropriate tools and commands. You can use various plugins or tools such as Docker, Kubernetes, AWS, Azure, etc., to perform the deployment.
  • Configure the post-build actions or steps for the pipeline job to notify the stakeholders about the deployment status and outcome. You can use various plugins or tools such as Email Extension, Slack, Teams, etc., to send notifications.

How to build a job in Jenkins?

To build a job in Jenkins, you can use one of the following methods:

  • Manual build: You can manually trigger a build by clicking on the Build Now button on the job page or by using the Build with Parameters option if the job has parameters.
  • Scheduled build: You can schedule a build to run at a specific time or interval by using the Build periodically option in the build triggers section of the job configuration. You can use a cron expression to specify the schedule.
  • Triggered build: You can trigger a build by an external event or another job by using the options such as GitHub hook trigger for GITScm polling, Poll SCM, Build after other projects are built, etc., in the build triggers section of the job configuration. You can also use the Remote access API to trigger a build programmatically.

Why do we use a pipeline in Jenkins?

We use pipeline in Jenkins because it provides several advantages such as:

  • It allows us to define and execute a series of steps or stages as a single continuous process using a DSL based on Groovy.
  • It enables us to write our pipeline code in a version-controlled repository along with our source code, which improves collaboration and maintainability.
  • It supports parallel execution, error handling, shared libraries, etc., which provide more control and flexibility over our pipeline logic.
  • It offers a graphical representation of our pipeline stages and their status, which improves visibility and troubleshooting.

Is Only Jenkins enough for automation?

No, Jenkins alone is not enough for automation. Jenkins is an automation server that helps us automate the CI/CD process of our software project. However, Jenkins relies on various other tools and plugins to perform different tasks such as source code management, testing, deployment, notification, etc. For example, we may need tools such as Git, Maven, Docker, Kubernetes, etc., to work with Jenkins. Therefore, Jenkins is not enough for automation by itself, but it acts as an orchestrator that integrates with other tools and plugins to achieve automation.

How will you handle secrets?

To handle secrets in Jenkins, we can use one of the following methods:

  • Credentials plugin: This is a built-in plugin that allows us to store and manage secrets such as username and password, SSH key, API token, etc., in Jenkins. We can create credentials in Jenkins either globally or per project and use them in our jobs or pipelines. We can also use a credentials binding plugin to inject credentials as environment variables in our jobs or pipelines.
  • Secret text plugin: This is an extension of the credentials plugin that allows us to store and manage secrets as plain text in Jenkins. We can create secret text credentials in Jenkins either globally or per project and use them in our jobs or pipelines. We can also use a secret text binding plugin to inject secret text as environment variables in our jobs or pipelines.
  • HashiCorp Vault plugin: This is an external plugin that allows us to integrate Jenkins with HashiCorp Vault, which is a tool for securely storing and accessing secrets. We can configure Vault credentials in Jenkins either globally or per project and use them in our jobs or pipelines. We can also use the Vault binding plugin to inject Vault secrets as environment variables in our jobs or pipelines.

Explain different stages in CI-CD setup

The different stages in CI-CD setup are:

  • Code integration: This is the stage where developers merge their code changes from their local branches into a shared repository frequently, usually several times a day. This helps to detect and fix bugs early, improve code quality, and reduce integration conflicts.
  • Code delivery: This is the stage where the code changes are tested and delivered to a staging environment where they are ready for deployment. This involves automating the testing and delivery stages of the software development lifecycle so that the software can be deployed to any environment at any time with minimal manual intervention.
  • Code deployment: This is the stage where the code changes are deployed to the production environment where they are accessible to the end-users. This involves automatically deploying the code changes to the production environment as soon as they pass the testing and delivery stages. This enables faster feedback from users, shorter release cycles, and lower deployment risks.

Name some of the plugins in Jenkins?

Some of the plugins in Jenkins are:

  • Git plugin: This plugin allows us to integrate Jenkins with Git, which is a distributed version control system. It enables us to clone, fetch, checkout, merge, and push Git repositories in our jobs or pipelines.
  • Maven plugin: This plugin allows us to integrate Jenkins with Maven, which is a build automation tool for Java projects. It enables us to invoke Maven goals and phases in our jobs or pipelines.
  • Docker plugin: This plugin allows us to integrate Jenkins with Docker, which is a tool for building and running containerized applications. It enables us to build, run, push, and pull Docker images and containers in our jobs or pipelines.
  • Kubernetes plugin: This plugin allows us to integrate Jenkins with Kubernetes, which is a platform for managing containerized workloads and services. It enables us to run dynamic agents on Kubernetes pods and deploy applications to Kubernetes clusters in our jobs or pipelines.
  • Email Extension plugin: This plugin allows us to enhance the email notification functionality of Jenkins. It enables us to send customized email notifications with rich content and attachments in our jobs or pipelines.

Conclusion

In this blog, I have shared some of the important interview questions and answers related to Jenkins. I hope you found this blog useful and learned something new.

If you have any questions or feedback, please feel free to leave a comment below or contact me on GitHub or LinkedIn.

Thank you for reading and happy learning!

--

--

No responses yet