sidebar_position | title |
---|---|
1 |
Jenkins Introduction |
Jenkins is an open source automation server. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery.
Insted of configuring the pipeline in the Jenkins UI, we can define the entire pipeline in a Jenkinsfile and check it into source control. The file name is case sensitive and must be named Jenkinsfile
.
This pipeline can be written in two ways:
Scripted Pipeline is based on Groovy syntax. It is a general-purpose programming language with a syntax similar to Java, but with additional features that support common programming idioms.
node {
stage('Build') {
sh 'npm install'
echo 'Building..'
}
stage('Test') {
echo 'Testing..'
}
stage('Deploy') {
echo 'Deploying..'
}
}
Declarative Pipeline is a new way of defining the entire pipeline using a simple and easy to understand structure.
pipeline {
agent any
stages {
stage('Build') {
steps {
sh npm install
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying..'
}
}
}
}
- Pipeline: The top-level directive that defines the entire pipeline.
- Agent: The agent directive defines where the entire pipeline, or a specific stage, will execute in the Jenkins environment.
- Stages: The stages directive defines the different stages in pipeline.
- Stage: The stage directive defines a single stage in a pipeline.
- Steps: The steps directive defines the steps to be executed in a stage.
Post Step:
The post section is used to define actions to be taken after the completion of the pipeline. It can be used to send notifications, sccess/failure messages, etc.
stages{}
post{
always {
sh 'echo pipeline completed'
}
success {
sh 'echo pipeline sucess'
}
failure {
sh 'echo pipeline failed'
}
}
When:
The when directive is used to control the flow of the pipeline based on the conditions. It can be used to skip stages, or entire pipelines, based on the conditions.
when{
expression { BRANCH_NAME == 'main' }
}
stage('Building the image') {
when{
expression { BRANCH_NAME == 'main' }
}
steps {
sh 'docker build -t pradumnasaraf/devops:latest .'
}
}
To check default environment variables in Jenkins, we can use the following URL:
http://<host>/env-vars.html
http://localhost:8080/env-vars.html
The tools directive is used to define the tools required for the pipeline. It can be used to define the JDK, Maven and Gradle tools.
With the 1st approach we can directly use the tools and its commands in the pipeline.
tools {
maven 'maven-3.6.3'
jdk 'jdk-11'
}
We can also follow wrapper approach to define the tools.
stage('Build') {
steps {
withMaven(maven: 'maven-3.6.3') {
sh 'mvn clean install'
}
}
}
stage('Build') {
steps {
nodejs('node-12.18.3') { // nodejs is the name of the tool
sh 'npm install'
}
}
}
Note: The tools must be installed in Jenkins. To install the tools, go to Manage Jenkins > Global Tool Configuration
.
The parameters directive is used to define the parameters required for the pipeline. It can be used to define the parameters like string, boolean, choice, etc. we can check the parameters in the UI by clicking on the Build with Parameters
button.
parameters {
string(name: 'NAME', defaultValue: 'pradumnasaraf', description: 'Enter your name')
booleanParam(name: 'DEBUG', defaultValue: true, description: 'Enable debug mode')
choice(name: 'VERSION', choices: ['1.0', '2.0', '3.0'], description: 'Select version')
}
stage('Build') {
steps {
echo "Hello ${params.NAME}" //Accessing the parameters by params.{parameter_name}
echo "Debug mode is ${params.DEBUG}"
echo "Version is ${params.VERSION}"
}
}
The triggers directive is used to define the triggers for the pipeline. Common way are, poll, github webhooks, etc.
The replay option is used to re-run the pipeline. It is useful to test withou making/committing any changes to the code.
- Learning Resources - Learn more about Jenkins with these resources.