0
0
JenkinsHow-ToBeginner · 4 min read

How to Create a CI/CD Pipeline in Jenkins: Step-by-Step Guide

To create a CI/CD pipeline in Jenkins, define a Jenkinsfile that describes your build, test, and deploy stages using the pipeline syntax. Then, create a Jenkins pipeline job that points to this Jenkinsfile in your source code repository to automate your software delivery process.
📐

Syntax

The basic syntax for a Jenkins pipeline uses the pipeline block with agent to specify where to run, and stages to define steps like build, test, and deploy.

  • pipeline: The root block for the pipeline script.
  • agent: Defines the environment where the pipeline runs (e.g., any available agent).
  • stages: Contains multiple stage blocks representing steps.
  • stage: A single step like build or test.
  • steps: Commands to execute inside a stage.
groovy
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing...'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying...'
            }
        }
    }
}
💻

Example

This example Jenkinsfile shows a simple CI/CD pipeline with build, test, and deploy stages that print messages. It demonstrates how to automate these steps in Jenkins.

groovy
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying application...'
            }
        }
    }
}
Output
[Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /var/lib/jenkins/workspace/example-pipeline [Pipeline] { [Pipeline] stage [Pipeline] { (Build) [Pipeline] echo Building the application... [Pipeline] } [Pipeline] stage [Pipeline] { (Test) [Pipeline] echo Running tests... [Pipeline] } [Pipeline] stage [Pipeline] { (Deploy) [Pipeline] echo Deploying application... [Pipeline] } [Pipeline] } [Pipeline] End of Pipeline
⚠️

Common Pitfalls

Common mistakes when creating Jenkins pipelines include:

  • Not committing the Jenkinsfile to the source repository, so Jenkins cannot find it.
  • Using incorrect syntax or missing required blocks like agent or stages.
  • Not configuring the Jenkins job to use pipeline script from SCM (source control).
  • Forgetting to install necessary plugins like Pipeline plugin.

Always validate your Jenkinsfile syntax and test pipeline steps incrementally.

groovy
/* Wrong Jenkinsfile example missing agent block */
pipeline {
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
    }
}

/* Correct Jenkinsfile with agent block */
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
    }
}
📊

Quick Reference

ConceptDescription
pipelineRoot block defining the entire pipeline
agentSpecifies where the pipeline runs (e.g., any)
stagesGroups multiple stages of the pipeline
stageDefines a single step like build or test
stepsCommands executed inside a stage
echoPrints messages to the Jenkins console

Key Takeaways

Create a Jenkinsfile with pipeline syntax to define your CI/CD steps.
Use the agent block to specify the environment for running the pipeline.
Commit the Jenkinsfile to your source repository and link it in Jenkins job.
Test your pipeline syntax to avoid common errors and missing blocks.
Install necessary Jenkins plugins like Pipeline for smooth operation.