Jenkins For Beginners
Jenkins Pipelines
Simple Pipeline Job
In this guide, you'll learn how to create a simple Pipeline Job in Jenkins. Follow along as we walk through configuring a basic pipeline, inspecting its stages, and later integrating additional build steps with Maven.
Creating the Pipeline Job
Begin by opening the Jenkins UI and clicking on New Item. Select the Pipeline option and assign a name to your job (for example, "Hello World Pipeline"). Finally, click OK.
On the job configuration page, you'll encounter several sections including General, Advanced, Project Options, and Pipeline. Here, you can add a description and configure project-specific options. One useful configuration is preventing the pipeline from resuming after a controller restart—a feature that was previously discussed with Freestyle Projects.
Defining the Pipeline Script
You can define the pipeline script in two ways:
- Directly inline on the configuration page.
- Automatically sourced from a Version Control System like Git or Subversion.
For this tutorial, we'll use the Pipeline Script option to create a simple "Hello World" pipeline. In later sections, we will explore scripted pipelines in more detail.
Enter the following pipeline script:
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
Within this script:
- The
agent any
directive indicates that the job can run on any available node (commonly the Jenkins controller in basic setups). - The pipeline contains one stage named "Hello" that executes a step to display the message "Hello World" using the
echo
command. - Optionally, you can enable the Groovy sandbox for restricted execution. Disabling it without proper administrative approval may cause the job to wait for authorization.
After configuring the pipeline, click Build Now. When the build starts, you'll see a visual representation of the pipeline execution. Clicking on the "Hello" stage displays the console logs, where you can observe the "Hello World" output. The raw logs can also be reviewed by clicking on the success icon.
Additionally, the build log provides detailed execution output similar to the example below:
Started by user Dasher Admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/hello-world-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
Hello World
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
This log includes the execution timings, the pipeline overview, and the individual step logs. The interface also supports restarting from a specific stage or replaying the pipeline if necessary.
Extending the Pipeline: GitHub and Maven Integration
Next, we'll extend the pipeline by adding stages that integrate with GitHub and use Maven for project builds.
The example below demonstrates a pipeline that:
- Configures a Maven tool installation.
- Creates a stage named "Echo Version" to output a message along with the Maven version.
- Includes a stage named "Build" that checks out code from a GitHub repository.
Here is the enhanced pipeline script:
pipeline {
agent any
tools {
// Installs the Maven version configured as "M3" and adds it to the environment PATH.
maven "M3"
}
stages {
stage('Echo Version') {
steps {
sh 'echo Print Maven Version'
sh 'mvn -version'
}
}
stage('Build') {
steps {
// Clone code from a GitHub repository.
git 'https://github.com/jglick/simple-maven-project-with-tests.git'
}
}
}
}
When you trigger the build, you might encounter an error message similar to the following if Maven is not correctly configured:
Started by user Dasher Admin
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 6: Tool type "maven" does not have an install of "M3" configured - did you mean "null"? @ line 6, column 15.
maven "M3"
^
1 error
...
Finished: FAILURE
This error indicates that Jenkins could not find a Maven installation named "M3".
Configuring Maven in Jenkins
To resolve this Maven configuration issue, follow these steps:
- Open the Jenkins Dashboard and navigate to Manage Jenkins.
- Click on Global Tool Configuration (or simply Tools in some Jenkins versions).
- Scroll down to the Maven section. Since no Maven installation is currently configured, add a new installation.
If you have Maven installed on your node, provide its installation path. Otherwise, you can configure Jenkins to automatically install Maven from Apache. For example, set up Maven version 3.9.8 and assign it a unique name such as M398—this naming makes it clear which version is in use.
After configuring Maven, go back to your pipeline configuration and update the tools
section to reference the newly named installation:
pipeline {
agent any
tools {
// Installs the Maven version configured as "M398" and adds it to the environment PATH.
maven "M398"
}
stages {
stage('Echo Version') {
steps {
sh 'echo Print Maven Version'
sh 'mvn -version'
}
}
// Uncomment the Build stage once tool configuration is verified.
// stage('Build') {
// steps {
// git 'https://github.com/jglick/simple-maven-project-with-tests.git'
// }
// }
}
}
Triggering the build now should succeed. In the console output, you will first observe Maven being installed (if necessary) and unpacked. The "Echo Version" stage will then run, printing "Print Maven Version" followed by details about the Maven version installed. An example output might look like this:
+ echo Print Maven Version
Print Maven Version
+ mvn --version
Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cff34256)
Maven home: /var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/M398
Java version: 17.0.12, vendor: Ubuntu, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.8.0-39-generic", arch: "amd64", family: "unix"
This confirms that Maven has been correctly configured in Jenkins and the pipeline is executing using the Groovy sandbox.
Conclusion
In this article, we demonstrated how to create a simple Pipeline Job in Jenkins, from setting up a basic "Hello World" pipeline to extending it with GitHub integration and Maven build steps. We also addressed Maven configuration issues and provided steps to resolve them, ensuring smooth pipeline execution.
Note
For further insights, explore other Jenkins documentation such as the Jenkins Pipeline Documentation to deepen your understanding of more advanced pipeline features.
Thank you for reading, and happy building!
Watch Video
Watch video content