Jenkins Interview Questions

Jenkins Q & A

1. What is Jenkins?
Jenkins is an open-source continuous integration software tool written in the Java posture exercises programming language for testing and reporting on isolated changes in a larger code base in real time. The software enables developers to find and solve defects in a code base rapidly and to automate testing of their builds.

2. What are the uses of jenkins?
– Continuous integration
– Continuous deployment pipelines which deploy latest code to multiple environments
– Automated test execution
– Job execution involving execution of an application
– AWS related operations
– Code coverage check

3. What is a jenkins deployment pipeline?
Jenkins deployment pipeline is a job which executes using single script. This script is capable of pulling the latest code, updating configurations, deploying code to multiple environments, execute unit and integation tests in stages.
This can be called continuous delivery or continuous deployment pipeline.

4. What is Continuous delivery?
Continuous delivery is a series of practices designed to ensure that code can be rapidly and safely deployed to production by delivering every change to a production-like environment and ensuring business applications and services function as expected through rigorous automated testing. Since every change is delivered to a staging environment using complete automation, you can have confidence the application can be deployed to production with a push of a button when the business is ready.

5. What is continuous deployment?
Continuous deployment is the next step of continuous delivery: Every change that passes the automated tests is deployed to production automatically. Continuous deployment should be the goal of most companies that are not constrained by regulatory or other requirements.

4. What is the difference between Continuous delivery and Continuous deployment pipeline?
The main difference is manual control in deployment. In continuous delivery, user permission is required to deploy code to production or equivalent environment.
But in case of continuous deployment, the code is deployed to production environment automatically.

Continuous deployment is used when there is a complete confidence on code and well formed automated test cases are included.
Continuous delivery is used in case of manual intervention such as sign off from manual testers in lower environments before deploying it to production.

5. How do you achieve continuous deployment in jenkins pipeline?
Trigger the jenkins pipeline automatically, by adding trigger steps in pipeline, on merging code in master branch. There should be no manual intervention by user.
The trigger is not mandatory. It can also be done by starting the execution of jenkins pipeline job manually but no intereference once the job begins.
An ideal CD pipeline will have stages which execute automated tests before deploying it to production or equivalent environment.

6. How do you achieve continuous delivery in jenkins pipeline?
Add a trigger in jenkins pipeline on code merge or start pipeline job execution manually. In addition to this there should be a step asking the user to proceed or terminate the pipeline before production environment.
This can be achieved using “input” step before production deployment stage in jenkins pipeline.

7. What are the steps to create a pipeline?
– Set up jenkins instance.
– login to jenkins instance
– click on New item
– Provide pipeline name
– click on “Pipeline” option in list of job types
– Click on create

8. What are the different types of Domain specific languages in jenkins pipeline?
Declarative and Scripted pipeline are the two domain specific languages in jenkins pipeline

9. What is the difference between declarative and scripted jenkins pipelines?
– Declarative pipeline makes the pipeline code easier to read and write. Scripted pipeline is complex compared to declarative.
– Scripted pipeline uses stricter groovy based syntaxes because it was the first pipeline to be built on the groovy foundation. Declarative pipeline was introduced to offer a simpler and more optioned Groovy syntax.
– The declarative pipeline is defined within a block labelled �pipeline� whereas the scripted pipeline is defined within a �node�.
– Declarative Pipeline encourages a declarative programming model, whereas Scripted Pipelines follow a more imperative programming model.

10. What are the different configurations to run pipeline script in a jenkins pipeline?
– Pipeline: code is written and added directly in the pipeline configuration.
– Pipeline script from SCM : A groovy file is added in a repository. Repository details are provided in job. The pipeline script is imported from repository and then executed.

11. What are plugins in jenkins?
Plugins are the primary means of enhancing the functionality of a Jenkins environment to suit organization- or user-specific needs. The plugins can be used in any type of jenkins job.

12. What are the steps to install a plugin?
– Click on manage jenkins
– Click on manage plugins
– Click on installed tab and search for the plugin. If the plugin is available and updated, then it is already installed.
– If plugin is not found in installed tab, search for the plugin. Click on install.
Note: Few plugins might require restarting of jenkins instance/server.

13. What are different sections in a jenkins declarative pipeline?
– Pipeline: This is the main section which includes all the stages.
syntax: pipeline{}
– agent: The agent section specifies where the entire Pipeline, or a specific stage.
syntax: agent{ label ‘<INSERT_LABEL_NAME>’}
– environment: defines environment variable which can be used throughout the pipeline script.
environment {
variable1=”hello world”
}
– stages: includes list of stages
Stages{
stage(“build”){}
stage(“test”){}
stage(“deploy”){}
}
– stage: defines the stage.
stage(“build”){
//steps required in building project
}
– steps: Set of instructions are provided in this section
steps{
cleanWs()
}
– script: script involves list of instructions / steps mainly including shell scripts
stage(“build”){
steps{
script{
sh “man clean install”
}
}
}
– post: post section is like a finally block. It can be executed in stage level and pipeline level. It includes additional options such as always, failure etc
For example, consider stage level scenario
stage(“build”){

}
post{
//executed always even if the stage fails
always{
echo “completed stage”
}
//executed only when stage fails
failure{
echo “stage failed”
}
}

Consider pipeline level:
pipeline{
stages{
stage(“build”){
echo “build”
}
stage(“test”){
echo “test”
}
stage(“deploy”){
echo “Deploy”
}
}
post{
always{
echo “pipeline completed”
}
}
}

14. What are the steps to configure ssh credentials in jenkins instance?
1. Click on Credentials. Select System on the left.
2. Under System, click the Global credentials (unrestricted) link to access this default domain.
3. Click Add Credentials on the left.
Note: If there are no credentials in this default domain, you could also click the add some credentials link (which is the same as clicking the Add Credentials link).
4. From the Kind field, choose the type of credentials to add.
5. From the Scope field
6. Select SSH Username with private key – specify the credentials Username, Private Key and optional Passphrase into their respective fields.
Note: Choosing Enter directly allows you to copy the private key�s text and paste it into the resulting Key text box.

15. Assuming ssh key is already configured in jenkins, how do you checkout latest code from git repository?
– Install git plugin
– create git step in build stage
stage(‘Build’){
steps{
script{
git(url:”ssh://SSH_ID@github.com/org-name/project-name.git”,branch:”master”)
}
}
}
note: replace SSH_ID with the ID configured for ssh credentials in jenkins instance

16. How do you build a maven project in jenkins instance?
– configure required Java version in global tool configuration
Manage jenkins > global tool configuration > provide a name in JDK section >
Select install automaticall and select required version from dropdown.
Note: this is applicable only for versions > 1.8
– Install maven pipeline and Config File Provider Plugin
– Add settings.xml configuration
Goto manage Jenkins > Managed files > Add a new config > Provide valid ID > Paste the config file and server credentials if required. Save the file
– Use the following script in build stage:
withMaven(globalMavenSettingsConfig: ‘<INSERT_SETTINGS_FILE_ID_HERE>’, jdk: ‘<INSERT_JAVA_VERSION_CONFIGURED_IN_GLOBAL_TOOL_CONFIGURATION>’, maven: ‘INSERT_MAVEN_SETTINGS_ID>’) {
sh “mvn clean install”
}
17. How to access aws resources from jenkins pipeline?
– Set aws credentials by selecting credential type as AWS Credentials.
– Provide aws access key ID and secret key ID.
– In the pipeline add the following script.
withAWS(credentials: “<INSERT_AWS_CREDENTIALS_ID_HERE>”, region: “<INSERT_AWS_REGION_HERE>”) {
//add aws cli steps inside sh “” block
}

18. When you create a pipeline, sometimes workspace@2 is created along with workspace in jenkins pipeline job. What is the reason behind this?
When workspace block ws(…) is used, automatically amend the prefix “@NUMBER” when the workspace to be used was already occupied by other build job.
You can use dir(…) inorder to navigate to particular directory in workspace.

19. Consider the scenario where latest build artifact needs to be deployed to a server. How do you achieve this in jenkins pipeline?
– Congfigure server as agent.
On your master machine go to Manage Jenkins > Manage Nodes.
– Enter Node Name
– Select Permanent Agent
Fill out the following:
– Set a number of executors : (one or more) as needed.
– Set a Remote FS Root: a home directory for the master on the agent machine.
For a Windows agent, use something like: “C:\Jenkins\”
– Select the appropriate Usage setting: Select Leave this machine for tied jobs only
– Launch Method:
An easy way to control a Windows agent is by using Launch agent via Java Web Start (Recommended for Windows)
TODO: add steps for other methods.
Availability
Keep this agent online as much as possible
TODO: add details for each option.
Press OK.
– Create a pipeline job with build step.
– Add git checkout step
git(url:”ssh://SSH_ID@github.com/org-name/project-name.git”,branch:”master”)
– Stash the artifacts
stash includes: ‘INSERT_ARTIFACT_FOLDER_NAME’, name: ‘deploy_artifacts’
– Create a deployment step with node label(created in step 1) as agent label
stage(‘deploy’){
agent{ label ‘INSERT_NODE_LABEL_HERE’}
steps{
script{
dir(“/path/to/deployment_folder”){
unstash ‘deploy_artifacts’
}
}
}
}

20. What is the difference between stash and archive?
stashed artifacts can be used only when the pipeline is active. Once the pipeline execution is completed, stashed artifacts cannot be unstashed/retireved.
Archived artifacts will be available even after pipeline execution is completed. This is vanished only when next execution begins and worksapce is cleaned.

21. How do you delete old workspace in jenkins pipeline? Why is this step required?
Workspace is cleaned by calling cleanWs() method. This is a best practice scenario where workspace is cleared at the beginnning of pipeline execution.
If workspace is not cleared there are chances of new artifacts not being able to replace old artifacts. Outdated artifacts will be deployed to the server despite checking out latest code.

22. What is the difference between $ and ${} in jenkins pipeline?
Both $ and ${} are used to access defined variables in jenkins pipeline.
Consider a scenario:
VAR1_=”DNU”
VAR1=”hello”
VAR2=”world”
VAR3=”$VAR1_$VAR2″
//print hello_world
print($VAR3)

Here expected output is hello_world. But since VAR1_ is already present, it is substituted in VAR3 instead of VAR1 and ‘_’. By using ${} we can make sure the variables are substituted in right manner.
VAR3=”${VAR1}_${VAR2}” prints hello world

23. Consider the scenario where I have built a customised plugin for internal purposes. How can I use install it in jenkins?
– Click on manage jenkins
– Click on manage plugins
– Click on Advanced tab.
– This allows user to upload plugin file and install it.

24. What are the different ways to add a parameter to jenkins pipeline?
– Using job configuration
In jenkins pipeline job configuration, click on ‘This project is parameterized’.
Click on Add parameter, select required parameter type. For example: String parameter.
Provide Name.
Default value is optional.
Click on Save.
You can access this parameter in jenkins pipeline using $parameter_name
– Using parameter block in jenkins pipeline
parameters { string(name: ‘DEPLOY_ENV’, defaultValue: ‘staging’, description: ”)

25. Assuming java and jenkins installation is completed in windows machine, how do you start the jenkins instance from?
– Open command prompt
Go to the directory where your war file is placed and run the following command:
– Run the following command
java -jar jenkins.war
– Access it using http://localhost:8080/

26. What is the command to send email with attachment in jenkins pipeline?
– Install Email Extension Plugin
– Use the following script in jenkins pipeline
emailext (subject: “This is a test email”,body: “This is a test body”,to: “test@test.com”,mimeType: ‘text/html’,attachmentsPattern: “attachment.txt”)

27. What are the steps to publish html reports to jenkins dashboard?
– Install HTML Publisher Plugin
– publishHTML([allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: ‘test/reports’,
reportFiles: ‘index.html’,
reportName: ‘Test reports’
])

28. What are the advantages of Jenkins?
– Bugs tracking is easy at an early stage in the development environment.
– Provides a large numbers of plugin support.
– Iterative improvement to the code.
– Build failures are cached at integration stage.
– For each code commit changes an automatic build report notification generates.
– To notify developers about build report success or failure, it is integrated with LDAP mail server.
– Achieves continuous integration agile development and test driven development.
– With simple steps, maven release project is automated.

29. What is the difference between restart and saferestart in jenkins instance?
Restart does not wait for running builds to get completed. Restart api is JENKINS_URL/restart
safe restarts allows jenkins to complete running builds and then restart.
Safe restart api is JENKINS_URL/safeRestart.

30. How to take a backup of your Jenkins build jobs?
Within the XML configuration, each Jenkins build is stored. When this folder is copied, the configuration of all the build jobs that are managed by the Jenkins master is backed up. If we can perform a Jenkins Git integration, then it is good. When we copy the contents of the folder, we will see that the build jobs described in the folder will be restored when the Jenkins server is started the next time.

31. Name some of the SCM tools that are supported by Jenkins.
– Git
– Subversion
– CVS
– Mercurial

Leave a Comment

Your email address will not be published.