Jenkins is a powerful automation tool used for CI/CD (Continuous Integration and Continuous Deployment). Python can be integrated into Jenkins pipelines for automation, testing, deployment, and DevOps workflows.
This guide covers:
Setting up Jenkins for Python
Writing a Jenkinsfile with Python
Running Python scripts in a pipeline
Best practices for Python in Jenkins
1. Installing Python in Jenkins
To use Python in Jenkins, ensure:
🔹 Python is installed on the Jenkins server (python3 --version
)
🔹 The Python plugin is installed in Jenkins (optional but helpful)
🔹 Jenkins has permission to execute Python scripts
1.1 Installing Python on Linux
sudo apt update && sudo apt install python3 python3-pip -y
Check Python version:
python3 --version
1.2 Installing Python on Windows
- Download and install Python from python.org.
- Add Python to the system PATH.
- Verify installation: cmdCopyEdit
python --version
2. Creating a Jenkins Pipeline for Python
Jenkins uses Declarative Pipelines to define CI/CD workflows. You can create a Jenkinsfile
to automate Python scripts.
2.1 Sample Python Jenkinsfile
This pipeline runs a Python script (script.py
) in different stages.
pipeline {
agent any
stages {
stage('Checkout Code') {
steps {
git 'https://github.com/your-repo/python-project.git'
}
}
stage('Setup Python Environment') {
steps {
sh 'python3 -m venv venv'
sh 'source venv/bin/activate'
}
}
stage('Install Dependencies') {
steps {
sh 'pip install -r requirements.txt'
}
}
stage('Run Python Script') {
steps {
sh 'python3 script.py'
}
}
stage('Run Tests') {
steps {
sh 'pytest tests/'
}
}
}
}
3. Running Python Scripts in Jenkins
3.1 Simple Python Execution in a Freestyle Job
- Open Jenkins Dashboard → New Item → Select Freestyle Project.
- Under Build Steps, select Execute Shell.
- Add the following command: shCopyEdit
python3 script.py
3.2 Running a Python Virtual Environment in Jenkins
To run a virtual environment in a Jenkins job:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python script.py
4. Using Jenkins Pipeline with Docker for Python
If your project runs inside a Docker container, use the Docker agent in Jenkins.
pipeline {
agent {
docker {
image 'python:3.10'
}
}
stages {
stage('Run Python Script') {
steps {
sh 'python3 script.py'
}
}
}
}
5. Deploying Python Applications Using Jenkins
After testing, Jenkins can deploy your Python app to a server.
5.1 Deploying a Python Flask App
stage('Deploy to Server') {
steps {
sh 'scp -r * user@server:/var/www/python-app'
sh 'ssh user@server "systemctl restart gunicorn"'
}
}
6. Running Python Unit Tests in Jenkins
Jenkins can execute pytest for unit testing.
6.1 Running pytest
in a Pipeline
stage('Run Tests') {
steps {
sh 'pytest --junitxml=report.xml'
}
}
6.2 Displaying Test Results in Jenkins
- Install the JUnit plugin in Jenkins.
- Modify the Jenkinsfile:
post { always { junit 'report.xml' } }