What is a
Jenkins pipeline?
A pipeline is a collection of jobs that brings the software from version
control into the hands of the end users by using automation tools. It is a
feature used to incorporate continuous delivery in software
development workflow.
Pipeline
Concepts
The below fundamentals are
common to both, scripted and declarative pipeline:
1.
Pipeline: A
user-defined block which contains all the stages. It is a key part of
declarative pipeline syntax.
2.
Node: A node is
a machine that executes an entire workflow. It is a key part of the scripted
pipeline syntax.
3. Agent: instructs
Jenkins to allocate an executor for the builds. It is defined for an entire
pipeline or a specific stage.
It has the following parameters:
· Any: Runs pipeline/ stage on any
available agent
· None: applied at the root of the
pipeline, it indicates that there is no global agent for the entire pipeline
& each stage must specify its own agent
· Label: Executes the pipeline/stage
on the labelled agent.
· Docker: Uses docker container as an
execution environment for the pipeline or a specific stage.
1.
Stages: It
contains all the work; each stage performs a specific task.
2.
Steps: steps are
carried out in sequence to execute a stage
Jenkins
Pipeline syntax example
pipeline{
stage(‘SCM checkout’) {
//Checkout from your SCM(Source
Control Management)
//For eg: Git Checkout
}
stage(‘Build’) {
//Compile code
//Install dependencies
//Perform Unit Test, Integration Test
}
stage(‘Test’) {
//Resolve test server dependencies
//Perform UAT
}
stage(‘Deploy’) {
//Deploy code to prod server
//Solve dependency issues
}
}
Schedule a build periodically
Jenkins uses Cron expressions
to schedule a job. Each line consists of 5 fields separated by TAB or
whitespace:
Syntax: (Minute
Hour DOM Month DOW)
MINUTE: Minutes in one hour (0-59)
HOURS: Hours in
one day (0-23)
DAYMONTH: Day
in a month (1-31)
MONTH: Month in
a year (1-12)
DAYWEEK: Day of
the week (0-7) where 0 and 7 are sunday
Example: H/2 *
* * * (schedule your build for every 2 minutes)
Build Shared library for reusable logic
Refer this link
Common Mistakes to avoid in Jenkins
1. Not storing pipeline code in
Jenkinsfile
2. Blocking Executor and waiting
for Input
3. Repetition of similar pipeline steps
4. Creating large global variable declarations
Best Practices - https://www.jenkins.io/doc/book/pipeline/pipeline-best-practices/