无法使用云构建上传非图像工件
unable to upload non-image artifacts with cloud build
我有一个非常简单的容器(实际上是 Cloud Build quickstart sample code) that generates a file. I'm trying to extend this container to upload said file to a bucket via the documentation on 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 到达 "upload artifacts" 步骤之前擦除文件一样,但这似乎没有意义。我想这是一个非常常见的用例,但我并没有在文档方面取得任何进展。有任何想法吗?谢谢。
这里的问题是,在当前步骤中,您只是在构建容器而不是 运行ning 它,因此不会创建 time.txt 文件。即使您 运行 容器,文件也会在容器内创建,因此您需要从容器内获取它,以便 gsutil 可以 "see" 文件。
我在 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'
希望这对你有用。
我有一个非常简单的容器(实际上是 Cloud Build quickstart sample code) that generates a file. I'm trying to extend this container to upload said file to a bucket via the documentation on 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 到达 "upload artifacts" 步骤之前擦除文件一样,但这似乎没有意义。我想这是一个非常常见的用例,但我并没有在文档方面取得任何进展。有任何想法吗?谢谢。
这里的问题是,在当前步骤中,您只是在构建容器而不是 运行ning 它,因此不会创建 time.txt 文件。即使您 运行 容器,文件也会在容器内创建,因此您需要从容器内获取它,以便 gsutil 可以 "see" 文件。
我在 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'
希望这对你有用。