Jenkins下无法使用pip install命令?

20

那是我的Jenkinsfile。

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'
                }
            }
        }
    }
}

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

[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 pip install ...

然后我收到以下错误信息:

[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并尝试使用虚拟环境:

我取消了sudo并尝试使用虚拟环境:

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

但是当我尝试使用sudo时,我得到了与之前相同的结果,但这次virtualenv未被找到。

我的最终目标是能够运行我的Python脚本。这个Python脚本会在其相同的目录中生成一个XML文件。然后Jenkins应该读取这个XML文件。我尝试使用Docker,但没有取得很大进展。那么我该怎么办?


1
通常情况下,由Jenkins启动的Docker容器以“普通”用户身份运行(即uid 1000gid 1000)。从日志中可以看出,pip试图在/.local中安装依赖项,但由于权限问题无法执行。请检查$HOME是否指向$WORKSPACE目录。如果是这个问题,您可以使用withEnv手动设置它。另一种选择是在/.local上挂载一个具有rwx权限的volume,以便任何用户都可以在那里写入,这应该解决您的问题。 - tftd
我该如何检查 $HOME 指向哪里?我在 macOS 上。 - Rahul
当然不会起作用 - 你所做的唯一更改是“echo” $HOME。那么它输出什么? :) - tftd
它只是输出一个 /。 - Rahul
让我们在聊天中继续这个讨论 - Rahul
显示剩余2条评论
4个回答

28

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

pipeline {
    agent none
    stages {
        stage('Build') {
            agent {
                docker {
                    image 'python:3-alpine'
                }
            }
            steps {
                withEnv(["HOME=${env.WORKSPACE}"]) {
                    sh 'pip install --user -r requirements.txt'
                    sh 'python WebChecker.py'
                }
            }
            post {
                always {
                    junit 'output.xml'
                }
            }
        }
    }
}

3
@Rahul 这是一个可行的答案,请考虑接受它。 - Federico Bonelli
1
如果您想要一行解决方案,您还可以执行 pip install --target ${env.WORKSPACE} -r requirements.txt。这样,您就可以从步骤中删除显式的 withEnv - nikoo28

1

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

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

Sudo也应该添加到PATH变量中。


第一个代码片段中的错误是因为您没有写入'/.local'的权限。尝试以管理员身份运行它。

2
据我所知,sudo 不是“默认”alpine docker镜像的一部分。 您需要通过扩展docker镜像并添加 RUN apk install --no-cache sudo 来明确安装它,否则您将收到 command not found 。这不是因为它不在您的PATH中,而是因为它根本没有安装。 在大多数情况下,它将安装在这些路径之一 /usr/bin/ ; /bin ; /sbin ,这些路径几乎在所有Linux发行版上都设置了PATH - tftd
我该如何以管理员身份运行它?我该如何将其设置为路径变量?我正在使用macOS。 - Rahul
@Rahul,请查看此链接 https://www.architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/ - Agile_Eagle

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'尝试运行时,由于没有主目录,默认情况下为'/',不能写入。

我认为这是Jenkins的一个谬误(请参见https://issues.jenkins-ci.org/browse/JENKINS-47026)。其他CICD服务似乎处理得非常好。

Yoichi Nakayama的答案对我有用,我会点赞它。


我相信这就是使用Podman而不是Docker来运行Jenkins时出现问题的具体细节。 - Max Cascone

0
尝试添加参数-u root:root,像这样:
withDockerContainer(image: 'python:3.6', args:'-u root:root'){
        sh """
            pip install --user -r requirements.txt
            python WebChecker.py
        """
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接