Building a Jenkins CI/CD Pipeline: Step-by-Step Guide
Continuous Integration/Continuous Deployment (CI/CD) is essential for modern software development. Jenkins, the popular open-source automation server, simplifies building, testing, and deploying applications. In this guide, you'll learn to set up a basic Jenkins pipeline with practical examples and clear instructions.
Step 1: Installing Jenkins
Commands:
Install Java:
sudo apt update sudo apt install openjdk-11-jdk -y java -version
Add Jenkins Repository:
wget -q -O - <https://pkg.jenkins.io/debian-stable/jenkins.io.key> | sudo apt-key add - sudo sh -c 'echo deb <https://pkg.jenkins.io/debian-stable> binary/ > /etc/apt/sources.list.d/jenkins.list'
Install Jenkins:
sudo apt update sudo apt install jenkins -y sudo systemctl start jenkins sudo systemctl enable jenkins
Step 2: Setting Up Jenkins
Unlock Jenkins: Locate the initial admin password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Enter this password on the Jenkins setup page.
Install Suggested Plugins: On the "Customize Jenkins" page, choose "Install suggested plugins."
Create Admin User: Fill out the admin username, password, and email fields.
Step 3: Creating Your First Jenkins Pipeline
Go to the Jenkins dashboard.
Click "New Item" and select "Pipeline".
Enter a name for your pipeline, e.g.,
MyFirstPipeline
.In the pipeline configuration, select "Pipeline script" and paste the following code:
pipeline { agent any stages { stage('Build') { steps { echo 'Building the application...' // Simulate build step sh 'echo "Build Step Simulated"' } } stage('Test') { steps { echo 'Testing the application...' // Simulate test step sh 'echo "Test Step Simulated"' } } stage('Deploy') { steps { echo 'Deploying the application...' // Simulate deployment step sh 'echo "Deploy Step Simulated"' } } } }
Save the pipeline configuration.
On the pipeline page, click "Build Now" to run the pipeline.
Step 4: Adding Source Control Integration
Install the Git Plugin:
Navigate to "Manage Jenkins" > "Manage Plugins" > "Available" and search for "Git Plugin."
Install and restart Jenkins.
Update the pipeline script to pull code from a GitHub repository:
pipeline { agent any stages { stage('Checkout') { steps { git url: '<https://github.com/your-repo/sample.git>', branch: 'main' } } stage('Build') { steps { echo 'Building the application...' sh 'make build' } } stage('Test') { steps { echo 'Testing the application...' sh 'make test' } } } }
Step 5: Adding Notifications
Install the Slack Notification Plugin:
- Go to "Manage Jenkins > Manage Plugins", search for "Slack Notification," and install it.
Configure Slack:
In Jenkins, go to "Manage Jenkins > Configure System > Slack".
Enter your Slack team subdomain and integration token.
Update the pipeline script to send notifications:
pipeline { agent any stages { stage('Build') { steps { echo 'Building the application...' } } } post { success { slackSend(channel: '#builds', message: 'Build succeeded!') } failure { slackSend(channel: '#builds', message: 'Build failed.') } } }
Step 6: Monitoring and Troubleshooting
Monitor Builds:
- Check the pipeline dashboard for build status.
View Logs:
- Click a specific build and select "Console Output" for detailed logs.
Wrapping Up
Congratulations! You've set up a Jenkins pipeline that integrates with GitHub, runs build and test stages, and sends notifications. With these foundations, you can expand your pipeline to deploy applications to production environments or integrate advanced tools like Docker, Kubernetes, or SonarQube.
Encourage readers to experiment with Jenkins plugins and refine their pipeline scripts for real-world projects. Happy automating! ๐
Subscribe to my newsletter
Read articles from Suyash Bhawsar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Suyash Bhawsar
Suyash Bhawsar
Tech enthusiast, DevOps learner. Arch Linux w/ KDE. Rust learner. Harmonium player. Sudoku solver. Passionate about music and technology.