如何在 Cloud Build 中运行测试版 gcloud 组件,例如“gcloud beta artifacts docker images scan”?

问题描述

我正在尝试将 Container Analyis API link 包含在 Cloud Build 管道中。这是一个测试版组件,我需要先使用命令行安装它:

gcloud components install beta local-extract

然后我可以运行按需容器分析(如果容器在本地存在):

gcloud beta artifacts docker images scan ubuntu:latest

我的问题是如何在 Cloud Build 中使用 beta local-extract 之类的组件?

我尝试迈出第一步并安装缺少的组件L

## Update components
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['components','install','beta','local-extract','-q']
  id: Update component

但是一旦我进入下一步,更新就会消失(因为它不在容器中)

我也尝试安装该组件,然后使用 (& 或 ;) 运行扫描,但它失败了:

## Run vulnerability scan
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['components','-q',';','gcloud','artifacts','docker','images','scan','ubuntu:latest','--location=europe']

  id: Run vulnaribility scan

我得到:

Already have image (with digest): gcr.io/cloud-builders/gcloud
ERROR: (gcloud.components.install) unrecognized arguments:
  ;
  gcloud
  beta
  artifacts
  docker
  images
  scan
  ubuntu:latest
  --location=europe (did you mean '--project'?)
  To search the help text of gcloud commands,run:
  gcloud help -- SEARCH_TERMS

所以我的问题是:

  1. 如何在 Cloud Build 中运行“gcloud beta artifacts docker images scan ubuntu:latest”?
  2. 奖励:从上一个命令中,我如何获得需要作为参数传递给下一步的“扫描”输出值? (我想它应该是带有 --format 的东西)

解决方法

您应该尝试使用 cloud-sdk docker 镜像:

https://github.com/GoogleCloudPlatform/cloud-sdk-docker

Cloud Build 团队(暗示?)推荐它:

https://github.com/GoogleCloudPlatform/cloud-builders/tree/master/gcloud

使用 cloud-sdk-docker 容器,您可以将入口点更改为 bash 管道 gcloud 命令。这是一个(丑陋的)例子:

https://github.com/GoogleCloudPlatform/functions-framework-cpp/blob/d3a40821ff0c7716bfc5d2ca1037bcce4750f2d6/ci/build-examples.yaml#L419-L432

关于你的奖金问题。是的,--format=value(the.name.of.the.field) 可能就是您想要的。诀窍是知道字段的名称。我通常在我的开发工作站上以 --format=json 开头来找出名称。

,

问题来自 Cloud Build。它会缓存一些经常使用的图像,如果您想在 GCLOUD CLI 中使用全新功能,缓存可能会太旧。

我今晚做了一个测试,缓存中的版本是326。 328刚刚发布。因此,缓存版本已经使用了 2 周,对于您的功能来说可能太旧了。您所在地区的情况可能更糟!

解决此问题的解决方案是明确请求最新版本。

  • 转到此网址 gcr.io/cloud-builders/gcloud

  • 复制最新版本 enter image description here

  • 在您的 Cloud Build 流水线步骤中粘贴完整版本名称。

副作用是构建时间更长。事实上,因为这个最新的镜像没有被缓存,所以必须在 Cloud Build 中下载。