GCP cloudbuild.yaml 创建 "workspace/tests" 但需要 "workspace/my_app/tests"

GCP cloudbuild.yaml creates "workspace/tests" but need "workspace/my_app/tests"

新手使用 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 以便构建可以找到我的测试。

这是我的 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']

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

无论如何,解决您的问题的一个简单方法是将您的存储库克隆到当前目录,我的意思是在 /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', '.']