让python3在jenkins中工作

时间:2017-08-31 16:45:30

标签: python jenkins

我无法让python3在jenkins中工作。 Jenkins目前在docker容器中运行,我使用pipeline脚本来促进CI / CD

这是我的Jenkinsfile for 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不被jenkins认可,因为它尚未安装。如何在我的jenkins文件夹中安装python3?我尝试在这里进行更改 - 但出于某种原因 - 这似乎不起作用(使用shiningpanda插件)

enter image description here

python2.7确实存在于/usr/bin/python中,但这似乎是詹金斯无法识别的

1 个答案:

答案 0 :(得分:0)

TL&DR:

默认情况下,Jenkins docker映像不包含python。因此,有必要在基础映像上安装python。请注意,您还可以在完全由python安装的docker-image中运行测试。

说明

一种方法是修改Jenkins docker-image。我使用Jenkins-lts构建,因为它通常较小。然后利用apk软件包管理器和

第1步:使用以下内容创建Docker文件

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