1

每次推送到我的存储库的特定分支时,我都有一个触发器来运行构建作业。

如果我尝试使用以下命令“手动”(不使用触发器)运行构建作业:

# Submit the build job
_cmd = f"gcloud builds submit --no-source --config {config['build']['cloudbuild']} --substitutions {substitutions}"
subprocess.run(_cmd, shell=True, check=True)

它按预期工作并成功完成,没有任何问题。但是,如果我对我的存储库执行 git push 以使用触发器执行此操作,则在触发器启动构建作业并从我的 cloudbuild YAML 文件中检测到完整结构后,它会在第一步中断执行并显示错误消息:

第一步:

steps:
# Clone repo to Cloud Build environment
- name: 'gcr.io/cloud-builders/git'
  args: ['clone',
         '--branch',"$_BRANCH_NAME",
         '${_REPO_URL}', '.',
         '--depth', '1',
         '--verbose']
  id: 'Clone Repo'

错误信息:

fatal: destination path '.' already exists and is not an empty directory.

你知道问题可能是什么吗?

提前致谢!


编辑:

尝试在git clone之前清除目录,结果还是一样:

steps:
# Clear Cloud Build environment
- name: 'gcr.io/cloud-builders/git'
  args: ['rm', '-rf', '.']
  id: 'Clear Cloud Build environment'
  
# Clone repo to Cloud Build environment
- name: 'gcr.io/cloud-builders/git'
  args: ['clone',
         '--branch',"$_BRANCH_NAME",
         '${_REPO_URL}', '.',
         '--depth', '1',
         '--verbose']
  waitFor: ['Clear Cloud Build environment']
  id: 'Clone Repo'
4

2 回答 2

3

在本地运行作业时,使用--no-source不上传文件并使用空/workspace目录运行 Cloud Build

当您使用触发器启动 Cloud Build 时,您的/workspace目录已经包含源,并且您的克隆目标目录不为空。

你可以:

  • 在另一个目录中克隆,然后将内容移动到/workspace.
  • 或者rm -rf在你/workspace运行克隆命令之前执行一个
于 2020-12-17T16:01:12.770 回答
0

显然,通过更改 git clone 的目标目录并更新剩余步骤中的dir标签来解决问题:

steps:
# Clone repo to Cloud Build environment
- name: 'gcr.io/cloud-builders/git'
  args: ['clone',
         '--branch',"$_BRANCH_NAME",
         '${_REPO_URL}', 'new_workspace/',    # new directory
         '--depth', '1',
         '--verbose']
  id: 'Clone Repo'

# Create the image for the component 1
- name: 'gcr.io/cloud-builders/docker'
  args: ['build',
         '-t', 'gcr.io/$_PROJECT_ID/comp_1:$_IMG_TAG', '.']
  dir: 'new_workspace/implementation/pipeline/components/comp_1'  # update with new directory
  id: 'Build Component Image'

于 2020-12-17T19:52:26.543 回答