【问题标题】:Jenkins job fails for python3 targetspython3目标的Jenkins作业失败
【发布时间】:2021-06-16 04:05:13
【问题描述】:

我一直在尝试运行 Jenkins 作业(用于 python3 标签),但在 lintingtesting 阶段失败。以下是每个阶段的详细信息:

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 设置,才能识别命令 pylintpytest

我尝试通过指定 virtualenv 路径来设置它,然后为所有必需的包运行pip install,如下所示:

Jenkins 文件:

    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' failedmake: 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.

【问题讨论】:

  • 你不应该在配方中使用 make 的 $(shell ...) 函数。您的配方已经在 shell 中运行,不需要它,它可能会导致令人困惑的行为,因为它先扩展,然后再扩展配方的其余部分。请改用 shell 语法,例如 $$(find -type f -name "*.py")

标签: python python-3.x jenkins makefile jenkins-pipeline


【解决方案1】:

您可以在保存 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 或通过paramsas 在内部使用变量:dir(&lt;YOURVARIABLE_Containing_the PATH&gt;)

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)

【讨论】:

  • 感谢@Altaf。这样可行。但是,在上述路径中,每次存储库中有新的拉取请求时,PR 编号都会更改。有没有办法从 Jenkins 环境中获取这条路径并将其传递给 dir
  • @AnchalVerma 如果这对您有用,那么请您接受答案。在dir 是的,这是可能的,您可以使用环境变量。 eg:dir (env.&lt;YOURVARIABLE&gt;)或者作为参数传递dir(params.&lt;YOURVARIABLE&gt;),我已经更新了答案
  • 谢谢 Altaf,这很有效。我只需要用($WORKSPACE) 替换它
  • @Anchal Verma 很酷!如果对您有帮助,您能否接受答案。我已经在答案中更新了它
【解决方案2】:

更新您的 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")

【讨论】:

  • 我试过了,但它无法设置venv,并抛出了虚拟环境未找到的错误。我也尝试安装它,但效果不佳。无论如何,我可以通过添加dir 来定位到当前目录,正如@Altaf 所建议的那样
猜你喜欢
  • 1970-01-01
  • 2019-01-18
  • 1970-01-01
  • 2022-09-27
  • 2011-09-18
  • 2019-01-14
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多