3. INTRODUCTION OF JENKINSFILE
• A continuous delivery pipeline is an automated expression of your process for getting software from
version control right through to your users and customers.
@2020 copyright KalKey training
4. CREATING A JENKINSFILE
• The current Jenkinsfile has two ways of writing, and pipeline if it is the root, it is called Declarative
Pipeline. In this case, you cannot write the Groovy script directly, and if you want to write Groovy
you script need to use the directive.
• Pipeline If that does not start from, say Scripted Pipeline, to this case also write directly Groovy
script, node() arrow stage(), such as, can also be written Pipeline Steps method. Although it seems
convenient, degrees of freedom are too high and tend to be craftsmen code.
@2020 copyright KalKey training
5. JENKINS PIPELINE & COMPONENTS
• What is Jenkins Pipeline?
Jenkins Pipeline (or simply "Pipeline" with a capital "P") is a suite of plugins which supports
implementing and integrating continuous delivery pipelines into Jenkins.
• Pipeline
A Pipeline is a user-defined model of a CD pipeline. A Pipeline’s code defines your entire build process,
which typically includes stages for building an application, testing it and then delivering it.
Also, a pipeline block is a key part of Declarative Pipeline syntax.
• Node
A node is a machine which is part of the Jenkins environment and is capable of executing a Pipeline.
Also, a node block is a key part of Scripted Pipeline syntax.
@2020 copyright KalKey training
6. CONT….
• Stage
A stage block defines a conceptually distinct subset of tasks performed through the entire Pipeline (e.g.
"Build", "Test" and "Deploy" stages), which is used by many plugins to visualize or present Jenkins
Pipeline status/progress.
• Step
A single task. Fundamentally, a step tells Jenkins what to do at a particular point in time (or "step" in the
process). For example, to execute the shell command make use the sh step: sh 'make'. When a plugin
extends the Pipeline DSL, [1] that typically means the plugin has implemented a new step
@2020 copyright KalKey training
7. JENKINSFILE (DECLARATIVE PIPELINE)
pipeline {
agent any
stages {
stage('Build') {
steps {
echo "We are in Build Stage"
}
}
stage('Test') {
steps {
echo "We are in Test Stage"
}
}
stage('Deploy') {
steps {
echo "We are in Deploy Stage"
}
}
}
}
@2020 copyright KalKey training
9. STRING INTERPOLATION
• Jenkins Pipeline uses rules identical to Groovy for string interpolation.
• While Groovy supports declaring a string with either single quotes, or double quotes
def username = 'Jenkins'
echo 'Hello Mr. ${username}'
echo "I said, Hello Mr. ${username}“
Result:
Hello Mr. Jenkins
I said, Hello Mr Jenkins
@2020 copyright KalKey training
10. USING ENVIRONMENT VARIABLES
• Jenkins Pipeline exposes environment variables via the global variable env, which is available from anywhere within
a Jenkinsfile.
• Declarative Pipeline:
pipeline {
agent any
environment {
NAME = 'ricardo'
LASTNAME = 'gonzalez'
}
stages {
stage('Build') {
steps {
sh 'echo $NAME $LASTNAME'
}
}
}
}@2020 copyright KalKey training
11. PLUGIN INTEGRATION
• Git Setup
Configure Git pulgin on Jenkins
Git is one of the most popular tools for version control system. you can pull code from git repositories
using jenkins if you use github plugin.
Prerequisites:
Jenkins server
Install Git on Jenkins server
yum install git –y
Setup Git on jenkins console
Install git plugin without restart
Manage Jenkins > Jenkins Plugins > available > github
Configure git path
Manage Jenkins > Global Tool Configuration > git
@2020 copyright KalKey training
12. MAVEN INTRODUCTION
• Maven is a powerful project management tool that is based on POM (project object model). It is used
for projects build, dependency and documentation.
• Building a software project typically consists of such tasks as downloading dependencies, putting
additional jars on a class-path, compiling source code into binary code, running tests, packaging
compiled code into deployable artifacts such as JAR, WAR, and ZIP files, and deploying these
artifacts to an application server or repository.
• Apache Maven automates these tasks, minimizing the risk of humans making errors while building the
software manually and separating the work of compiling and packaging our code from that of code
construction
@2020 copyright KalKey training
13. WHY USE MAVEN?
• Simple project setup that follows best practices: Maven tries to avoid as much configuration as
possible, by supplying project templates.
• Dependency Management: It includes automatic updating, downloading and validating the
compatibility, as well as reporting the dependency closures
• Isolation between project Dependencies and Plugins: With Maven, project dependencies are retrieved
from the dependency repositories while any plugin's dependencies are retrieved from the plugin
repositories, resulting in fewer conflicts when plugins start to download additional dependencies.
• Central Repository System: Project dependencies can be loaded from the local file system or public
repositories, such as Maven Central
@2020 copyright KalKey training
14. PROJECT OBJECT MODEL(POM)
• The configuration of a Maven project is done via a Project Object Model (POM), represented by a
pom.xml file. The POM describes the project, manages dependencies, and configures plugins for
building the software.
• The POM also defines the relationships among modules of multi-module projects.
@2020 copyright KalKey training
15. LET'S LOOK AT THE
BASIC STRUCTURE OF
A TYPICAL POM FILE:
@2020 copyright KalKey training
16. PROJECT IDENTIFIERS
• Maven uses a set of identifiers, also called coordinates, to uniquely identify a project and specify how
the project artifact should be packaged:
• groupId – a unique base name of the company or group that created the project
• artifactId – a unique name of the project
• version – a version of the project
• packaging – a packaging method (e.g. WAR/JAR/ZIP)
The first three of these (groupId:artifactId:version) combine to form the unique identifier and are the
mechanism by which you specify which versions of external libraries (e.g. JARs) your project will use.
@2020 copyright KalKey training
17. DEPENDENCIES
• These external libraries that a project uses are called dependencies. The dependency management
feature in Maven ensures automatic download of those libraries from a central repository, so you don't
have to store them locally.
• This is a key feature of Maven and provides the following benefits:
• uses less storage by significantly reducing the number of downloads off remote repositories
• makes checking out a project quicker
• provides an effective platform for exchanging binary artifacts within your organization and beyond
without the need for building artifact from source every time
@2020 copyright KalKey training
18. CONT….
• In order to declare a dependency on an
external library, you need to provide the
groupId, artifactId, and the version of the
library. Let's take a look at an example:
@2020 copyright KalKey training
19. REPOSITORIES
• A repository in Maven is used to hold build artifacts and dependencies of varying types. The default local
repository is located in the .m2/repository folder under the home directory of the user.
• If an artifact or a plug-in is available in the local repository, Maven uses it. Otherwise, it is downloaded
from a central repository and stored in the local repository. The default central repository is Maven
Central.
@2020 copyright KalKey training
20. CONT….
• Some libraries, such as JBoss server, are not
available at the central repository but are
available at an alternate repository. For those
libraries, you need to provide the URL to the
alternate repository inside pom.xml file:
@2020 copyright KalKey training
21. PROPERTIES
• Custom properties can help to make your pom.xml file easier to read and maintain. In the classic use
case, you would use custom properties to define versions for your project's dependencies.
• Maven properties are value-placeholders and are accessible anywhere within a pom.xml by using the
notation ${name}, where name is the property.
• Let's see an example: in next slide
@2020 copyright KalKey training
22. CONT…
• Now if you want to
upgrade Spring to a
newer version, you
only have to change
the value inside
the<spring.version>
property tag and all the
dependencies using
that property in their
<version> tags will be
updated.
@2020 copyright KalKey training
23. BUILD
• The build section is also a
very important section of the
Maven POM. It provides
information about the default
Maven goal, the directory for
the compiled project, and the
final name of the application.
The default build section
looks like this:
• The default output folder for
compiled artifacts is named
target, and the final name of
the packaged artifact consists
of the artifactId and version,
but you can change it at any
time.
@2020 copyright KalKey training
24. USING
PROFILES
• Another important feature
of Maven is its support
for profiles. A profile is
basically a set of
configuration values. By
using profiles, you can
customize the build for
different environments
such as
Production/Test/Develop
ment:
@2020 copyright KalKey training
25. MAVEN BUILD LIFECYCLES
• Every Maven build follows a specified lifecycle. You can execute several build lifecycle goals, including
the ones to compile the project’s code, create a package, and install the archive file in the local Maven
dependency repository.
@2020 copyright KalKey training
26. LIFECYCLE PHASES
• Validate – checks the correctness of the project
• Compile – compiles the provided source code into binary artifacts
• Test – executes unit tests
• Package – packages compiled code into an archive file
• Integration-test – executes additional tests, which require the packaging
• Verify – checks if the package is valid
• Install – installs the package file into the local Maven repository
• Deploy – deploys the package file to a remote server or repository
@2020 copyright KalKey training
27. CONT…
Maven Setup
Install & configure Maven build tool on Jenkins
Maven is a code build tool which used to convert your code to an artifact. this is a widely used plugin to build in continuous integration
Prerequisites
Jenkins server
Install Maven on Jenkins
1. Download maven packages https://maven.apache.org/download.cgi onto Jenkins server. In this case, I am using /opt/maven as my installation directory
Link : https://maven.apache.org/download.cgi
# Creating maven directory under /opt
mkdir /opt/maven cd /opt/maven
# downloading maven version 3.6.0
Wget http://mirrors.estointernet.in/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
tar -xvzf apache-maven-3.6.1-bin.tar.gz
2. Setup M2_HOME and M2 paths in .bash_profile of the user and add these to the path variable
vi ~/.bash_profile
M2_HOME=/opt/maven/apache-maven-3.6.1
M2=$M2_HOME/bin
PATH=<Existing_PATH>:$M2_HOME:$M2
@2020 copyright KalKey training
28. CHECKPOINT
• logoff and login to check maven version
mvn –version
• So far we have completed the installation of maven software to support maven plugin on the jenkins
console. Let's jump onto Jenkins to complete the remaining steps.
• Setup maven on Jenkins console:
1. Install maven plugin without restart
• Manage Jenkins > Jenkins Plugins > available > Maven Invoker
• Manage Jenkins > Jenkins Plugins > available > Maven Integration
2. Configure maven path
• Manage Jenkins > Global Tool Configuration > Maven
@2020 copyright KalKey training