DevOps Bootcamp · Lesson

Jenkins Pipeline as Code with Jenkinsfile

Define build, test, and deploy stages declaratively in a Jenkinsfile checked into your repo, so the pipeline itself is versioned alongside the application.

Lesson 4 of 413 steps

Jenkins Pipeline as Code with Jenkinsfile is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

Pipeline as Code

Instead of configuring jobs by hand in the Jenkins UI, describe the whole pipeline in a Jenkinsfile stored in your repository. The pipeline becomes code: reviewed, versioned, and portable.

Declarative vs Scripted

Jenkins offers two syntaxes. Declarative pipelines are structured and beginner-friendly; scripted pipelines are full Groovy. We focus on declarative.

The pipeline Block

Every declarative Jenkinsfile starts with a pipeline block containing an agent (where it runs) and stages.

pipeline {
  agent any
  stages {
    stage('Build') {
      steps { echo 'Building...' }
    }
  }
}

Defining Stages

Each stage groups related steps. Stages appear as columns in the Jenkins UI, giving a clear visual of pipeline progress.

stages {
  stage('Build') { steps { sh 'make build' } }
  stage('Test')  { steps { sh 'make test' } }
}

Running Shell Steps

The sh step runs a shell command on the agent. Use it to compile code, run tests, or build a Docker image.

steps {
  sh 'docker build -t myapp:${BUILD_NUMBER} .'
}

Environment Variables

An environment block defines variables for all steps. Jenkins also exposes built-ins like BUILD_NUMBER and GIT_COMMIT.

environment {
  REGISTRY = 'registry.example.com'
  IMAGE    = 'myapp'
}

Credentials Binding

Never hard-code secrets. Store them in Jenkins credentials and pull them in with the credentials() helper so they are masked in logs.

environment {
  REG_PASS = credentials('registry-password')
}

Post Actions

A post block runs after stages finish, branching on result. Perfect for notifications and cleanup.

post {
  success { echo 'Pipeline succeeded' }
  failure { echo 'Pipeline failed' }
  always  { sh 'docker image prune -f' }
}

Parallel Stages

Speed things up by running independent work in parallel - for example unit and integration tests at the same time.

stage('Tests') {
  parallel {
    stage('Unit') { steps { sh 'make unit' } }
    stage('Integration') { steps { sh 'make int' } }
  }
}

Conditional Stages

The when directive runs a stage only if a condition holds - for instance, deploy only on the main branch.

stage('Deploy') {
  when { branch 'main' }
  steps { sh './deploy.sh' }
}

Multibranch Pipelines

Point Jenkins at a repo and it discovers every branch with a Jenkinsfile, creating a pipeline per branch automatically. Feature branches get CI for free.

Quick Check

Where does a declarative pipeline definition live?

Recap

You learned to define pipelines as code:

  • pipeline { agent; stages } is the declarative skeleton
  • sh steps run commands; environment and credentials() manage config
  • post, parallel, and when add power
  • Multibranch pipelines give every branch CI automatically

The Jenkinsfile lives with your code and evolves with it.

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Jenkins Pipeline as Code with Jenkinsfile” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Jenkins Pipeline as Code with Jenkinsfile”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “Jenkins Pipeline as Code with Jenkinsfile”?

Define build, test, and deploy stages declaratively in a Jenkinsfile checked into your repo, so the pipeline itself is versioned alongside the application. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Jenkins Pipeline as Code with Jenkinsfile” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Jenkins
  2. Building Docker Images with Jenkins
  3. Automated Deployment with Jenkins
  4. Jenkins Pipeline as Code with Jenkinsfile
← Back to DevOps Bootcamp