3

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

Jayaram
  • 6,276
  • 12
  • 42
  • 78
  • Have you tried installing `python3` and the home or executable being at `/usr/bin/python3`? Anyways, I think you probably need `python3` to be installed in your Jenkins machine (your container image) – otorrillas Aug 31 '17 at 16:48
  • how does one do that? i tried going into the docker container and manually installing the python3 executable but i get the below error - `E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied) E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?` - but `sudo` doesnt work in a docker container :( – Jayaram Aug 31 '17 at 16:59
  • 1
    You should do it at image build time (e.g. your Docker file) – otorrillas Sep 01 '17 at 09:28
  • According to https://issues.jenkins-ci.org/browse/JENKINS-29007 it looks like Jenkins currently does not support python3?.. – MarSoft Oct 28 '17 at 14:33
  • @Kannaj did you manage to get this working? I'm in the same situation as you.. Jenkins spins up a container for each job but I don't know how to "upgrade" python to version 3 or "install" it... It only have version 2 now.. Hoping you have an answer as this is an old post... Thanks! – superloopnetworks Mar 04 '21 at 21:35

2 Answers2

5

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
FlyingV
  • 2,207
  • 19
  • 18
  • 1
    Attention to anyone: you do need `FROM jenkins/jenkins:lts-alpine`, not `FROM jenkins/jenkins`, or i got this error: `/bin/sh: 1: apk: not found`. Also, neither `pip`, nor `pip3` were found, so [i changed it to bootstrapping pip](https://stackoverflow.com/a/69253251/4575793) in this addition to [this answer](https://stackoverflow.com/a/63062550/4575793). I also ran into issues with setuptools. Does `postgresql` relate to the answer? – Cadoiz Sep 20 '21 at 11:02
  • 1
    Yes @cadoiz: That is exactly correct, and why the original answer included the right FROM :) – FlyingV Sep 23 '21 at 19:00
0

I also modified my Dockerfile like FlyingV suggested, but ran into some issues. Using curl to bootstrap pip works like this:

FROM jenkins/jenkins:lts-alpine
        #You need jenkins:lts-alpine instead of jenkins for apk among others
USER root

#Use apk to add python3 and then start bootstrapping pip
RUN apk add python3 \
        && curl -O https://bootstrap.pypa.io/get-pip.py \
        && python3 get-pip.py
        #I needed python&pip for ansible, which itself needs some more stuff.

#To have a clean environment with the typical aliases
RUN 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 #gives: /usr/glibc-compat/sbin/ldconfig: /usr/glibc-compat/lib/ld-linux-x86-64.so.2 is not a symbolic link
RUN apk add build-base #gives: /usr/glibc-compat/sbin/ldconfig: /usr/glibc-compat/lib/ld-linux-x86-64.so.2 is not a symbolic link
RUN apk add python3-dev #gives: /usr/glibc-compat/sbin/ldconfig: /usr/glibc-compat/lib/ld-linux-x86-64.so.2 is not a symbolic link

#change back to user jenkins
USER  jenkins

Here two links to the other dependencies taken into consideration for my specific case: the first one, the second, probably more generally applicable one and gcc.

Cadoiz
  • 1,446
  • 21
  • 31