0Pricing
Docker & DevOps Fundamentals · 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.

Jenkins Pipeline as Code with Jenkinsfile is a free Docker & DevOps Fundamentals lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & DevOps Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “Jenkins Pipeline as Code with Jenkinsfile” is free to read here on the web, and the Docker & DevOps Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & DevOps Fundamentals course, upgrade to CoddyKit PRO.

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 Docker & DevOps Fundamentals 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 Docker & DevOps Fundamentals?

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

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 Docker & DevOps Fundamentals lesson?

Yes. Every Docker & DevOps Fundamentals 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 Docker & DevOps Fundamentals