【问题标题】:unable to upload non-image artifacts with cloud build无法使用云构建上传非图像工件
【发布时间】:2019-09-23 23:29:33
【问题描述】:

我有一个生成文件的非常简单的容器(实际上是Cloud Build quickstart 示例代码)。我正在尝试扩展此容器以通过storing non-image artifacts with Cloud Build 上的文档将所述文件上传到存储桶。

我的 Dockerfile 构建一个简单的容器并执行一个脚本:

FROM alpine
WORKDIR /app
COPY . /app # the only file present is quickstart.sh
CMD ["./quickstart.sh"]

脚本(quickstart.sh)生成一个简单的时间戳文件:

#!/bin/sh
echo "Creating file 'time.txt'"
echo "The time is $(date)" > time.txt

## for debugging:
# pwd
# ls 
# cat time.txt

我的 cloudbuild.yaml 文件基本上是从上述文档中复制粘贴的,并配置为上传文件:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/quickstart-image', '.' ]
artifacts:
  objects:
    location: 'gs://my-bucket/'
    paths: ['*.txt']
images:
- 'gcr.io/$PROJECT_ID/quickstart-image'

但是,文件无法上传,因此构建失败。当我运行构建命令时

gcloud builds submit --config cloudbuild.yaml .

所有日志都成功到最后:

Artifacts will be uploaded to gs://my-bucket using gsutil cp
*.txt: Uploading path....
CommandException: No URLs matched: *.txt
CommandException: 1 file/object could not be transferred.
ERROR
ERROR: could not upload *.txt to gs://my-bucket/; err = exit status 1

gsutil 声称找不到匹配的文件。但是,如果我手动构建并生成文件,我可以使用gsutil cp *.txt gs://my-bucket/ 毫无问题地上传文件。因此,就好像在 Cloud Build 到达“上传工件”步骤之前文件已被擦除,但这似乎没有意义。我想这是一个非常常见的用例,但我并没有单独在文档方面取得任何进展。有任何想法吗?谢谢。

【问题讨论】:

  • 您在脚本中使用了相对路径。相反,在输出和输入文件名中使用完整路径。原因是您正在对 shell 和程序的默认工作目录进行假设(通过忽略它们)。
  • 谢谢@JohnHanley。你说的很有道理。但是,更新 shell 脚本以写入完整路径 (/app/time.txt) 并更新 yaml 配置以读取完整路径 (/app/*.txt/**/*.txt**/*.txt) 仍然会产生相同的错误 :( 知道我是什么可能会丢失吗?
  • 查看 Cloud Build 服务帐号的角色。它有写入你的存储桶的作用吗?
  • @guillaumeblaquiere Cloud Build 服务帐户角色能够写入存储桶。我可以在构建时成功将文件写入存储桶如果它们已经存在于本地目录中。只有quickstart.sh动态生成的文件写入失败。

标签: docker google-cloud-platform cloud google-cloud-storage google-cloud-build


【解决方案1】:

这里的问题是,使用当前步骤,您只是构建容器而不运行它,因此不会创建 time.txt 文件。即使您运行容器,文件也会在容器内创建,因此您需要从容器内获取它,以便 gsutil 可以“看到”该文件。

我在 cloudbuild.yaml 文件中添加了 2 个步骤来执行此操作:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/quickstart-image', '.' ]
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'run', '--name', 'containername', 'gcr.io/$PROJECT_ID/quickstart-image']
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'cp', 'containername:/app/time.txt, './time.txt']
artifacts:
  objects:
    location: 'gs://mybucket/'
    paths: ['*.txt']
images:
- 'gcr.io/$PROJECT_ID/quickstart-image'

我希望这对你有用。

【讨论】:

  • 这确实有效,谢谢@siamsot。我现在明白 building 实际上并没有创建文件,因为 running 是调用 CMD 的东西,它会生成文件。不太清楚的是复制文件的要求。在与同事交谈后,我相信我正在做的是将文件从我的孩子 quickstart-image 容器复制到父 cloud-builders/docker 容器中,这是 gsutil 可以访问的(而不是复制到我的本地机器)。听起来对吗?
  • 以我理解容器的方式,我会说有一个容器在运行,但是在第三步中我们指示 Cloud Build worker 进入容器,获取 txt 文件然后将其放入在工作目录中。如果我错了,其他人可以纠正我,但这是我的理论。
  • @siamsot 我找到了您的帖子,并认为您可能知道如何解决我的问题? stackoverflow.com/questions/63687647/…
猜你喜欢
  • 2015-10-10
  • 1970-01-01
  • 2021-01-02
  • 2017-05-20
  • 2013-03-25
  • 2022-07-29
  • 2011-01-13
  • 2020-04-18
  • 1970-01-01
相关资源
最近更新 更多