DEV Community

Cover image for Jenkins: Creating a Pipeline for Python projects
Miracle Olabode ®️💭
Miracle Olabode ®️💭

Posted on

Jenkins: Creating a Pipeline for Python projects

I recently attempted to set up a pipeline using Jenkins for my Python projects. The process was not straightforward, but I was able to figure it out.

I'll try to describe how I did it in this article so that anyone starting off or working on a project like it can follow the guide.

There are few prerequisite needed to get started with setting up the pipeline

  • Understanding Docker and Jenkins
  • Install Docker
  • Jenkins(this article will cover the setting up of the virtual environment)

To check if you have docker installed on your computer, open up your terminal and execute the command "docker" If docker is installed, you should see something like to this. If not, download and install docker, now let's do something more fun, lol.

Docker installed

Next we would installed jenkins on docker with the following command


 run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk11

Enter fullscreen mode Exit fullscreen mode

the command automatically tells docker to install a jenkins image and bind port 8080 and port 50000
Kindly take note on the password that would appear and probably note it down somewhere as you would be needing it

authentication

Now lets open up the Jenkins image we just created with the following steps

  • Open up a new browser
  • Go to localhost:8080
  • You should be prompted for a password(enter the password you noted down earlier)

password

  • install suggested plugins and follow the prompt

You should see the Dashboard Page

Jenkins Dashboard

Now, Lets Create a Pipeline

Click on New Item on the Sidebar, give the Item a name and select Pipeline then click on OK.

pipeline build dashboard

Provide a description that is appropriate for your projects, then scroll down to the script section.

enter these codes here



pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: 'main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/DevOlabodeM/pytest-intro-vs-M']]])
            }
        }
        stage('Build') {
            steps {
                git branch: 'main', url: 'https://github.com/DevOlabodeM/pytest-intro-vs-M'
                sh 'python3 ops.py'
            }
        }
        stage('Test') {
            steps {
                sh 'python3 -m pytest'
            }
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

A walk through on what the code does

  • Stages: Here, we've outlined every step required for our pipeline.

  • Stage: Here, there are three stages (Checkout, Build and Test)

-CHECKOUT
We created a pipeline script to checkout of our GitHub repo during the checkout stage and placed it inside the stages braces. "https://github.com/DevOlabodeM/pytest-intro-vs-M

-BUILD
Inside the braces{}, in the build stage, we generated a pipline shell script for our python file to run here you would have to install python3 on your container so, run 'docker ps' to see what containers are running - copy the container ID for Jenkins, such as '8f7c957e19fd' afterwards - To open an interactive terminal within the Docker Container, use the command 'docker exec -it -u 0 8f7c957e19fd /bin/bash' (user 0)
Run the following commands to install Python3 and pip within the Docker container



apt-get update
apt-get install python3
apt-get install python3-pip


Enter fullscreen mode Exit fullscreen mode

-TEST
In the test stage, we generated a pipeline shell script to test our python file, then 'pip install pytest' will install the pytest package, which will run the unit/integration tests during your test stage in the pipeline.
Script session
After this stage, click save

dashboard

Viola! We've built our Pipeline; now it's time to put it into action. To do so, go to the sidebar and click Build Now, and the Pipeline will start.
If Pipeline was built properly, you should see this
build dashboard

If not, please inspect the console to determine what is wrong and correct it.

Thank you for sticking around with me; I hope you found it useful.
Please provide feedback and reactions.

Top comments (0)