python3 个目标的 Jenkins 作业失败
Jenkins job fails for python3 targets
我一直在尝试 运行 Jenkins 作业(针对 python3 标签),但它在 linting
和 testing
阶段失败了。以下是每个阶段的详细信息:
linting : 此阶段会触发 Makefile 目标 make lint
,它会尝试对我项目中的所有 python 文件进行 运行 pylint。
即 pylint $(shell find -type f -name "*.py")
测试: 此阶段触发 Makefile 目标 make test
,它试图对我项目中的所有文件进行 运行 pytest。
即 pytest --cov=. --cov-report=term --cov-report=html --cov-fail-under=95 my_project
以上两个阶段都需要在 Jenkins 中进行一些预安装或 virtualenv 设置,以便它识别命令 pylint
和 pytest
。
我尝试通过指定 virtualenv 路径来设置它,然后 运行 为所有必需的包设置 pip install
,如下所示:
詹金斯文件:
stage("linting") {
steps {
withEnv(["HOME=${env.WORKSPACE}", "PATH+PIP=${env.WORKSPACE}/.local/bin"]) {
sh "make lint-code"
}
}
}
生成文件:
lint-code:
pip install --upgrade pip
pip install -r requirements.txt
@pylint $(shell find -type f -name "*.py")
requiremnts.txt
pylint
pytest
我得到 make: pylint: Command not found Makefile:5: recipe for target 'lint' failed
或 make: pylint: Command not found Makefile:5: Error [127]
此外,即使在 Jenkins 阶段传递 $PATH 后也没有设置,因此出现以下错误:
The scripts py.test and pytest are installed in '/home/ubuntu/workspace/my_project_PR-56/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
您可以在保存 pytest 或脚本的 Jenkins 中更新您的 PATH 变量。
另一种选择是将 dir
与脚本所在的路径一起使用:
// dir("<Your path where scripts are present>")
dir ("/home//ubuntu/workspace//my_project_PR-56//.local//bin"){
sh "make lint-code"
}
编辑:
您还可以使用 env
或通过 params
在内部使用变量:dir(<YOURVARIABLE_Containing_the PATH>)
pipeline
{
parameters
{
string(name: 'Script_Location', defaultValue: '/home//ubuntu/workspace//my_project_PR-56//.local//bin', description: 'Where the script shall be executed')
}
stage ('stage 1'){
dir (params.Script_Location){
sh "make lint-code"
}
}
}
或
dir ($WORKSPACE)
如下更新 lint 代码的 makefile:
生成文件
lint-code:
pip install --upgrade pip
python3 -m venv venv
source ./venv/bin/activate
pip install -r requirements.txt
deactivate
source ./venv/bin/activate
@pylint $(shell find -type f -name "*.py")
我一直在尝试 运行 Jenkins 作业(针对 python3 标签),但它在 linting
和 testing
阶段失败了。以下是每个阶段的详细信息:
linting : 此阶段会触发 Makefile 目标 make lint
,它会尝试对我项目中的所有 python 文件进行 运行 pylint。
即 pylint $(shell find -type f -name "*.py")
测试: 此阶段触发 Makefile 目标 make test
,它试图对我项目中的所有文件进行 运行 pytest。
即 pytest --cov=. --cov-report=term --cov-report=html --cov-fail-under=95 my_project
以上两个阶段都需要在 Jenkins 中进行一些预安装或 virtualenv 设置,以便它识别命令 pylint
和 pytest
。
我尝试通过指定 virtualenv 路径来设置它,然后 运行 为所有必需的包设置 pip install
,如下所示:
詹金斯文件:
stage("linting") {
steps {
withEnv(["HOME=${env.WORKSPACE}", "PATH+PIP=${env.WORKSPACE}/.local/bin"]) {
sh "make lint-code"
}
}
}
生成文件:
lint-code:
pip install --upgrade pip
pip install -r requirements.txt
@pylint $(shell find -type f -name "*.py")
requiremnts.txt
pylint
pytest
我得到 make: pylint: Command not found Makefile:5: recipe for target 'lint' failed
或 make: pylint: Command not found Makefile:5: Error [127]
此外,即使在 Jenkins 阶段传递 $PATH 后也没有设置,因此出现以下错误:
The scripts py.test and pytest are installed in '/home/ubuntu/workspace/my_project_PR-56/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
您可以在保存 pytest 或脚本的 Jenkins 中更新您的 PATH 变量。
另一种选择是将 dir
与脚本所在的路径一起使用:
// dir("<Your path where scripts are present>")
dir ("/home//ubuntu/workspace//my_project_PR-56//.local//bin"){
sh "make lint-code"
}
编辑:
您还可以使用 env
或通过 params
在内部使用变量:dir(<YOURVARIABLE_Containing_the PATH>)
pipeline
{
parameters
{
string(name: 'Script_Location', defaultValue: '/home//ubuntu/workspace//my_project_PR-56//.local//bin', description: 'Where the script shall be executed')
}
stage ('stage 1'){
dir (params.Script_Location){
sh "make lint-code"
}
}
}
或
dir ($WORKSPACE)
如下更新 lint 代码的 makefile:
生成文件
lint-code:
pip install --upgrade pip
python3 -m venv venv
source ./venv/bin/activate
pip install -r requirements.txt
deactivate
source ./venv/bin/activate
@pylint $(shell find -type f -name "*.py")