Ken*_* D. 22 virtualenv jenkins
我正在使用Makefile来提供一致的单个命令来设置virtualenv,运行测试等.我已经将我的Jenkins实例配置为从mercurial repo中提取然后运行"make virtualenv",它执行此操作:
virtualenv --python=/usr/bin/python2.7 --no-site-packages . && . ./bin/activate && pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,它坚持使用系统安装的pip并尝试在系统站点包而不是virtualenv中安装我的包依赖项:
error: could not create '/usr/local/lib/python2.7/dist-packages/flask': Permission denied
Run Code Online (Sandbox Code Playgroud)
如果我添加一些调试命令并明确指向我的virtualenv中的pip,事情会变得更加混乱:
virtualenv --python=/usr/bin/python2.7 --no-site-packages . && . ./bin/activate && ls -l bin && which pip && pwd && ./bin/pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)
这会生成以下输出:
New python executable in ./bin/python2.7
Not overwriting existing python script ./bin/python (you must use ./bin/python2.7)
Installing setuptools, pip...done.
Running virtualenv with interpreter /usr/bin/python2.7
Run Code Online (Sandbox Code Playgroud)
看起来詹金斯并没有从头开始为每个版本重建环境,这让我感到奇怪的选择,但不应影响我的直接问题
"ls -l bin"的输出显示要安装在virtualenv和executable中的pip:
-rw-r--r-- 1 jenkins jenkins 2248 Apr 9 21:14 activate
-rw-r--r-- 1 jenkins jenkins 1304 Apr 9 21:14 activate.csh
-rw-r--r-- 1 jenkins jenkins 2517 Apr 9 21:14 activate.fish
-rw-r--r-- 1 jenkins jenkins 1129 Apr 9 21:14 activate_this.py
-rwxr-xr-x 1 jenkins jenkins 278 Apr 9 21:14 easy_install
-rwxr-xr-x 1 jenkins jenkins 278 Apr 9 21:14 easy_install-2.7
-rwxr-xr-x 1 jenkins jenkins 250 Apr 9 21:14 pip
-rwxr-xr-x 1 jenkins jenkins 250 Apr 9 21:14 pip2
-rwxr-xr-x 1 jenkins jenkins 250 Apr 9 21:14 pip2.7
lrwxrwxrwx 1 jenkins jenkins 9 Apr 10 19:31 python -> python2.7
lrwxrwxrwx 1 jenkins jenkins 9 Apr 10 19:31 python2 -> python2.7
-rwxr-xr-x 1 jenkins jenkins 3349512 Apr 10 19:31 python2.7
Run Code Online (Sandbox Code Playgroud)
"哪个点"似乎想要使用正确的输出:
/var/lib/jenkins/jobs/Run Tests/workspace/bin/pip
Run Code Online (Sandbox Code Playgroud)
我目前的工作目录是我所期望的:
/var/lib/jenkins/jobs/Run Tests/workspace
Run Code Online (Sandbox Code Playgroud)
但是...... wtf?
/bin/sh: 1: ./bin/pip: Permission denied
make: *** [virtualenv] Error 126
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Run Code Online (Sandbox Code Playgroud)
sor*_*rin 15
在过去的两年里,我每天都在和詹金斯一起使用python virtualenvs,在多家公司和小型项目中,我不能说我找到了"答案".不过,我希望分享我的经验可以帮助其他人节省时间.希望我会得到进一步的反馈,以便更容易做出决定.
如果您使用多个需要相同virtualenv的构建器,最简单的方法是将环境转储到文件并在新构建器的开头处将其源代码化.
为了便于维护,我打算调查这些:
如果你点击shebang命令行限制,最好的办法是将你的jenkins主目录更改为just /j.
可以使Jenkins管道在虚拟环境中运行,但需要考虑多种因素.
/bin/sh- 这可以在Manage Jenkins - > Configure System - > Shell - > Shell executable中配置.设置此项/bin/bash将使source工作.如果您使用版本控制的多分支管道,jenkins会创建一个工作空间,其中包含分支名称和路径中的提交哈希 - 这可能会很长.venv脚本(例如pip)都以hashbang行开头,其中包括venv中python解释器的完整路径(python解释器本身是一个符号链接).例如,
~/workspace/ink_feature-use-jenkinsfile-VGRPYD53GGGDDSBIJDLSUDYPJ34QR63ITGMC5VJNB56W6ID244AA/env/bin$ cat pip
#!/var/jenkins_home/workspace/ink_feature-use-jenkinsfile-VGRPYD53GGGDDSBIJDLSUDYPJ34QR63ITGMC5VJNB56W6ID244AA/env/bin/python3.5
Run Code Online (Sandbox Code Playgroud)
Bash只读取N任何可执行文件的第一个字符 - 我发现它并不完全包含完整的venv路径:
bash: ./pip: /var/jenkins_home/workspace/ink_feature-use-jenkinsfile-VGRPYD53GGGDDSBIJDLSU: bad interpreter: No such file or directory
Run Code Online (Sandbox Code Playgroud)
通过使用Python执行脚本可以避免这个特殊问题.例如python3.5 ./pip
我建议避免使用 ShiningPanda。
我使用Anaconda / Miniconda设置了我的虚拟环境。安装 conda 时,请确保您以 jenkins 用户身份运行。
your_user@$ sudo -u jenkins sh
jenkins@$ wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
jenkins@$ bash Miniconda3-latest-Linux-x86_64.sh
Run Code Online (Sandbox Code Playgroud)
由于 Jenkins 运行sh而不是bash,我将 conda 路径添加到/etc/profile:
export PATH="/var/lib/jenkins/miniconda3/bin:$PATH"
Run Code Online (Sandbox Code Playgroud)
然后在 Jenkinsfile 中,您可以创建和删除conda 环境。这是为每个构建创建新环境的示例:
pipeline {
agent any
stages {
stage('Unit tests') {
steps {
sh '''
conda create --yes -n ${BUILD_TAG} python
source activate ${BUILD_TAG}
// example of unit test with nose2
pip install nose2
nose2
'''
}
}
}
post {
always {
sh 'conda remove --yes -n ${BUILD_TAG} --all'
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有同样的问题。正如我所看到的 - 您的项目名为“运行测试”。所以,这个名字包含空格。这对我来说就是问题。我刚刚重命名了项目,例如 RunTests - 和 venv 现在正在工作!注意 - jenkins 询问您有关确认重命名项目的信息。
| 归档时间: |
|
| 查看次数: |
25132 次 |
| 最近记录: |