Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting python3 to work in jenkins

Tags:

python

jenkins

I'm having trouble getting python3 to work in jenkins. Jenkins is currently running in a docker container and i'm using pipeline scripts to facilitate CI/CD

This is my Jenkinsfile for a python repo

pipeline {
    agent any
    tools {
        nodejs 'nodejs'
        python3 'python3'
    }
    environment{

    }
    stages {
        stage('build'){

            steps{
                echo 'Preparing'

                sh 'python3 --version'
                sh 'pip3 install -U pytest'
                script{
                    // pull git tag and add to a variable to set the build info - {tag#build_no}
                    GIT_TAG = sh(script: "git describe --abbrev=0 --tags", returnStdout: true).trim()
                    sh 'echo ${GIT_TAG}'
                    currentBuild.displayName = "${GIT_TAG}#${BUILD_NUMBER}"
                }
            }
        }

        stage('Checkout'){
            steps {
                echo 'Checking out code from repo'
                checkout scm
            }
        }

        stage('install'){
            steps{
                echo 'installing libraries'
                sh 'pip3 install -r requirements.txt'
            }
        }

        stage('test'){
            steps {
                echo 'running tests'
                sh 'pytest'
            }
            post{
                success{
                    bitbucketStatusNotify(buildState: 'SUCCESSFUL')
                    office365ConnectorSend message: "The build was successfull", status: "Success", webhookUrl: "${env.HOOK}"
                }
                failure{
                    bitbucketStatusNotify(buildState: 'FAILED')
                    office365ConnectorSend message: "The build has failed", status: "Failure", webhookUrl: "${env.HOOK}"
                }
            }
        }
    }
}

python3 isnt recognized by jenkins as it hasnt been installed yet. How do i get a python3 installation in my jenkins folder? I tried making the changes here - but for some reason - this doesnt seem to work (using the shiningpanda plugin)

enter image description here

python2.7 actually does exist in /usr/bin/python but this seem to be unrecognized by Jenkins

like image 713
Kannaj Avatar asked Oct 11 '25 19:10

Kannaj


1 Answers

TL&DR:

By default, the Jenkins docker image does NOT include python. It is therefore necessary to install python on the base image. It should be noted, you can also have the test run in a fully python-installed docker-image.

Explanations

One method is to modify the Jenkins docker-image. I use the Jenkins-lts build because it is typically smaller. Then leverage apk package manager and add python

Step1: Create a Docker File with the following

FROM jenkins/jenkins:lts-alpine
USER root
RUN apk add python3 && \
 python3 -m ensurepip && \
 pip3 install --upgrade pip setuptools && \
 if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
 if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
 rm -r /root/.cache
RUN pip install alpine==0.0.2
RUN apk add pkgconf
RUN apk add build-base
RUN apk add python3-dev
RUN apk add postgresql-dev
RUN apk add postgresql-client
like image 172
FlyingV Avatar answered Oct 14 '25 09:10

FlyingV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!