Jenkinsfile: Your CI/CD Pipeline as Code
A Jenkinsfile is a text file that defines your entire CI/CD pipeline as code, living in your source control. It automates build, test, and deploy steps. The main footgun is confusing its two syntaxes: Declarative is simpler, while Scripted offers more power.
WHY IT EXISTS Before Jenkinsfile, pipelines were often configured through the Jenkins web UI. This made them difficult to version, review, or share across projects. Jenkinsfile was created to treat pipeline configuration as code, solving these problems by storing the definition in a text file within the project's source control.
THE MENTAL MODEL Think of a Jenkinsfile as a recipe for your software delivery process. Just like a recipe lists ingredients and steps (mix, bake, cool), a Jenkinsfile lists stages (Build, Test, Deploy) and the steps within them. By putting this recipe in your source code repository, everyone on the team can see, review, and improve it.
HOW IT WORKS Jenkins reads a file named Jenkinsfile from the root of your source code repository. This file is written in a Groovy-based Domain-Specific Language (DSL) and comes in two syntaxes: Declarative and Scripted. Declarative is simpler and more structured, using blocks like pipeline, agent, stages, and steps. The agent directive tells Jenkins where to run the job. The stages block contains a sequence of stage blocks, like 'Build' or 'Test'. Each stage contains steps which are the actual commands. Scripted Pipeline is a more free-form and powerful syntax for advanced use cases.
WHEN TO USE IT Use a Jenkinsfile for any project managed by Jenkins. It's the modern standard for defining CI/CD pipelines, enabling the "Pipeline as Code" methodology. This provides code review for your automation, an audit trail, and a single source of truth that can be viewed and edited by the entire team.
WHEN NOT TO USE IT For extremely simple, one-off tasks that don't need versioning or collaboration, configuring a job through the Jenkins UI might seem quicker. However, for any real project, a Jenkinsfile is best practice. Relying on the UI is a form of "click-ops" that doesn't scale, is hard to maintain, and lacks a proper audit trail.
ONE CANONICAL EXAMPLE A basic Declarative Jenkinsfile defines a three-stage pipeline. The entire definition is wrapped in a pipeline block. The agent any directive tells Jenkins to run on any available agent. This is followed by a stages block which contains three distinct stage sections, each named for its purpose: 'Build', 'Test', and 'Deploy'. Inside each stage, a steps block contains the commands to execute, such as echo 'Building..' for the build stage. This structure clearly separates the phases of the CI/CD process.
Read the original → jenkins.io
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.