Jenkins Pipeline Example Step by Step
By John Johnson
Introduction
Are you tired of manually deploying your code? Do you want to automate your build, test, and deployment process? Look no further than Jenkins Pipeline!
As a software developer, I’ve experimented with various CI/CD tools, but Jenkins Pipeline remains my favorite. In this article, I’ll walk you through a step-by-step example of how to set up Jenkins Pipeline for your project.
Curiosities, Statistics, and Facts
- Jenkins is the most widely used open-source CI/CD tool, with over 1.5 million installations worldwide.
- Jenkins Pipeline is a powerful plugin that allows you to define and automate your build, test, and deployment process as a code.
- According to a survey by DZone, 79% of developers prefer Jenkins as their CI/CD tool.
- Jenkins Pipeline supports multiple programming languages, including Java, Python, and Node.js.
Setting Up Jenkins Pipeline
Assuming you already have Jenkins installed, let’s start by installing the Pipeline plugin. Go to the Jenkins dashboard, click on Manage Jenkins, then Manage Plugins. In the Available tab, search for Pipeline, select the checkbox next to Pipeline and click Install without restart.
Once the plugin is installed, we can create a new Pipeline job. Click on New Item on the Jenkins dashboard, enter a name for the job, select Pipeline and click OK.
In the Pipeline section, select Pipeline script from SCM as the Definition. Choose your version control system (e.g., Git), enter the repository URL, and select the branch to build. In the Script Path field, enter the path to your Jenkinsfile (e.g., Jenkinsfile).
Save the job and click on Build Now to trigger the first build.
Writing a Jenkinsfile
A Jenkinsfile is a Groovy script that defines your Pipeline. It’s usually located at the root of your project, and you can version control it along with your code.
Let’s create a simple Jenkinsfile that builds and tests a Node.js project:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'npm install'
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
}
}This Jenkinsfile defines two stages: Build and Test. In the Build stage, we run the npm install command to install the project dependencies. In the Test stage, we run the npm test command to execute the unit tests.
Adding Deployment
Now that we have our build and test stages, let’s add a deployment stage. We’ll assume that we’re deploying to a remote server via SSH.
First, we need to install the SSH Agent plugin on Jenkins. Go to Manage Jenkins, then Manage Plugins, and search for SSH Agent. Install the plugin without restarting.
Next, let’s add a Deploy stage to our Jenkinsfile:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'npm install'
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
stage('Deploy') {
steps {
sshagent(['my-ssh-credentials']) {
sh 'ssh user@remote-server cd /path/to/deploy && git pull'
}
}
}
}
}In the Deploy stage, we use the sshagent step to load our SSH credentials from Jenkins. Then, we run the ssh command to connect to the remote server and execute the git pull command.
You’ll need to replace my-ssh-credentials, user, remote-server, /path/to/deploy, and git pull with your actual values.
FAQs
Q: Can Jenkins Pipeline be used with any programming language?
A: Yes, Jenkins Pipeline supports multiple programming languages, including Java, Python, and Node.js.
Q: Do I need to install any plugins to use Jenkins Pipeline?
A: Yes, you’ll need to install the Pipeline and SSH Agent plugins.
Q: Can I define my Pipeline as code?
A: Yes, a Jenkinsfile is a Groovy script that defines your Pipeline as code.