Skip to main content
XenonStack

DevOps

XenonStack Blog Implementing Pipeline as Code with Jenkins and Apache Groovy

and | Published | Updated

Pipeline as Code with Jenkins: Jenkinsfile and Groovy
8:07

Key takeaway

  • Pipeline as code with Jenkins: how a Jenkinsfile works, Declarative vs Scripted Groovy, a minimal example, adoption steps, trade-offs and governance.

What is pipeline as code with Jenkins and Groovy?

Pipeline as code means the whole delivery flow, from build to test to deploy, is written in a text file called a Jenkinsfile and kept in source control beside the application. According to the Jenkins Pipeline documentation, the definition of a Pipeline is written into that file, and this is generally considered best practice.

Groovy comes in through Scripted Pipeline, which the Pipeline Syntax page describes as effectively a general-purpose DSL built with Groovy. Declarative Pipeline wraps a stricter structure around the same idea. The rest of this guide shows how to choose between them, move off hand-built jobs, and keep pipelines governed. For a full delivery example, see our continuous delivery pipeline with Jenkins guide.

Why move from chained jobs to a Jenkinsfile?

Older Jenkins setups create one Freestyle job per step (build, test, deploy) and chain them with post-build actions. The Jenkins documentation notes that Jenkins has always allowed this kind of rudimentary chaining, and that Pipeline makes sequential tasks a first-class citizen. The documented benefits of a Jenkinsfile in source control are:

Two columns: chained Freestyle jobs (one job per step, settings in the UI, little review, fragile to restarts) mapped to pipeline as code (Jenkinsfile stages, branch builds, review and audit, durable pipelines).
  • An automatic pipeline build for each branch and pull request.
  • Code review of the pipeline itself.
  • An audit trail of pipeline changes.
  • A single source of truth that the whole team can see.

Pipeline is also described as durable, surviving planned and unplanned controller restarts, and it can pause for human approval. Source: Jenkins Pipeline documentation.

Declarative or Scripted: which should you use?

Both styles share many steps, such as sh and junit, but they are built differently. The Pipeline Syntax page sets out the trade-off.

StyleWhat it isWhen it fits
DeclarativeA pipeline block with a strict, pre-defined structureSimpler delivery pipelines; easier to read and review
Scriptednode blocks governed mainly by Groovy, with very few limitsAdvanced cases with complex logic and experienced authors

The script step lets a Declarative pipeline run a block of Scripted code. The documentation calls it an escape hatch and recommends moving large or complex script blocks into Shared Libraries.

What does a minimal Jenkinsfile look like?

This example follows the style of the Jenkins Pipeline Syntax page. The agent section says where the pipeline or a stage runs and is required at the top level. Parameters include any, none, label, node, docker and dockerfile.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the project'
            }
        }
        stage('Test') {
            steps {
                echo 'Running the tests'
            }
        }
    }
}

How do you adopt pipeline as code step by step?

  1. Install Jenkins and confirm the Pipeline plugin suite is present. For a Kubernetes install, follow our Jenkins on Kubernetes setup guide.
  2. Pick one existing chain of Freestyle jobs and write its build, test and deploy steps as stages in a Declarative Jenkinsfile.
  3. Commit the Jenkinsfile to the application repository and point a Pipeline or Multibranch job at it.
  4. Run it on a branch, compare results with the old chain, then retire the old jobs.
  5. Add review rules for Jenkinsfile changes, as for any code.
  6. Move repeated logic into Shared Libraries instead of long script blocks.

What are the limits and trade-offs?

Because Pipeline must survive a controller restart, it serialises data back to the controller. The Pipeline Syntax page says Scripted Pipeline must serialize data back to the controller, and that some Groovy idioms, such as certain collection.each patterns, are not fully supported. Scripted freedom also costs readability. Keep Declarative as the default and use Scripted only where logic needs it. Check the Jenkins security documentation before running shared or untrusted pipeline code.

How does this fit governance and the wider delivery checklist?

A Jenkinsfile in source control is a policy surface: who may change a pipeline, who reviews it and what a deploy stage may touch all become reviewable. Pair it with a broader DevOps checklist. When AI agents start proposing or running pipeline changes, ElixirData Context OS keeps governed context, versioned policy and decision traces in one decision layer above AI agents, and AgentGRC keeps autonomous agent actions inside policy, with the audit trail risk teams need.

To map one pipeline decision your agents should make, and the context and policy it depends on, book a Context Workshop. See what happens in the workshop.

Frequently asked questions

What is pipeline as code in Jenkins?

It means the definition of a Jenkins Pipeline is written into a text file, called a Jenkinsfile, that can be committed to source control. According to the Jenkins documentation, this treats the pipeline as part of the application, to be versioned and reviewed.

Do I need to learn Groovy to write a Jenkinsfile?

Not for simple pipelines. Declarative Pipeline is designed to make writing and reading pipeline code easier, with a strict, pre-defined structure. Scripted Pipeline is built on Groovy and gives more freedom, so Groovy knowledge matters when you use it.

What is the difference between Declarative and Scripted Pipeline?

According to the Jenkins documentation, Declarative uses a structured pipeline block and suits simpler delivery pipelines, while Scripted uses node blocks, is governed mainly by Groovy and suits advanced users with complex needs.

Why is a Jenkinsfile better than chained Freestyle jobs?

Jenkins has always allowed rudimentary chaining of Freestyle jobs, but Pipeline makes sequential delivery a first-class feature. A Jenkinsfile in source control also gives review, an audit trail and one source of truth, as the Jenkins documentation lists.

What happens to a running pipeline if the Jenkins controller restarts?

The Jenkins documentation describes Pipeline as durable: it survives both planned and unplanned controller restarts. This is why scripted code must serialise data, and why some Groovy idioms are not fully supported.

Follow XenonStack on LinkedIn: XenonStack on LinkedIn for new guides on agentic AI, data and platform engineering.

Ready to put AI agents to work? Explore ElixirClaw, the agentic OS platform to build, orchestrate and govern AI agents.

Table of Contents

navdeep-singh-gill

Navdeep Singh Gill

Global CEO and Founder of XenonStack

LinkedIn profile

Navdeep Singh Gill is serving as Chief Executive Officer and Product Architect at XenonStack. He holds expertise in building SaaS Platform for Decentralised Big Data management and Governance, AI Marketplace for Operationalising and Scaling. His incredible experience in AI Technologies and Big Data Engineering thrills him to write about different use cases and its approach to solutions.

Get the latest articles in your inbox

Subscribe Now

Interested in solving your challenges with the XenonStack team?
Get Started

Tell us about you and your priorities so we can prepare the right conversation. It takes about 2 minutes.

Please Select your Industry
Banking
Fintech
Payment Providers
Wealth Management
Discrete Manufacturing
Semiconductor
Machinery Manufacturing / Automation
Appliances / Electrical / Electronics
Elevator Manufacturing
Defense & Space Manufacturing
Computers & Electronics / Industrial Machinery
Motor Vehicle Manufacturing
Food and Beverages
Distillery & Wines
Beverages
Shipping
Logistics
Mobility (EV / Public Transport)
Energy & Utilities
Hospitality
Digital Gaming Platforms
SportsTech with AI
Public Safety - Explosives
Public Safety - Firefighting
Public Safety - Surveillance
Public Safety - Others
Media Platforms
City Operations
Airlines & Aviation
Defense Warfare & Drones
Robotics Engineering
Drones Manufacturing
AI Labs for Colleges
AI MSP / Quantum / AGI Institutes
Retail Apparel and Fashion

Continue