Continuous integration (CI) and continuous delivery (CD) are practices in modern software development that enable teams to automate the testing and deployment of applications. Jenkins, an open-source automation server, provides the tools needed to implement CI and CD effectively. This guide explains how to set up and use Jenkins for these practices, including detailed examples and terminal outputs.
What is Jenkins?
Jenkins is a Java-based open-source automation server that allows developers to build, test, and deploy their software projects automatically. It’s widely used for managing continuous integration and continuous delivery workflows.
Setting up Jenkins for CI/CD
Installation:
- Jenkins can be installed on various operating systems. On Ubuntu, you can install Jenkins by adding the repository key to your system, then installing Jenkins via apt-get. Here’s how you can do it:Terminalwget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'sudo apt-get updatesudo apt-get install jenkins
- Once installed, start Jenkins using
sudo systemctl start jenkins
, and ensure it’s enabled on boot withsudo systemctl enable jenkins
.
- Jenkins can be installed on various operating systems. On Ubuntu, you can install Jenkins by adding the repository key to your system, then installing Jenkins via apt-get. Here’s how you can do it:
Initial setup:
- Access Jenkins through your web browser at
http://your_server_ip_or_domain:8080
. The initial setup screen asks you to enter the administrator password, found in/var/lib/jenkins/secrets/initialAdminPassword
. - Follow the on-screen instructions to install the suggested plugins and set up the first admin user.
- Access Jenkins through your web browser at
Creating a job for CI:
- Go to the Jenkins dashboard and click on "New Item".
- Enter a name for your project, select "Freestyle project", and click OK.
- Under Source Code Management, select Git and enter your repository URL and credentials if needed.
- In the Build Triggers section, check "Poll SCM" and schedule when Jenkins should poll the repository for changes, using cron syntax, like
H/5 * * * *
(every five minutes).
Configuring build steps:
- In the Build section, click on "Add build step" and select "Execute shell".
- Enter the commands required to build your project. For example:Terminalnpm installnpm test
Setting up CD:
- Under Post-build Actions, click "Add post-build action" and select "Deploy war/ear to a container".
- Configure the necessary parameters, such as container name and credentials.
Integrating with other tools
Jenkins can integrate with various development, testing, and deployment tools, enhancing your CI/CD pipeline:
- Version control systems: Different version control tools like Git can be integrated through plugins.
- Build tools: In Jenkins, the "Build" step allows for the configuration of build tools like Maven and Gradle, specifying tasks and parameters that Jenkins will execute to compile and package the software.
- Testing frameworks: Jenkins includes plugin support for many different testing frameworks like JUnit, allowing you to run and display test results directly from the pipeline.
- Deployment tools: Jenkins can deploy your applications using plugins like the "Deploy to container" plugin.
Automation scripts
Using Jenkins' Pipeline (a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins), you can define your build, test, and deploy phases as code. Here's an example of a Jenkins Pipeline script:
pipeline {agent anystages {stage('Build') {steps {sh 'echo "Building..."'sh 'npm install'}}stage('Test') {steps {sh 'echo "Testing..."'sh 'npm test'}}stage('Deploy') {steps {sh 'echo "Deploying..."'sh 'scp -r build/* user@deployment-server:/path/to/application'}}}}
This script runs on any agent, goes through build, test, and deploy stages, and uses shell commands to execute tasks.
Monitoring and notifications
- Monitoring: Jenkins provides various ways to monitor the pipeline through the use of plugins like Timestamper for adding timestamps to console output or the Build Monitor Plugin for a more graphical representation of pipeline status.
- Notifications: Jenkins can notify team members of build statuses through emails, Slack messages, or other means using plugins like Email-ext or the Slack Notification plugin.
With these setups, you can automate the entire process from code commit to deployment, reducing manual errors and increasing productivity. Jenkins, with its extensive plugin ecosystem, offers a flexible way to adapt CI/CD to any project's needs.
For further reading on CI/CD in Jenkins, see the official Jenkins documentation.