Automate your Python project deployments with Jenkins – a visual guide to setting up CI/CD.
Let’s say you are writing a Python project. You write some code, save it, and then test it to make sure it works. But doing this every time can be boring and slow.
What if a robot could do this for you?
That’s what CI/CD does.
CI/CD is short for:
Don’t worry about the big words. Here’s what they really mean:
So, instead of doing everything by hand, a computer can do it for you!
Jenkins is a free tool that helps you with CI/CD. It works like a robot assistant. When you change your code, Jenkins can:
You just set it up once, and Jenkins keeps helping you every time.
In this blog post, I will teach you how to set up Jenkins for your Python project. You don’t need to be an expert. We’ll do it step by step.
Before we start building a Jenkins CI/CD pipeline, let’s make sure you have everything ready. Here’s what you need before creating a Jenkins CI/CD pipeline for Python:
1. Python Environment Installed
You need Python on your computer. If you’re not sure, open a terminal and type:
python --version
If it shows a version like Python 3.10.0, you’re good. If not, you can download Python here.
2. Git and GitHub (or any Git Repository)
Git helps you track your code. GitHub is where your code can live online.
You should know a few basic Git commands like:
git init
git add .
git commit -m "My message"
git push
3. Jenkins Installed (Locally or on a Server)
Jenkins is the tool we’ll use to build the CI/CD pipeline.
You can:
You can follow the official guide at jenkins.io.
4. Basic Knowledge of Python Scripting and Git
You don’t need to be a pro, but you should know:
Now that you know what you need, let’s set up Jenkins. Jenkins is the tool that will help us build the CI/CD pipeline for your Python project.
You don’t need to worry — we’ll keep things easy!
You can install Jenkins on any system you’re using. Here are simple guides for each:
brew install jenkins-lts
Once Jenkins is installed, open your browser and go to:
http://localhost:8080
You’ll see the Jenkins login or setup page.
After logging in, you’ll see the Jenkins dashboard.
Here’s what you’ll find:
You’ll spend most of your time creating jobs and checking build results here.
To work with Python and Git, you’ll need a few plugins.
Here’s how to install them:
Jenkinsfile.Once installed, restart Jenkins if asked.
That’s it!
You’ve installed Jenkins and added the tools you need for working with Python.
Next, we’ll connect Jenkins to your Python project and set up your first pipeline.
Let’s now prepare your Python project for Jenkins. We’ll create a sample project, add a test, and connect it to GitHub.
This way, Jenkins can watch your code, test it, and build it when changes are made.
Create a new folder for your project. Inside it, add these 3 files:
main.pydef add(a, b):
return a + b
test_sample.pyWe’ll use Python’s built-in testing tool called unittest.
import unittest
from main import add
class TestAddFunction(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
if __name__ == "__main__":
unittest.main()
You can also use pytest if you prefer. Just change the test style and run
pytestinstead.
requirements.txtThis tells Jenkins what Python packages are needed.
unittest
(If you’re using pytest, write pytest instead.)
Now let’s put your project on GitHub and connect it to Jenkins.
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/your-username/your-repo.git
git push -u origin master
2. Open Jenkins
Go to your Jenkins dashboard:http://localhost:8080
3. Create a New Item
4. Connect to GitHub
Webhooks tell Jenkins when something changes in your repo.
http://your-jenkins-url/github-webhook/
4. Set Content type to application/json
5. Choose “Just the push event”
6. Click Add webhook
Now every time you push code, GitHub tells Jenkins to build it!
If your repo is private, Jenkins needs permission to access it.
Now your Python project is ready. Jenkins can:
Next, we’ll set up your first Jenkins pipeline using a Jenkinsfile.
Let’s do it step by step.
Now that your Python project is ready and connected to Jenkins, we’re all set to move on to the next step — writing the Jenkins pipeline using a Jenkinsfile.
This will tell Jenkins exactly what to do each time you push your code (like install packages, run tests, etc.).
This means every time you push new code, Jenkins knows what to do — without you clicking buttons.
pipeline {
agent any
stages {
stage('Clone Repo') {
steps {
git 'https://github.com/username/repo-name.git'
}
}
stage('Install Dependencies') {
steps {
sh 'pip install -r requirements.txt'
}
}
stage('Run Tests') {
steps {
sh 'pytest'
}
}
stage('Build & Deploy') {
steps {
echo 'Deploying application...'
// Your deployment logic here
}
}
}
}
1. pipeline { ... }
This is like the start and end of your recipe. Everything you want Jenkins to do goes inside these curly braces {}.
2. agent any
This tells Jenkins where to run your tasks.
“Any” means Jenkins can use any available computer or server to do the job.
3. stages { ... }
This is where the main steps of your recipe live. Each stage is a part of the whole process.
You have four stages here:
4. Stage 1: Clone Repo
stage('Clone Repo') {
steps {
git 'https://github.com/username/repo-name.git'
}
}
This step tells Jenkins:
“Go to this GitHub address and get the latest copy of the code.”
It’s like getting your ingredients ready before cooking.
5. Stage 2: Install Dependencies
stage('Install Dependencies') {
steps {
sh 'pip install -r requirements.txt'
}
}
Here Jenkins runs a command (sh means “run shell command”) to install the Python packages your project needs.requirements.txt lists these packages.
So Jenkins makes sure the computer has all the right tools before testing.
6. Stage 3: Run Tests
stage('Run Tests') {
steps {
sh 'pytest'
}
}
This step runs your tests using pytest.
Tests check if your code works correctly.
If a test fails, Jenkins will stop here and tell you something is wrong.
7. Stage 4: Build & Deploy
stage('Build & Deploy') {
steps {
echo 'Deploying application...'
// Your deployment logic here
}
}
This is where Jenkins gets your code ready to share or use in the real world.
Right now, it just prints a message.
You can add more steps here to send your app to a server or cloud.
Once your Jenkins pipeline (Jenkinsfile) is ready, you want to run it automatically and watch how it works.
Jenkins needs to know when to start building your project. There are two common ways:
How to set it up:
http://your-jenkins-url/github-webhook/
application/json and choose “Just the push event.”How to set it up:
H/5 * * * * means every 5 minutes).If your build fails, don’t panic! Here are some common issues:
requirements.txt has all needed packages.With these tips, you can easily run, watch, and fix your Python CI/CD pipeline in Jenkins.
When Jenkins runs your Python code, it’s best to keep the environment clean.
Using virtual environments like virtualenv or venv helps Jenkins install packages only for your project, without messing with other projects or system-wide Python.
How to use it in Jenkinsfile:
Before installing packages, add these steps:
stage('Setup Virtualenv') {
steps {
sh 'python3 -m venv venv'
sh '. venv/bin/activate'
sh 'pip install -r requirements.txt'
}
}
This way, Jenkins works inside the virtual environment, making your pipeline safer and more reliable.
It’s important to check how much of your code is tested.
Tools like coverage.py help you see which parts of your Python code are covered by tests.
Steps to add coverage:
pip install coveragecoverage run -m pytestcoverage report or coverage htmlIn Jenkins, you can publish these reports for easy viewing.
After tests pass, you can automatically deploy your app so users get the latest version without delay.
You can deploy apps built with popular Python frameworks like Flask, Django, or FastAPI.
Here’s a simple example of deployment to Heroku in your Jenkinsfile:
stage('Deploy to Heroku') {
steps {
sh 'heroku login'
sh 'git push heroku main'
}
}
Using these tips, you can make your Jenkins CI/CD pipeline more powerful, clean, and fully automatic — from testing to deployment.
1. Keep Pipeline Code Versioned
Always keep your Jenkinsfile and related scripts in your project’s Git repository.
This way, changes to your pipeline are tracked like your code, and you can easily update or fix your build process.
2. Make Builds Fast and Repeatable
Keep your builds quick by:
3. Isolate Test Environments
Run your tests in a clean, separate environment so they don’t interfere with each other.
Using tools like virtualenv, venv, or Docker containers helps keep tests reliable and repeatable.
4. Run Security Scans and Linters
Add steps to your pipeline to check code quality and security.
Linters (like flake8 or pylint) catch style and error issues.
Security tools (like bandit) help find vulnerabilities in your code.
Following these practices will make your Jenkins CI/CD pipeline more reliable, easier to maintain, and safer for your Python projects.
Setting up CI/CD for your Python projects using Jenkins may seem tricky at first, but step by step, it becomes much easier.
With Jenkins, you can automate testing, building, and deploying your Python apps — saving you time and avoiding mistakes.
Remember to prepare your environment, write a clear Jenkinsfile, and monitor your builds carefully.
Using virtual environments, running tests with coverage, and automating deployment will make your pipeline strong and reliable.
Follow the best practices we talked about to keep your projects clean, fast, and secure.
Start small, keep learning, and soon you’ll have a smooth CI/CD process that helps your Python projects grow with confidence!
Want to make sure you’ve grasped the key concepts of Python CI/CD with Jenkins? Take this quick quiz and find out!
You've learned how to set up a basic CI/CD pipeline for your Python project using Jenkins — now it's your turn to put that knowledge into practice.
requirements.txt within the Jenkins pipeline environment. stage('Install Dependencies') {
steps {
sh 'pip install -r requirements.txt'
}
} stage('Test') {
steps {
sh 'python -m pytest tests/'
}
} stage('Lint') {
steps {
sh 'flake8 .' // or 'pylint your_package_name'
}
} stage('Package') {
steps {
sh 'python setup.py sdist bdist_wheel'
}
} Take it a step further by adding:
stage('Test') {
steps {
sh 'pytest tests/'
}
} Just make sure you've installed pytest using pip install pytest. After debugging production systems that process millions of records daily and optimizing research pipelines that…
The landscape of Business Intelligence (BI) is undergoing a fundamental transformation, moving beyond its historical…
The convergence of artificial intelligence and robotics marks a turning point in human history. Machines…
The journey from simple perceptrons to systems that generate images and write code took 70…
In 1973, the British government asked physicist James Lighthill to review progress in artificial intelligence…
Expert systems came before neural networks. They worked by storing knowledge from human experts as…
This website uses cookies.