首页 > 解决方案 > 如何在 Cloud Build 中运行 beta gcloud 组件,例如“gcloud beta artifacts docker images scan”?

问题描述

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

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', 'install', 'beta', 'local-extract', '-q', ';', 'gcloud', 'beta', '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)

标签: google-cloud-platformgoogle-cloud-buildbeta

解决方案


您应该尝试使用cloud-sdkdocker 映像:

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我的开发工作站开始找出名称。


推荐阅读