Welcome, aspiring developers! You’ve written some code, maybe pushed it to a repository, but how do you get it from your computer to a server for the world to see, automatically and reliably? That’s where CI/CD comes in, and Jenkins is one of the most popular tools to make it happen.
In this guide, we’ll walk you through setting up a simple CI/CD pipeline with Jenkins. We’ll cover the basics, from installing Jenkins to configuring a basic pipeline that can build and potentially deploy a simple application. By the end, you’ll have a foundational understanding of how continuous integration and continuous delivery work in practice.
What Exactly is CI/CD?
Before we dive into Jenkins, let’s quickly understand the core concepts:
- Continuous Integration (CI): This is the practice of frequently merging code changes from multiple developers into a shared main branch, followed by automated testing (like unit tests) to detect integration issues early. Think of it as constantly checking if everyone’s code plays nicely together.
- Continuous Delivery (CD): Building on CI, this is the process of automating the entire software release process. After successful integration and testing, the code is automatically prepared for release. This means you can deploy your application to production at any time with confidence.
Together, CI/CD helps teams build, test, and deploy software faster and more reliably. Now, let’s see how Jenkins helps us implement this.
Getting Started: Installing Jenkins
The first step in our simple CI/CD Jenkins journey is getting Jenkins up and running. Jenkins is a Java-based application, so you’ll need Java installed.
Prerequisites:
- A server or computer (Linux, Windows, or macOS)
- Java Development Kit (JDK) installed (version 8 or 11 are commonly used)
Installation Steps (Example for Ubuntu/Debian Linux):
- Add the Jenkins repository key:
bash
wget -q -O – https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add – - Add the Jenkins repository to your system’s sources list:
bash
sudo sh -c ‘echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list’ - Update your package list:
bash
sudo apt update - Install Jenkins:
bash
sudo apt install jenkins - Start the Jenkins service:
bash
sudo systemctl start jenkins - Check the status (optional):
bash
sudo systemctl status jenkins
Once installed, you should be able to access Jenkins through your web browser at http://your_server_ip:8080.
Initial Jenkins Setup
When you access Jenkins for the first time, you’ll be guided through a setup process:
1. Unlock Jenkins: You’ll be asked for an initial admin password. This password is in a file on your server. The Jenkins page will tell you the exact path (usually /var/lib/jenkins/secrets/initialAdminPassword on Linux). Retrieve this password and enter it.
2. Install Plugins: Jenkins is highly extensible through plugins. For a jenkins pipeline tutorial for beginners, select “Install suggested plugins”. This will install essential plugins for building, testing, and deployment.
3. Create Admin User: Create your first admin user.
4. Configure URL: Confirm the Jenkins URL.
After these steps, Jenkins is ready to go!
Building Your First Simple Jenkins Pipeline
Now for the core part: creating a pipeline to automate code deployment jenkins. We’ll create a simple “Pipeline” type job.
- From the Jenkins dashboard, click “New Item”.
- Enter an item name (e.g., MySimpleApp-Pipeline).
- Select “Pipeline” and click “OK”.
On the configuration page, scroll down to the “Pipeline” section. Here, you define your pipeline using a Jenkinsfile. A Jenkinsfile is a text file that defines the steps of your pipeline. It can be written using Groovy syntax.
For a simple ci cd jenkins example, let’s define a pipeline that does the following:
- Checks out code from a Git repository.
- Builds the code (e.g., runs a simple script).
- Tests the code (e.g., runs tests).
- (Optional) Deploys the code.
You can write the Jenkinsfile directly in the “Script” area, or better yet, store it in your source code repository and tell Jenkins to read it from there (“Pipeline script from SCM”).
Here’s a very basic example of a Jenkinsfile script you could put in the “Script” area for a simple application (replace placeholders with your actual commands):
pipeline {
agent any // Run on any available agent
stages {
stage('Checkout') {
steps {
// Clone the repository
git 'https://github.com/your-username/your-repo.git'
}
}
stage('Build') {
steps {
echo 'Building the application…'
// Replace with your build command, e.g.:
// sh 'mvn clean install'
sh 'echo "Build completed!"'
}
}
stage('Test') {
steps {
echo 'Running tests…'
// Replace with your test command, e.g.:
// sh 'npm test'
sh 'echo "Tests passed!"'
}
}
stage('Deploy') {
steps {
echo 'Deploying the application…'
// Replace with your deployment command/script, e.g.:
// sh './deploy.sh'
sh 'echo "Deployment simulated!"'
}
}
}
}
Explanation:
- pipeline: Defines the entire pipeline.
- agent any: Specifies where the pipeline should run. any means on any available Jenkins agent.
- stages: Contains the sequence of stages in your pipeline.
- stage(‘Stage Name’): Defines a stage (e.g., ‘Checkout’, ‘Build’).
- steps: Contains the individual steps within a stage.
- git ‘…’: A step to clone a Git repository.
- sh ‘…’: A step to execute a shell command.
- echo ‘…’: A step to print a message to the console output.
Replace the placeholder commands (git ‘…’, sh ‘echo “…”‘) with the actual commands needed to checkout, build, test, and deploy *your* specific application code.
Click “Save” to create your pipeline job.
Running Your Pipeline
Now that your pipeline job is configured, you can run it manually:
1. Go to the dashboard.
2. Click on your pipeline job name (MySimpleApp-Pipeline).
3. Click “Build Now” on the left-hand menu.
Jenkins will start executing the stages defined in your Jenkinsfile. You can click on the build number that appears in the “Build History” and then click “Console Output” to see the progress and output of each step.
If everything is configured correctly, your pipeline should successfully checkout the code, run the build and test steps, and execute the deploy step!
What’s Next?
You’ve successfully set up ci cd pipeline jenkins for a basic application! This is just the beginning. To take this further, you can explore:
- Triggering builds automatically: Configure your pipeline to run every time code is pushed to your Git repository using webhooks.
- Adding more complex steps: Integrate static analysis tools, security scans, or more sophisticated deployment strategies.
- Using Jenkins plugins: Explore plugins for specific technologies or deployment targets (like Docker, Kubernetes, cloud providers).
- Parameterizing builds: Allow users to specify options when triggering a build.
This jenkins build deploy tutorial provides a solid foundation for understanding and implementing CI/CD with Jenkins.
Conclusion
Setting up a CI/CD pipeline might seem daunting at first, but tools like Jenkins make it accessible even for beginner developers. By automating your build, test, and deployment processes, you can significantly improve your development workflow, reduce errors, and deliver features faster.
We hope this simple guide helps you take the first step into the world of automated software delivery!