Before you can build a pipeline, you need a Jenkins server. Jenkins can be installed on a variety of operating systems. For this lesson, we’ll assume a standard installation on a Linux server.
Install Jenkins: Follow the official Jenkins documentation for your operating system. This typically involves adding the Jenkins package repository, installing the Jekins package, and starting the Jenkins service.
Initial Setup: Once the service is running, open your web browser and navigate to http://<your_server_ip>:8080
. You will be prompted to enter an initial administrator password, which you can find on the server.
Plugin Installation: Jenkins will then guide you to install plugins. It’s recommended to choose “Install suggested plugins” to get a solid foundation. You can always add more later.
Create Admin User: Finish the setup by creating your first administrator user account.
Your Jenkins server is now ready to build pipelines.
A Jenkins pipeline is an automated workflow that orchestrates your CI/CD process. We define it in a file called a Jekins File, which lives in your project’s Git repository. This is known as Pipeline as Code.
A Jenkins pipeline is structured into stages, which represent logical steps in your workflow.
Build: This stage compiles your source code, runs unit tests, and packages the application. For a Node.js application, this might involve running nmp.install and nmp.build
Test: This stage runs more comprehensive tests, such as integration or end-to-end tests. It ensures that different components of your application work together as expected.
Deploy: This stage deploys the built application to an environment. This could be a staging environment for further testing or the production environment for a final release.
Here is a simple Jenkins file for a web application. It defines three stages: Build
, Test
, and Deploy
.
// Jenkinsfile
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building the application...'
// A command to build the application
sh 'npm run build'
}
}
stage('Test') {
steps {
echo 'Running tests...'
// A command to run automated tests
sh 'npm test'
}
}
stage('Deploy') {
steps {
echo 'Deploying to a staging environment...'
// A command to deploy the application
sh './deploy-script.sh'
}
}
}
}
Git Integration: You configure Jenkins to monitor a Git repository. When a developer pushes a new commit to a specific branch, Jenkins automatically triggers a new pipeline run. This is the essence of Continuous Integration. You provide Jenkins with the URL of your Git repository and the credentials to access it.
Other Tools: You can integrate with tools for containerization (Docker), infrastructure as code (Terraform), and a host of other services through plugins. Jenkins can run shell commands, execute scripts, and interact with APIs to automate any part of your workflow.
Once the is in your repository and Jenkins is configured, your pipeline becomes fully automated.
A developer commits code and pushes to the Git repository.
Jenkins detects the change and clones the repository.
The pipeline starts, running the Build stage to compile and package the code.
If the build succeeds, the Test stage runs, executing all automated tests.
If all tests pass, the Deploy Jekins Filestage pushes the application to a staging environment.
This automated process provides rapid feedback to developers. If the build fails or a test breaks, Jenkins immediately notifies the team, allowing them to fix the issue before it becomes a bigger problem.