【问题标题】:Can't find installed modules in azure devops pipeline在 azure devops 管道中找不到已安装的模块
【发布时间】:2021-12-29 10:02:00
【问题描述】:

我正在构建阶段管道(环境 prp、测试、代码)。目前已经面临拦截器。似乎每个阶段都是一个单独的过程。我的 requirements.txt 安装正确,但测试阶段引发 ModuleNotFoundError。感谢任何提示如何使它工作:)

yaml:

trigger: none
parameters:
  - name: "input_files"
    type: object
    default: ['a-rg', 't-rg', 'd-rg', 'p-rg']

stages:
  - stage: 'Env_prep'
    jobs:
      - job: "install_requirements"
        steps:
          - script: |
              python -m pip install --upgrade pip
              python -m pip install -r requirements.txt
  - stage: 'Tests'
    jobs:
      - job: 'Run_tests'
        steps:
          - script: |
              python -m pytest -v tests/variableGroups_tests.py

【问题讨论】:

    标签: python azure azure-devops azure-pipelines


    【解决方案1】:

    不同的作业和阶段能够在 Azure Pipelines 中的不同代理上执行。在您的情况下,安装要求是运行测试的直接先决条件,因此一切都应该在一项工作中完成:

    trigger: none
    parameters:
      - name: "input_files"
        type: object
        default: ['a-rg', 't-rg', 'd-rg', 'p-rg']
    
    stages:
      - stage: Test
        jobs:
          - job:
            steps:
              - script: |
                  python -m pip install --upgrade pip
                  python -m pip install -r requirements.txt
                displayName: Install Required Components
              - script: |
                  python -m pytest -v tests/variableGroups_tests.py
                displayName: Run Tests
    

    除非您希望日志输出在控制台中分开,否则甚至不需要将它们分成单独的脚本步骤。

    【讨论】:

    • 好的,但是有没有办法以某种方式合并阶段?我的意思是我希望 ro 运行 requirements.txt 一次并允许进一步的阶段使用它的模块。
    • 并非如此 - 在您的原始场景中,如果您重新运行管道,该安装阶段将再次运行,因此每次触发管道时都会发生。更好的解决方案是更改您的脚本以检查 requirements.txt 的存在,并且仅在它不存在时才安装。
    猜你喜欢
    • 2021-12-02
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2020-10-15
    • 2020-12-25
    相关资源
    最近更新 更多