DEV Community

Raghwendra Sonu
Raghwendra Sonu

Posted on

Create Jenkins Pipeline with an Example

In this article we are going to talk what is a Pipeline and how to create a pipeline in Jenkins.
Jenkins Pipeline is one of the plugin in Jenkins. This allows creating a workflow inside Jenkins. Very much similar to another Jenkins jobs, but, Pipeline jobs can be controlled with a Jenkins file. So, you can specify what to be done in the Jenkins file and the job will run accordingly. Groovy DSL is used to write this Jenkins file. You need to specify agents, stages, steps and action to be done in the file. The file name should be Jenkinsfile, so that Jenkins can identify the file. Let us take an example of creating a Jenkins Pipeline with maven.

Inside your project create a new file with name Jenkinsfile with below content:

pipeline {
agent any
stages {
stage ('Compile Stage') {
steps {
withMaven(maven : 'apache-maven-3.6.1') {
bat'mvn clean compile'
}
}
}
stage ('Testing Stage') {
steps {
withMaven(maven : 'apache-maven-3.6.1') {
bat'mvn test'
}
}
}
stage ('Install Stage') {
steps {
withMaven(maven : 'apache-maven-3.6.1') {
bat'mvn install'
}
}
}
}
}

Push the code changes in Git. You can use my GitHub repository for practice and build your own on top of this.
Here is the link: https://github.com/sonucogni/Jenkins_Pipeline_Demo

One thing We need to ensure that the name of Maven given in Jenkins Global Configuration matches with the Maven name in Jenkinsfile.
Alt Text

So, as you see in below screenshot, name of Maven is "apache-maven-3.6.1", so in Jenkins file also we have used the same.
Alt Text

Next step is to create a new Jenkins Pipeline Job, ensure that you have "Pipeline" plugin and "Pipeline Maven Integration" plugin is installed in your Jenkins instance.

Job will look as below, Save the job and build, this will search for the Jenkinsfile and will perform all the stages which we have defined inside.

Alt Text

Once the execution is over, it will show all stages and so it becomes so easy for us for debugging in- case of any failures. This is all about pipeline.

Alt Text

For more details refer : https://jenkins.io/doc/book/pipeline/getting-started/

Top comments (1)

Collapse
 
khoale1605 profile image
khoale1605 • Edited

Hi, I've built my own repo with following your instruction but after all I got this error,

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

And

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project csc-integral-cas-server: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] cwiki.apache.org/confluence/displa...
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn -rf :csc-integral-cas-server

with my:
JAVE_HOME is C:\Program Files\Java\jdk1.8.0_241
M2_HOME is C:\Program Files\apache-maven-3.6.3

Can you help me out please. Thank you.