top of page

Jenkins Scripted Pipeline with Python: A Complete Beginner's CI/CD Guide (VS Code + GitHub + Docker)

Updated: 3 days ago


Introduction: Why Learn Jenkins Scripted Pipelines?

If you're stepping into the world of DevOps and Continuous Integration/Continuous Deployment (CI/CD), Jenkins is one of the first tools you'll encounter — and for good reason. It's open-source, highly extensible, and forms the backbone of automation pipelines at companies of every size.


In this tutorial, we'll build a complete, real-world CI/CD workflow from the ground up. You'll learn how to:


  • Write and organize a simple Python project in VS Code

  • Push your code to a GitHub repository

  • Create a Jenkins Scripted Pipeline using a Jenkinsfile

  • Run that pipeline inside a Dockerized Jenkins setup

  • Automatically execute a Python script as part of your CI pipeline


By the end, you'll have a working pipeline that checks out code, builds it, runs a Python script, and reports success — the exact same pattern used in production-grade automation systems, just scaled down for learning.


What Is a Jenkins Scripted Pipeline?

Jenkins supports two pipeline syntaxes: Declarative and Scripted. The Scripted Pipeline is written in Groovy and offers more flexibility and control, making it ideal for understanding how Jenkins actually processes pipeline logic under the hood — stage by stage, step by step.

This makes it a great starting point for anyone who wants to understand CI/CD pipelines at a deeper, more hands-on level before moving to the more structured Declarative syntax.


Prerequisites

Before starting, make sure you have the following installed and running:

  • Docker Desktop

  • Jenkins running in Docker

  • Visual Studio Code

  • Git

  • A GitHub account

If you already have these set up, you're ready to go.


Step 1: Create Your Project Folder in VS Code

Open VS Code and create a new folder named:

SimpleScriptedPipeline

Inside this folder, create three files:

SimpleScriptedPipeline
│
├── hello.py
├── Jenkinsfile
└── README.md

This clean structure keeps your source code, documentation, and pipeline definition organized — a best practice for any real-world project.


Step 2: Write the Python Program

Open hello.py and add the following line:

python

print("Hello from Jenkins Scripted Pipeline!")

Save the file. This simple script will act as the "application" our pipeline builds and runs — in a real project, this could be a test suite, a data processing script, or a deployment task.

Step 3: Add a README File

Documentation matters, even for small projects. Add a short description inside README.md:

This is my first Jenkins Scripted Pipeline project.

Step 4: Create the Jenkinsfile

This is the most important file in the entire project — it defines how Jenkins builds, tests, and runs your code.

Create a file named exactly:

Jenkinsfile

No file extension. Jenkins specifically looks for this filename by default.

Add the following Scripted Pipeline code:

groovy

node {

    stage('Checkout') {
        echo "Checking out source code..."
    }

    stage('Build') {
        echo "Building Project"
    }

    stage('Run Python') {
        sh 'python3 hello.py'
    }

    stage('Finish') {
        echo "Pipeline Completed Successfully"
    }

}

A Quick Note on sh vs bat

If your Jenkins controller is running on Windows, you'd use:

groovy

bat 'python hello.py'

However, since Jenkins is running inside a Linux Docker container in this setup, sh is the correct choice:

groovy

sh 'python3 hello.py'
Important: bat only works on Windows agents. Using it inside a Linux container will cause your build to fail. Always match your shell step to your agent's operating system.

Step 5: Create a GitHub Repository

Head over to GitHub and create a new repository, for example:

simple-scripted-pipeline

Do not initialize it with a README if you've already created one locally — this avoids merge conflicts when you push.

Step 6: Initialize Git Locally

Open the integrated terminal in VS Code and run:

bash

git init

Step 7: Stage Your Files

bash

git add .

Step 8: Commit Your Changes

bash

git commit -m "Initial Commit"

Step 9: Connect to Your GitHub Repository

Replace the URL below with your own repository's URL:

bash

Step 10: Push Your Code to GitHub

bash

git branch -M main
git push -u origin main

Your project is now live on GitHub and ready to be connected to Jenkins.

Step 11: Open Jenkins

Navigate to your local Jenkins instance:

Log in with your Jenkins credentials.

Step 12: Create a New Pipeline Item

  1. Click New Item

  2. Enter the name: ScriptedDemo

  3. Select Pipeline as the project type

  4. Click OK

Step 13: Configure the Pipeline Source

Scroll down to the Pipeline section on the configuration page.

Select:

Pipeline script from SCM

This tells Jenkins to pull the pipeline definition directly from your GitHub repository instead of writing it manually in the Jenkins UI — a much more maintainable approach for real projects.

Step 14: Choose Your SCM

Select:

Git

Step 15: Add the Repository URL

Paste your GitHub repository URL:

Step 16: Specify the Branch

*/main

Step 17: Set the Script Path

Leave this as the default:

Jenkinsfile

Jenkins will automatically look for this file in the root of your repository.

Click Save.

Step 18: Trigger Your First Build

Click Build Now and watch Jenkins spring into action.

Step 19: Review the Console Output

Open the Console Output for your build. You should see something like this:

Started by user admin
Checking out source code...
Building Project
Hello from Jenkins Scripted Pipeline!
Pipeline Completed Successfully
Finished: SUCCESS

Congratulations — your first Jenkins Scripted Pipeline is up and running!

Understanding Each Pipeline Stage

Breaking down what's actually happening behind the scenes helps solidify your understanding of pipeline mechanics.

Stage

Purpose

Checkout

Downloads (clones) the project into the Jenkins workspace from your GitHub repository.

Build

In a real project, this compiles or prepares your code. Here, it simply prints a message to illustrate the step.

Run Python

Executes the actual Python script (hello.py) using the sh step.

Finish

Confirms the pipeline has completed all stages successfully.

What the Jenkins Workspace Looks Like

Once Jenkins clones your repository, it organizes the project inside its internal workspace directory like this:

workspace/
└── ScriptedDemo/
    ├── hello.py
    ├── README.md
    └── Jenkinsfile

This workspace is where all pipeline steps execute — every sh or bat command runs relative to this directory.

How Does Jenkins Know What to Execute?

This is a common point of confusion for beginners, so let's break it down clearly.

When you select "Pipeline script from SCM", Jenkins follows this exact sequence:

  1. Clones the Git repository you specified in the configuration.

  2. Looks for a file named Jenkinsfile in the location defined by the Script Path setting.

  3. Reads the Groovy code inside that file.

  4. Executes the pipeline stage by stage, running each step in sequence and reporting status back to the Jenkins dashboard.

This is what makes Jenkins so powerful for CI/CD: your pipeline definition lives alongside your code in version control, meaning every change to your build process is tracked, reviewed, and reproducible — a practice often called "Pipeline as Code."

Conclusion

You've now built a complete, functioning CI/CD workflow — from writing Python code in VS Code, to version-controlling it on GitHub, to automating its execution through a Jenkins Scripted Pipeline running in Docker. While this example is intentionally simple, the underlying structure — Checkout → Build → Test/Run → Finish — is the same pattern used in enterprise-grade deployment pipelines everywhere.

Next Steps to Level Up

  • Add a Test stage using pytest to validate your Python code automatically

  • Explore Declarative Pipelines for a more structured syntax

  • Set up GitHub Webhooks so Jenkins builds automatically on every push

  • Integrate Docker builds directly into your pipeline for containerized deployments

Mastering this foundational workflow sets you up perfectly for more advanced Jenkins concepts like parallel stages, shared libraries, and multi-branch pipelines.

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

Recommended Products For This Post
 
 
 

Recent Posts

See All

Comments


© 2026 by neoaitech.com

  • Linkedin
  • Facebook
  • Twitter
  • Instagram

B608, 11 K County, Pune, Maharashtra, India

bottom of page