簡體   English   中英

Pip 安裝不適用於 jenkins?

[英]Pip install not working with jenkins?

那是我的 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,但並沒有取得多大進展。 所以我該怎么做?

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

您需要將 virtualenv 添加到 PATH 變量。

如果您使用pip install virtualenv安裝它,它將位於pythonX.X/Lib/site-packages/

還應該將Sudo添加到 PATH 變量中。


第一個代碼片段中的錯誤是因為您沒有寫入'/.local'權限。 嘗試以管理員身份運行

嘗試添加參數-u root:root像這樣:

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

我現在遇到了這個。 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' 嘗試運行時,由於沒有主目錄,它默認為不可寫的 '/'。

我認為這是詹金斯的謬論(見https://issues.jenkins-ci.org/browse/JENKINS-47026 )。 其他 CICD 服務似乎可以很好地處理這個問題。

中山洋一的回答對我有用,我很贊成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM