Certified Jenkins Engineer

Jenkins Pipelines

Pipeline and Jenkinsfile

Jenkins Pipeline enables you to automate complex Continuous Integration (CI) and Continuous Deployment (CD) workflows using code. By defining your build, test, and deploy steps in a Jenkinsfile, you gain version-controlled, reusable, and maintainable pipelines.

The image shows a Jenkins Pipeline flowchart with stages: Building, Unit Testing, Linting, Dockerizing, Security, Deployment, and Tests. Each stage is represented by an icon and label.

Breaking down your workflow into stages—like Build, Test, Lint, Dockerize, Security Scan, Deploy, and Post-deploy Tests—helps you:

  • Achieve clear separation of concerns
  • Easily spot failures and bottlenecks
  • Parallelize independent tasks (e.g., linting alongside unit tests)

Note

Storing your pipeline stages in code lets you audit history, perform code reviews, and roll back changes through Git.

1. Writing Your Jenkinsfile

A Jenkinsfile is a Groovy-based script that describes your pipeline. There are two syntaxes:

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        sh 'mvn clean package'
      }
    }
    stage('Unit Test') {
      steps {
        sh 'mvn test'
      }
    }
    stage('Dockerize') {
      steps {
        sh 'docker build -t myapp:${BUILD_NUMBER} .'
        sh 'docker push myapp:${BUILD_NUMBER}'
      }
    }
    stage('Deploy') {
      steps {
        sh 'kubectl apply -f deployment.yaml'
      }
    }
  }
}

Key elements:

DirectivePurpose
pipelineTop-level block defining the entire pipeline
agent anyExecutes stages on any available Jenkins agent
stagesContainer for all stage directives
stage('Name')Logical grouping of related steps
stepsActual shell, script, or plugin commands to run

2. Declarative vs. Scripted Pipelines

Jenkins supports two pipeline styles. Choose Declarative for simplicity or Scripted for advanced use cases.

The image compares two types of pipeline projects: Scripted Pipeline, which is code-centric with flexibility and a learning curve, and Declarative Pipeline, which is human-readable and easier to learn.

FeatureDeclarative PipelineScripted Pipeline
SyntaxOpinionated, YAML-like structureUnrestricted Groovy code
ReadabilityHigh—designed for easy understandingLower—requires Groovy/programming skills
FlexibilitySupports standard workflowsFull control with dynamic stages
Error HandlingBuilt-in post conditionsCustom try/catch logic
Learning CurveGentle—ideal for most teamsSteeper—suited to experienced developers

3. Pipeline vs. Freestyle Projects

Freestyle jobs are configured via the Jenkins UI, while Pipeline projects use code. Pipelines offer far greater power, versioning, and resilience.

The image is a comparison chart between "Pipeline" and "Freestyle" in terms of structure, configuration, and complexity, highlighting their differences in task execution, configuration methods, and suitability for workflows.

AspectPipeline ProjectsFreestyle Projects
StructureStage-based, supports parallel executionSequential build steps
ConfigurationJenkinsfile in source control (Git, SVN)Job DSL or manual UI configuration
ResilienceResumes after controller restartRestarts lost progress
ScalabilityComplex workflows, shared libraries, reusable stepsLimited by UI plugins

Warning

Avoid using Freestyle jobs for multi-stage pipelines or complex branching logic. Migrating to Declarative Pipelines reduces job sprawl and improves traceability.

4. Key Benefits of Jenkins Pipelines

The image lists the benefits of pipelines, including code as configuration, resilience by design, human interaction integration, handling complexity with ease, extensibility beyond limits, and streamlined build management.

  1. Code as Configuration
    Version your Jenkinsfile alongside application code for audit trails and collaborative editing.
  2. Resilience by Design
    Pipelines automatically resume after Jenkins controller restarts, preserving workflow state.
  3. Human Interaction
    Pause for manual approvals, input parameters, or interactive prompts.
  4. Advanced Workflow Control
    Use forks, joins, loops, and parallel stages to orchestrate complex CI/CD flows.
  5. Extensibility
    Leverage plugins or Shared Libraries to add custom pipeline steps.
  6. Unified Job Management
    Combine multiple build and deploy steps into a single Pipeline job, reducing maintenance overhead.

Watch Video

Watch video content

Previous
Demo Jenkins Fingerprints