Explain Various job types in Jenkins
Various job types in Jenkins are as mentioned below :
Mention some of the useful plugins in Jenkins?
Below I have mentioned some important Plugins:
Git
Maven 2 project
Amazon EC2
Copy artifact
Green Balls
HTML publisher
Mention what commands you can use to start Jenkins manually.
- service jenkins status/stop/start/restart
- systemctl status/stop/start/restart jenkins
We can also restart/start jenkins by:
(jenkins_url)/safeRestart.
(jenkins_url)/restart.
Explain how to create a backup and copy files in Jenkins.
To create a backup all you need to do is to periodically back up your JENKINS_HOME directory. This contains all of your build jobs configurations, your slave node configurations, and your build history. To create a back-up of your Jenkins setup, just copy this directory. You can also copy a job directory to clone or replicate a job or rename the directory.
How will you secure Jenkins?
Various ways are :
- Ensure global security is on.
- Ensure that Jenkins is integrated with LDAP
- Ensure that matrix/Project matrix is enabled to fine-tune access.
- Limit physical access to Jenkins data/folders.
- Periodically run security audits on same.
Explain how you can deploy a custom build of a core plugin.
Below are the steps to deploy a custom build of a core plugin:
Stop Jenkins.
Copy the custom HPI to $Jenkins_Home/plugins.
Delete the previously expanded plugin directory.
Make an empty file called <plugin>.hpi.pinned.
Start Jenkins.
What is the relationship between Hudson and Jenkins?
You can just say Hudson was the earlier name and version of the current Jenkins. After some issues, the project name was changed from Hudson to Jenkins.
Explain how you can move or copy Jenkins from one server to another.
I will approach this task by copying the JENKINS_HOME (/var/lib/jenkin/) directory from the old server to the new one.
What are the various ways in which a build can be scheduled in Jenkins?
You can schedule a build in Jenkins in the following ways:
- By source code management commits i.e. By using webhooks.
- After completion of other builds
- Build periodically (Schedule a cron)
- Poll SCM – we can schedule when to poll scm or else if No schedules it will only run due to SCM changes if triggered by a post-commit hook
- Manual Build Requests
- Trigger builds remotely
How to configure email notifications in Jenkins.
To add email notifications we need to first – Go to the section ‘Manage Jenkins’, then Click on configure system and then Select Email notification
We need to give our SMTP server address. And other details. We use aws SMTP server and i.e. is email.smtp.us-east-1.amazonaws.com
We can further Click on the advance link and check Use SMTP Authentication check box
Provide username, password and SMTP port number; it is 465 for aws. Check charset and
make sure it is = UTF-8.
Its done now, So, whenever the build passes or fails you will get the email notification.
What is the difference between Maven, Ant, and Jenkins?
Maven and Ant are Build Technologies whereas Jenkins is a continuous integration tool.
Which SCM tools does Jenkins supports?
Below are Source code management tools supported by Jenkins:
CVS,
Subversion,
Git,
Mercurial,
Perforce,
Clearcase
What are the two components Jenkins is mainly integrated with?
According to me, Jenkins is mainly integrated with the following:
Version Control systems like GIT,SVN.
Build tools like Apache Maven.
Artifactory
Selenium (Test Tools)
Agents ( GCE, AWS, k8s)
Why do we use Jenkins with selenium?
Running Selenium tests in Jenkins allows you to run your tests every time your software changes and deploy the software to a new environment when the tests pass. Jenkins can schedule your tests to run at a specific time.
What is the build pipeline in Jenkins?
Job chaining in Jenkins is the process of automatically starting another job (s) after the execution of a job. This approach lets you build multi-step build pipelines or trigger the rebuild of a project if one of its dependencies is updated.
What is a Jenkins pipeline plugin?
The Jenkins Pipeline plugin is a game changer for Jenkins users. Based on a Domain Specific Language (DSL) in Groovy, the Pipeline plugin makes pipelines scriptable and it is an incredibly powerful way to develop complex, multi-step DevOps pipelines.
What is a DSL Jenkins?
The Jenkins “Job DSL / Plugin” is made up of two parts: The Domain Specific Language (DSL) itself that allows users to describe jobs using a Groovy-based language, and a Jenkins plugin which manages the scripts and the updating of the Jenkins jobs which are created and maintained as a result.
How schedule a build in Jenkins?
In Jenkins, under the job configuration, we can define various build triggers. Simply find the ‘Build Triggers’ section, and check the ‘ Build Periodically’ checkbox. With the periodic build you can schedule the build definition by the date or day of the week and the time to execute the build.
The format of the ‘Schedule’ textbox is as follows:
MINUTE (0-59), HOUR (0-23), DAY (1-31), MONTH (1-12), DAY OF THE WEEK (0-7)
Explain the Types of Jenkins pipeline
There are two type of Jenkins pipeline based on which format the Jenkinsfile is written.
Declarative pipeline
Scripted pipeline
The Declarative pipeline is a new feature that is added to create the pipeline. This is basically written in a Jenkinsfile which can be stored into a source code management system such as Git. Declarative pipelines is an ideal solution for the simple continuous delivery pipeline as it has very limited and pre-defined structure.
Lets see the structure and syntax of the Declarative pipeline.
The Declarative pipeline code will looks like this:
pipeline {
agent { label ‘node-1’ }
stages {
stage(‘Source’) {
steps {
git ‘https://github.com/digitalvarys/jenkins-tutorials.git”
}
}
stage(‘Compile’) {
tools {
gradle ‘gradle4’
}
steps {
sh ‘gradle clean compileJava test’
}
}
}
}
The scripted pipeline is a traditional way of writing the Jenkins pipeline as code. Ideally, Scripted pipeline is written in Jenkins file on web UI of Jenkins. Unlike Declarative pipeline, the scripted pipeline strictly uses groovy based syntax. Since this, The scripted pipeline provides huge control over the script and can manipulate the flow of script extensively. This helps developers to develop advance and complex pipeline as code.
Node Block:
Node is the part of the Jenkins architecture where Node or agent node will run the part of the workload of the jobs and master node will handle the configuration of the job. So this will be defined in the first place as
The scripted pipeline can be written as mentioned below.
node (‘node-1’) {
stage(‘Source’) {
git ‘https://github.com/digitalvarys/jenkins-tutorials.git”
}
stage(‘Compile’) {
def gradle_home = tool ‘gradle4’
sh “‘${gradle_home}/bin/gradle’ clean compileJava test”
}
}
Run the pipeline
Now lets configure the Jenkins pipeline. Following are the steps to create Jenkins Pipeline.
Step 1: Select “New Item” from the dashboard of the Jenkins instance
Step 2: Name the item and select pipeline as the style of the job item.
Step 3: Scroll down and select the definition of the Pipeline. Here we get two options.
One is the Pipeline script from SCM which is for Declarative Pipeline
Another is the Pipeline script which is for Scripted Pipeline that we will write directly on UI.
If it is the Pipeline Script, which will be written directly on UI, Need to write it on Script place (Scripted pipeline)
If we are selecting the Pipeline script from SCM definition, then we need to select the Git > SCM Repository URL > Credentials. (Declarative Pipeline).
What is a Jenkinsfile?
A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control. Creating a Jenkinsfile, which is checked into source control, provides a number of immediate benefits:
Code review/iteration on the Pipeline
Audit trail for the Pipeline
Single source of truth for the Pipeline, which can be viewed and edited by multiple members of the project.
How do you create Multibranch Pipeline in Jenkins?
The Multibranch Pipeline project type enables you to implement different Jenkins files for different branches of the same project.
In a Multibranch Pipeline project, Jenkins automatically discovers, manages, and executes Pipelines for branches that contain a Jenkinsfile in source control.
What is a blue ocean in Jenkins?
Blue Ocean is a project that rethinks the user experience of Jenkins, modeling and presenting the process of software delivery by surfacing information that’s important to development teams with as few clicks as possible, while still staying true to the extensibility that is core to Jenkins.
Name a Jenkins environment variable you have used in a shell script or batch file.
There are a number of environment variables that are available by default in any Jenkins build job. A few commonly used ones include:
$JOB_NAME
$NODE_NAME
$WORKSPACE
$BUILD_URL
$JOB_URL
$JENKINS_URL
Note that, as new Jenkins plug-ins are configured, more environment variables become available. For example, when the Jenkins Git plug-in is configured, new Jenkins Git environments variables, such as $GIT_COMMIT and $GIT_URL, become available to be used in scripts.
Name three security mechanisms Jenkins uses to authenticate users.
Jenkins can authenticate users in one of three ways:
Jenkins can use an internal database to store user data and credentials. (This is the default.)
Jenkins can be configured to authenticate against a (LDAP) Lightweight Directory Access Protocol server.
Jenkins can be configured to employ the authentication mechanism used by the application server upon which it is deployed.
Describe the standard process to configure and use third-party tools within Jenkins?
The process to use a third-party tool, such as Artifactory, Node, SonarQube or Git typically follows a four-step process.
- The third-party software must be installed.
- A Jenkins plug-in that supports the third-party tool must be installed through the Jenkins admin console.
- The third-party tool must be configured in the Tools tab of the Manage Jenkins section of the admin console. i.e. (You have to configure the third-party tool in the admin console.)
Name two ways a Jenkins node agent can be configured to communicate back with the Jenkins master.
Launch a Jenkins node agent from a browser window.
Launch a Jenkins node agent from the command line.
When a Jenkins node agent is launched from a browser, a JNLP file is downloaded. When it runs, the JNLP file launches a new process on the client machine that runs Jenkins jobs. To launch from the command line, the agent.jar file is required on the client. This executable JAR file is run from the command line, along with a reference to the slave agent’s JNLP file that is hosted on the server. Like the JNLP file downloaded through a web browser, running this command launches a process on the client that can communicate with the Jenkins master and run Jenkins build jobs when it has idle clock cycles.
Name three steps or stages a typical Jenkins pipeline might include.
A full-blown Jenkins pipeline will build a project from source code, put it through a variety of unit, integration, performance and user acceptance tests, and then, finally, if every test succeeds, deploy a packaged application to an application server, Nexus repository or Docker container. So, three fundamental stages would be:
Build
Test
Deploy
How can you temporarily turn off Jenkins’s security if the administrative users have locked themselves out of the admin console?
The JENKINS_HOME folder contains a file named config.xml. When security is enabled, this file contains an XML element named useSecurity that will be set to true. By changing this setting to false, security will be disabled the next time Jenkins is restarted.
<useSecurity>false</useSecurity>
When asked one of these advanced Jenkins interview questions on DevOps security, be sure to emphasize that disabling security should always be both a last resort and a temporary measure. Once any authentication issues are resolved, be sure to re-enable Jenkins security and reboot the CI server.
Polling a Git repository for new commits is considered a Jenkins anti-pattern. What is a sound alternative to SVN polling?
Constantly polling a source code management tool like Git or Subversion to check if a new commit has been issued is a waste of clock cycles and should be avoided.
A better approach is to reverse this process and have the source code tool trigger a Jenkins build when new commits happen.
With GitHub or GitLab, it is relatively easy to configure a post-commit hook that runs every time a commit is successful. When provided with the URL of the Jenkins build, the post-commit hook can easily trigger a Jenkins build, eliminating the need to have Jenkins constantly poll the source code repository.
Can you write a simple Jenkins Pipeline Code for Java?
Here is the simple Jenkins Pipeline Code for Java:
Jenkinsfile (Declarative Pipeline)
pipeline {
agent none
stages {
stage(‘Build’) {
agent any
steps {
checkout scm
sh ‘make’
stash includes: ‘**/target/*.jar’, name: ‘app’
}
}
stage(‘Test on Linux’) {
agent {
label ‘linux’
}
steps {
unstash ‘app’
sh ‘make check’
}
post {
always {
junit ‘**/target/*.xml’
}
}
}
stage(‘Test on Windows’) {
agent {
label ‘windows’
}
steps {
unstash ‘app’
bat ‘make check’
}
post {
always {
junit ‘**/target/*.xml’
}
}
}
} }
Steps to install jenkins in Centos:
Java is prerequisites for Jenkins, to install jenkins in Centos we need to download jenkins.repo file and key of the jenkins.repo file and then need to install jenkins .. yum install jenkins. service jenkins start ; service jenkins enable.
cd ~
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm –import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
sudo yum install jenkins
IF you need to change the default port of jenkins
open – sudo vim /etc/sysconfig/jenkins
Find the following line and change the port number you want to use (in this case we will be using 8090):
JENKINS_PORT=”8080″
service jenkins restart
What is “pipeline stage view” plugin :
When you have complex builds Pipelines, it is useful to be able to see the progress of each stage. The Pipeline Stage View plugin includes an extended visualization of Pipeline build history on the index page of a flow project, under Stage View. (You can also click on Full Stage View to get a full-screen view.)
How to install plugin in jenkins using hpi
In the Jenkins interface, navigate to Manage Jenkins, Manage Plugins.
Select the Advanced tab.
In the Upload Plugin section, select Choose File, and select ruby-runtime-plugin-0.14.hpi.
Select Upload. Jenkins displays a status screen that shows the plugin installing.
Log Parser Plugin
The log-parser plugin parses the console log generated by the Jenkins build. Parsing the console log allows for the following features:
- highlighting lines of interest in the log (errors, warnings, information)
- dividing the log into sections
- displaying a summary of number of errors , warnings and information lines within the log and its sections.
- linking the summary of errors and warnings into the context of the full log, making it easy to find a line of interest in the log
- showing a summary of errors and warnings on the build page
- Parsed log example
- Summary at build level
This shows a summary of errors and warnings on the build page:
What is Parameterized Trigger Plugin and Parameterized-Remote-Trigger-Plugin
Parameterized Trigger Plugin: This plugin lets you trigger new builds when your build has completed, with various ways of specifying parameters for the new build.
You can add multiple configurations: each has a list of projects to trigger, a condition for when to trigger them (based on the result of the current build), and a parameters section.
There is also a Parameterized Remote Trigger Plugin in case you want to trigger a build on a different/remote Jenkins Master.
Parameterized-Remote-Trigger-Plugin
A plugin for Jenkins CI that gives you the ability to trigger parameterized builds on a remote Jenkins server as part of your build.
This is done by calling the /buildWithParameters URL on the remote server. (or the /build URL, if you don’t specify any parameters)
How to trigger Jenkins job which is running behind a firewall.
we can run Jenkins behind a firewall (which could be a corporate firewall, a NAT’ed network like you have at home) but still receive webhooks in real-time from GitHub.com. You can generalize this to other services too – such as BitBucket or DockerHub, or anything really that emits webhooks, but the instructions will be for GitHub projects hosted on github.com.
Enter the memorably named Smee (https://smee.io). This is an OSS project provided by GitHub and also helpfully hosted as a service by GitHub. This can capture and forward webhooks for you. I’ll try to explain it with a diagram:
GitHub pushes an event (via HTTPS/json in this case) to Smee.io (the funny thing with circles, which is on the public web and accessible from GitHub.com) – and Jenkins in turn subscribes to Smee with an outgoing connection from a client. Note the direction of the arrows: Jenkins only makes an outbound connection.
This is the important point: this will work as long as the firewall is one way (like a NAT typically is, and many networks). If the Jenkins side can’t connect to anything on the outside world – well, this won’t help with that of course (but that is not often the case).