In this section, we will explore how to set up a Continuous Delivery (CD) pipeline using Jenkins. Continuous Delivery is a software development practice where code changes are automatically prepared for a release to production. It expands upon Continuous Integration by deploying all code changes to a testing environment and/or a production environment after the build stage.
Key Concepts
- Continuous Delivery (CD): The practice of automatically preparing code changes for release to production.
- Pipeline: A series of automated processes that take software from version control to production.
- Stages: Different phases in a pipeline, such as build, test, and deploy.
- Jenkinsfile: A text file that contains the definition of a Jenkins Pipeline and is checked into source control.
Steps to Set Up a CD Pipeline
- Define the Jenkinsfile
The Jenkinsfile is the cornerstone of your CD pipeline. It defines the stages and steps of your pipeline. Here is an example of a simple Jenkinsfile for a CD pipeline:
pipeline { agent any stages { stage('Build') { steps { echo 'Building...' // Add your build steps here } } stage('Test') { steps { echo 'Testing...' // Add your test steps here } } stage('Deploy to Staging') { steps { echo 'Deploying to Staging...' // Add your deployment steps here } } stage('Deploy to Production') { steps { input message: 'Approve deployment to production?', ok: 'Deploy' echo 'Deploying to Production...' // Add your production deployment steps here } } } }
- Create a New Pipeline Job in Jenkins
- Open Jenkins Dashboard.
- Click on "New Item".
- Enter a name for your pipeline and select "Pipeline".
- Click "OK".
- Configure the Pipeline
- In the pipeline configuration page, scroll down to the "Pipeline" section.
- Select "Pipeline script from SCM".
- Choose your SCM (e.g., Git) and provide the repository URL.
- Specify the path to your Jenkinsfile (e.g.,
Jenkinsfile
).
- Triggering the Pipeline
You can configure triggers to automatically start the pipeline when certain events occur, such as:
- SCM Polling: Jenkins will periodically check the repository for changes.
- Webhooks: Configure your version control system to notify Jenkins of changes.
- Running the Pipeline
Once configured, you can manually trigger the pipeline by clicking "Build Now" on the pipeline job page. Jenkins will execute the stages defined in the Jenkinsfile.
Practical Example
Let's create a more detailed Jenkinsfile for a Node.js application:
pipeline { agent any environment { NODE_ENV = 'production' } stages { stage('Build') { steps { script { def nodeVersion = '14.x' sh "nvm install ${nodeVersion}" sh "nvm use ${nodeVersion}" sh 'npm install' } } } stage('Test') { steps { sh 'npm test' } } stage('Deploy to Staging') { steps { sh 'npm run deploy:staging' } } stage('Deploy to Production') { steps { input message: 'Approve deployment to production?', ok: 'Deploy' sh 'npm run deploy:production' } } } }
Explanation
- Environment Variables:
NODE_ENV
is set to 'production'. - Build Stage: Installs the specified Node.js version and dependencies.
- Test Stage: Runs the tests.
- Deploy to Staging: Deploys the application to the staging environment.
- Deploy to Production: Waits for manual approval before deploying to production.
Exercises
Exercise 1: Create a Simple CD Pipeline
- Create a new repository and add a simple application (e.g., a "Hello World" Node.js app).
- Write a Jenkinsfile that includes build, test, and deploy stages.
- Configure a Jenkins pipeline job to use this repository.
- Trigger the pipeline and observe the stages.
Exercise 2: Add Manual Approval
- Modify the Jenkinsfile to include a manual approval step before deploying to production.
- Trigger the pipeline and approve the deployment when prompted.
Solutions
Solution to Exercise 1
- Create a repository and add a simple Node.js app.
- Example Jenkinsfile:
pipeline { agent any stages { stage('Build') { steps { sh 'npm install' } } stage('Test') { steps { sh 'npm test' } } stage('Deploy to Staging') { steps { sh 'npm run deploy:staging' } } stage('Deploy to Production') { steps { input message: 'Approve deployment to production?', ok: 'Deploy' sh 'npm run deploy:production' } } } }
- Configure the Jenkins pipeline job as described in the steps above.
- Trigger the pipeline and observe the stages.
Solution to Exercise 2
- Modify the Jenkinsfile to include the
input
step before the production deployment. - Trigger the pipeline and approve the deployment when prompted.
Summary
In this section, we covered the basics of setting up a Continuous Delivery pipeline using Jenkins. We learned how to define a Jenkinsfile, create and configure a pipeline job, and trigger the pipeline. We also explored a practical example and provided exercises to reinforce the concepts. In the next section, we will delve into automating deployments in more detail.
Jenkins: From Beginner to Advanced
Module 1: Introduction to Jenkins
Module 2: Jenkins Basics
- Jenkins Dashboard Overview
- Creating and Running Jobs
- Understanding Jenkins Pipelines
- Using Jenkins Plugins
Module 3: Jenkins Pipelines
Module 4: Advanced Jenkins Pipelines
- Pipeline Stages and Steps
- Parallel Execution in Pipelines
- Using Environment Variables
- Pipeline Best Practices
Module 5: Jenkins Administration
Module 6: Integrating Jenkins
- Integrating with Version Control Systems
- Integrating with Build Tools
- Integrating with Testing Tools
- Integrating with Deployment Tools