无法通过云构建上传非图像伪像

时间:2019-09-20 22:47:07

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

我有一个非常简单的容器(实际上是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到达“上传工件”步骤之前就已擦除了文件一样,但这似乎没有任何意义。我以为这是一个非常普遍的用例,但仅凭文档我就没有取得任何进展。有任何想法吗?谢谢。

1 个答案:

答案 0 :(得分:2)

这里的问题是,按照当前步骤,您只是在构建容器而不运行它,因此不会创建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'

我希望这对您有用。