【问题标题】:GCP cloudbuild.yaml creates "workspace/tests" but need "workspace/my_app/tests"GCP cloudbuild.yaml 创建“workspace/tests”但需要“workspace/my_app/tests”
【发布时间】:2020-04-18 16:21:13
【问题描述】:

使用 Cloud Build 的新手。尝试运行测试,但它缺少路径中的顶级项目文件夹。我不知道如何在构建步骤中解决这个问题。项目结构为:

gcp_cicd_workflow
  |-- src
     | my_module.py
  |-- tests
     | test_my_module.py

在云构建尝试运行测试时出现错误:

imported module 'test_my_module' has this __file__ attribute:
/workspace/gcp-cicd-workflow/tests/test_my_module.py
which is not the same as the test file we want to collect:
/workspace/tests/test_my_module.py

这对我来说非常有意义,因为默认情况下,Cloud Build 使用名为 /workspace 的目录作为工作目录。我需要做的是弄清楚如何创建路径 workspace/gcp-cicd-workflow 以便 build 可以找到我的测试。

这是我的完整 cloudbuild.yaml 文件(直到它出错:

steps:

  # Step 0
- name: 'gcr.io/cloud-builders/git'
  args: ['clone', '--recurse-submodules', 'https://github.com/GDBSD/gcp-cicd-workflow']

  # Step 1
  # Variable $COMMIT_SHA provided by the Cloud Build so we test the correct commit.
- name: 'gcr.io/cloud-builders/git'
  args: [ 'checkout', '$COMMIT_SHA']
  dir: 'gcp-cicd-workflow'

  # Step 2
  # Cloud Build automatically substitutes $PROJECT_ID for your Project ID
- name: 'gcr.io/$PROJECT_ID/python-cloudbuild'
  entrypoint: '/bin/bash'
  args: ['-c', 'python3 -m venv /workspace/venv']

  # Step 3
  # Installs any dependencies listed in the project's requirements.txt.
- name: 'gcr.io/$PROJECT_ID/python-cloudbuild'
  entrypoint: 'venv/bin/pip'
  args: ['install', '-V', '-r', 'requirements.txt']

  # Step 4
  # Runs pytest from the virtual environment (with all requirements)
  # using the verbose flag so you can see each individual test.
- name: 'gcr.io/$PROJECT_ID/python-cloudbuild'
  entrypoint: 'venv/bin/python'
  args: ['-m', 'pytest', '-v']

【问题讨论】:

    标签: python git google-cloud-platform google-cloud-build


    【解决方案1】:

    您有一个自定义构建器,但我不知道它是如何工作的。它可能适用于绝对路径,这应该是问题所在。

    无论如何,解决您的问题的一个简单方法是将您的存储库克隆到当前目录,我的意思是在/workspace/ 这样

    # Step 0, simply add a . as final arg
    - name: 'gcr.io/cloud-builders/git'
      args: ['clone', '--recurse-submodules', 'https://github.com/GDBSD/gcp-cicd-workflow', '.']
    

    【讨论】:

    • 谢谢,我想从命令行做所有事情,但想利用构建器工具。我的自定义构建器所做的只是使用单行 docker 添加 Python 3。
    • 如果你只使用 Python 和 PIP 没有在你的管道中进行任何自定义,你可以直接使用官方图像!无需自定义构建器!
    • 谢谢纪尧姆。这也是我此时的直觉。关键目标是 CI/CD,我需要在部署之前在 Builder 中或以某种方式运行测试。可能可以通过多种方式完成所有工作。正在使用本教程:github.com/GoogleCloudPlatform/professional-services/tree/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 2020-08-16
    相关资源
    最近更新 更多