Using Python in Jenkins Pipelines

Loading

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

  1. Download and install Python from python.org.
  2. Add Python to the system PATH.
  3. Verify installation: cmdCopyEditpython --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

  1. Open Jenkins Dashboard → New Item → Select Freestyle Project.
  2. Under Build Steps, select Execute Shell.
  3. Add the following command: shCopyEditpython3 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

  1. Install the JUnit plugin in Jenkins.
  2. Modify the Jenkinsfile: post { always { junit 'report.xml' } }

Leave a Reply

Your email address will not be published. Required fields are marked *