【问题标题】:Pip install not working with jenkins?Pip安装不适用于詹金斯?
【发布时间】:2018-05-30 13:46:27
【问题描述】:

那是我的 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 并尝试使用虚拟环境:

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,但并没有走得太远。那我该怎么办?

【问题讨论】:

  • 通常由Jenkins 启动的docker 容器以normal 用户(即uid 1000gid 1000)运行。从日志来看,pip 似乎正在尝试在 /.local 中安装依赖项,但由于权限问题而无法执行此操作。检查$HOME 是否指向$WORKSPACE 目录。也许您可以使用withEnv 并手动设置它,如果这是问题所在。您的另一个选择是mount volumerwx 权限/.local,这样任何用户都可以在那里写信,这应该可以解决您的问题。
  • 我如何检查 $HOME 指向的位置?我在 macOS 上
  • 添加 sh 'echo $HOME' 作为管道中的第一步,然后再次运行管道。我猜有一个env 变量pip / virtualenv 使用它来“查找”它试图写入的.local 内容的位置——但是由于某种原因,完整路径是/.local。所以也许$HOME 或另一个环境变量指向/ 而不是/home/jenkins/。 :)
  • 还是不行
  • 当然不行——你所做的唯一改变是“回显”$HOME。那么它输出什么? :)

标签: python docker jenkins pip


【解决方案1】:

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

【讨论】:

  • @Rahul 这是一个有效的答案,请考虑接受这个
  • 如果您想要一个单行解决方案,您也可以使用pip install --target ${env.WORKSPACE} -r requirements.txt。这样你就可以从你的步骤中删除显式的withEnv
【解决方案2】:

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

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

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


第一个代码 sn-p 中的错误是因为您没有写入'/.local' 的权限。尝试以 administrator

身份运行它

【讨论】:

  • sudo 据我所知,not 是“默认”高山 docker 镜像的一部分。您需要通过扩展 docker 映像并添加 RUN apk install --no-cache sudo 来显式安装它,否则您将获得 command not found。这并不是因为它不在您的PATH 中,而是因为它一开始就没有安装。在大多数情况下,它将安装在这些路径之一 /usr/bin/ ; /bin ; /sbin 中,几乎所有 Linux 发行版上都始终在 PATH 中设置。
  • 我如何以管理员身份运行它?以及如何将其设置为路径变量?我正在使用 macOS
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    我现在遇到了这个问题。 OPs 原始帖子中未显示的是 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 的回答对我有用,我赞成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-26
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多