Pip安装不与jenkins一起工作?

时间:2018-05-30 13:46:28

标签: python docker jenkins pip

这是我的Jenkins文件。

mapData

当我在Jenkins中运行它时,我得到了以下

pipeline {
    agent none
    stages {
        stage('Build') {
            agent {
                docker {
                    image 'python:3-alpine'
                }
            }
            steps {
                sh 'pip install --user -r requirements.txt'
                sh 'python WebChecker.py'
            }
            post {
                always {
                    junit 'output.xml'
                }
            }
        }
    }
}

然后我做sudo pip install ....

我收到以下错误:

[urltester] Running shell script

+ pip install --user -r requirements.txt

The directory '/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

The directory '/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

Collecting beautifulsoup4==4.6.0 (from -r requirements.txt (line 1))

  Downloading https://files.pythonhosted.org/packages/9e/d4/10f46e5cfac773e22707237bfcd51bbffeaf0a576b0a847ec7ab15bd7ace/beautifulsoup4-4.6.0-py3-none-any.whl (86kB)

Collecting requests==2.18.4 (from -r requirements.txt (line 2))

  Downloading https://files.pythonhosted.org/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl (88kB)

Collecting junit-xml==1.8 (from -r requirements.txt (line 3))

  Downloading https://files.pythonhosted.org/packages/a6/2a/f8d5aab80bb31fcc789d0f2b34b49f08bd6121cd8798d2e37f416df2b9f8/junit-xml-1.8.tar.gz

Collecting urllib3<1.23,>=1.21.1 (from requests==2.18.4->-r requirements.txt (line 2))

  Downloading https://files.pythonhosted.org/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)

Collecting idna<2.7,>=2.5 (from requests==2.18.4->-r requirements.txt (line 2))

  Downloading https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)

Collecting certifi>=2017.4.17 (from requests==2.18.4->-r requirements.txt (line 2))

  Downloading https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl (150kB)

Collecting chardet<3.1.0,>=3.0.2 (from requests==2.18.4->-r requirements.txt (line 2))

  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)

Collecting six (from junit-xml==1.8->-r requirements.txt (line 3))

  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl

Installing collected packages: beautifulsoup4, urllib3, idna, certifi, chardet, requests, six, junit-xml

Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/.local'

Check the permissions.



script returned exit code 1

然后我删除了sudo并尝试使用虚拟环境:

[urltester] Running shell script

+ sudo python -m pip install --user -r requirements.txt

/Users/me/.jenkins/workspace/urltester@tmp/durable-e36d9731/script.sh: line 1: sudo: not found

script returned exit code 127

但是当我尝试sudo时,我得到了同样的东西,但这次没有找到virtualenv。

我的最终目标是能够运行我的python脚本。这个python脚本将在同一目录中生成一个xml文件。然后Jenkins应该读取这个xml文件。我尝试过使用Docker,但没有达到目的。那我该怎么办?

4 个答案:

答案 0 :(得分:7)

tftd写道,将HOME更改为可写目录,例如:

MethodError: gradient(::Array{Int64,1}, ::Array{Int64,1}) is ambiguous. Candidates:
  gradient(f, x::Union{Array{T,1}, T}) where T<:Number in Calculus at /Applications/JuliaPro-0.6.4.1.app/Contents/Resources/pkgs-0.6.4.1/v0.6/Calculus/src/derivative.jl:17
  gradient(F::AbstractArray{T,1} where T, h::Array{T,1} where T) in Base.LinAlg at linalg/dense.jl:186
Possible fix, define
  gradient(::AbstractArray{T,1} where T, ::Array{T<:Number,1})

Stacktrace:
 [1] gradient(::Array{Int64,1}) at ./linalg/generic.jl:282

答案 1 :(得分:0)

您需要将virtualenv添加到PATH变量。

如果您使用pip install virtualenv进行安装,则会在pythonX.X/Lib/site-packages/

Sudo也应添加到PATH变量中。

第一个代码段中的错误是因为您没有权限写入'/.local'。尝试将其作为管理员

运行

答案 2 :(得分:0)

尝试像这样添加参数 -u root:root

withDockerContainer(image: 'python:3.6', args:'-u root:root'){
        sh """
            pip install --user -r requirements.txt
            python WebChecker.py
        """
    }

答案 3 :(得分:0)

我现在遇到这个问题。 OP的原始文章中未显示的是Jenkins用于运行Docker的命令。原来是:

docker run -t -d -u XXX:YYY -w / var / lib / jenkins / workspace /

其中XXX是运行jenkins的用户的UID,而YYY是组ID。在python容器中,无法识别此UID,因此没有主目录。当'pip install --user'尝试运行时,由于没有主目录,因此默认为'/',这是不可写的。

我认为这是詹金斯的谬论(请参阅https://issues.jenkins-ci.org/browse/JENKINS-47026)。那里的其他CICD服务似乎都可以解决这个问题。

中山洋一的答案对我有用,我对此表示反对。