Operations 5 min read

Understanding Jenkins Declarative Pipeline Syntax

This article explains Jenkins declarative pipeline syntax, showing how to define pipelines, agents, stages, and steps with code examples—including simple and Docker‑based pipelines—and describes optional elements such as environment variables, options, tools, triggers, parameters, and post‑actions, providing a practical guide for DevOps practitioners.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Understanding Jenkins Declarative Pipeline Syntax

Jenkins pipelines enable users to build complete continuous delivery (CD) pipelines as part of their application code, storing build, test, and delivery steps in a Jenkinsfile . The declarative pipeline syntax offers a simple predefined hierarchy that is accessible to users of all experience levels.

Below is a minimal pipeline that runs on an agent and contains a single Build stage executing a Maven install command:

pipeline {
  agent {
      label ''
  }
  stages {
    stage('Build') {
      steps{
      sh 'mvn install'
    }
   }
  }
}

An extended example demonstrates using a Docker container to build a Java application with Maven, limiting execution to the master branch and applying a six‑hour timeout:

pipeline {
  agent {
    docker {
      label ‘docker-node’
      image ‘maven’
      args ‘-v /tmp:/tmp -p 80:80’
    }
  }
  environment {
    GIT_COMMITTER_NAME = ‘jenkins’
  }
  options {
    timeout(6, HOURS)
  }
  stages {
    stage(‘Build’) {
      steps {
         sh ‘mvn clean install’
      }
    }

    stage(‘Archive’) {
      when {
        branch ‘*/master’
      }
      steps {
        archive ‘*/target/**/*’
        junit ‘*/target/surefire-reports/*.xml’
      }
    }
  }

  post {
    always {
       deleteDir()
    }
  }
}

Declarative Pipeline Syntax (Required)

pipeline : defines a Jenkins pipeline.

agent : specifies the node that executes pipeline stages. Options include label for a Jenkins node label or docker to run inside a Docker container (with image and args ).

stages : the collection of stages in the pipeline.

stage : a single stage within the pipeline.

steps : individual build steps such as sh , bat , timeout , echo , archive , junit , etc.

when : conditions for running a stage (e.g., based on branch or variable).

Additional sections can appear inside a stage: agent , environment , tools , and post .

Declarative Pipeline Syntax (Optional)

environment : defines environment variables for the pipeline.

options : sets pipeline execution options such as skipDefaultCheckout , timeout , buildDiscarder , and disableConcurrentBuilds .

tools : makes pre‑installed tools available in the pipeline.

triggers : configures pipeline scheduling and build triggers.

parameters : defines runtime parameters for the pipeline.

post : defines actions to run after the pipeline completes, with conditions like always , success , and failure .

About Us

Ze Yang is a DevOps practitioner focusing on enterprise‑level DevOps operations and development. He shares practical experience through courses on Linux operations and DevOps technologies, offering content derived from real‑world enterprise applications.

DockerCI/CDdevopsMavenJenkinsDeclarative Pipeline
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.