Python Pip安装不与jenkins一起工作?

Python Pip安装不与jenkins一起工作?,python,docker,jenkins,pip,Python,Docker,Jenkins,Pip,那是我的詹金斯档案 pipeline { agent none stages { stage('Build') { agent { docker { image 'python:3-alpine' } } steps { sh 'pip install --u

那是我的詹金斯档案

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'
                }
            }
        }
    }
}
当我在詹金斯运行它时,我得到了以下信息

[urltester]运行shell脚本
+pip安装--user-r requirements.txt
当前用户不拥有目录“/.cache/pip/http”或其父目录,并且已禁用缓存。请检查该目录的权限和所有者。如果使用sudo执行pip,您可能需要sudo的-H标志。
当前用户不拥有目录“/.cache/pip”或其父目录,并且已禁用缓存控制盘。检查该目录的权限和所有者。如果使用sudo执行pip,您可能需要sudo的-H标志。
收集beautifulsoup4==4.6.0(来自-r requirements.txt(第1行))
正在下载https://files.pythonhosted.org/packages/9e/d4/10f46e5cfac773e22707237bfcd51bbffeaf0a576b0a847ec7ab15bd7ace/beautifulsoup4-4.6.0-py3-none-any.whl (86kB)
收集请求==2.18.4(来自-r requirements.txt(第2行))
正在下载https://files.pythonhosted.org/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl (88kB)
收集JUnitXML==1.8(来自-r requirements.txt(第3行))
正在下载https://files.pythonhosted.org/packages/a6/2a/f8d5aab80bb31fcc789d0f2b34b49f08bd6121cd8798d2e37f416df2b9f8/junit-xml-1.8.tar.gz
正在收集urllib3=1.21.1(来自请求==2.18.4->-r requirements.txt(第2行))
正在下载https://files.pythonhosted.org/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)
收集idna=2.5(来自请求==2.18.4->-r requirements.txt(第2行))
正在下载https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)
收集certifi>=2017.4.17(来自请求==2.18.4->-r requirements.txt(第2行))
正在下载https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl (150kB)
收集chardet=3.0.2(来自请求==2.18.4->-r requirements.txt(第2行))
正在下载https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
收集六个(来自JUnitXML==1.8->-r requirements.txt(第3行))
正在下载https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
安装收集的软件包:beautifulsoup4、urllib3、idna、certifi、chardet、requests、six、JUnitXML
由于环境错误,无法安装程序包:[Errno 13]权限被拒绝:'/.local'
检查权限。
脚本返回退出代码1
然后我会安装sudopip

我得到以下错误:

[urltester]运行shell脚本
+sudopython-mpip安装--user-r requirements.txt
/Users/me/.jenkins/workspace/urltester@tmp/持久-e36d9731/script.sh:第1行:sudo:未找到
脚本返回退出代码127
然后我移除了sudo并尝试使用虚拟环境:

管道{
一号特工
舞台{
阶段(‘构建’){
代理人{
码头工人{
图片“python:3-alpine”
}
}
台阶{
sh‘虚拟虚拟虚拟——分发’
sh“源venv/bin/激活”
sh'pip安装--user-r requirements.txt'
sh'pythonwebchecker.py'
}
职位{
总是{
junit'output.xml'
}
}
}
}
}
但后来我得到了和我尝试sudo时一样的东西,但这次没有找到virtualenv


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

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

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

Sudo
也应添加到PATH变量中



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

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

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

尝试添加参数-u root:root,如下所示:

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

我现在遇到了这个问题。OPs原始帖子中没有显示的是詹金斯用来运行Docker的命令。结果是:

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

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

我认为这是詹金斯的谬论(见)。其他CICD服务似乎处理得很好


中山洋一的回答对我很有效,我正在投票支持它。

sudo
据我所知,它不是“默认”阿尔卑斯码头工人形象的一部分。您需要通过扩展docker映像并添加
RUN apk install--no cache sudo
来显式安装它,否则您将得到
command not found
。这并不是因为它不在您的
路径中,而是因为它不是一开始就安装的。在大多数情况下,它将安装在以下路径之一
/usr/bin//垃圾桶/sbin
在几乎所有linux发行版上总是设置在
路径中。我如何作为管理员运行它?如何将其设置为路径变量?我正在使用macos@Rahul请参见此图,
Jenkins
启动的docker容器通常使用