是否可以从Cloud Build步骤启动PubSub仿真器

问题描述

正如标题所述,我想知道是否可以从Cloud Build步骤中启动并使用pubsub仿真器?

options:
  env:
    - GO111MODULE=on
    - goproxy=https://proxy.golang.org
    - PUBSUB_EMULATOR_HOST=localhost:8085
  volumes:
    - name: "go-modules"
      path: "/go"

steps:
  - name: "golang:1.14"
    args: ["go","build","."]

  # Starts the cloud pubsub emulator
  - name: 'gcr.io/cloud-builders/gcloud'
    entrypoint: 'bash'
    args: [
      '-c','gcloud beta emulators pubsub start --host-port 0.0.0.0:8085 &'
    ]

  - name: "golang:1.14"
    args: ["go","test","./..."]

对于我需要的测试,它可以在本地运行,而不是使用云构建中的专用pubsub,而是要使用仿真器。

谢谢

解决方法

当我找到解决方法和一个interesting git repository时,我想与您分享解决方案。

根据需要,您需要一个cloud-build.yaml,并且要添加一个启动仿真器的步骤:

options:
  env:
    - GO111MODULE=on
    - GOPROXY=https://proxy.golang.org
    - PUBSUB_EMULATOR_HOST=localhost:8085
  volumes:
    - name: "go-modules"
      path: "/go"

steps:
  - name: "golang:1.14"
    args: ["go","build","."]

  - name: 'docker/compose'
    args: [
        '-f','docker-compose.cloud-build.yml','up','--build','-d'
    ]
    id: 'pubsub-emulator-docker-compose'

  - name: "golang:1.14"
    args: ["go","test","./..."]

如您所见,我运行了一个docker-compose命令,该命令实际上将启动模拟器。

version: "3.7"

services:
  pubsub:
    # Required for cloudbuild network access (when external access is required)
    container_name: pubsub
    image: google/cloud-sdk
    ports:
      - '8085:8085'
    command: ["gcloud","beta","emulators","pubsub","start","--host-port","0.0.0.0:8085"]
    network_mode: cloudbuild

networks:
  default:
    external:
      name: cloudbuild

设置容器名称和网络非常重要,否则您将无法从另一个云构建步骤访问pubsub仿真器。

,

这是有可能的,因为Cloud Build的每个步骤都在docker容器中执行,但是映像gcr.io/cloud-builders/gcloud仅安装了gcloud组件的最低数量,因此在启动模拟器之前,您需要通过gcloud安装pubsub模拟器命令

gcloud components install pubsub-emulator

由于大多数Gcloud模拟器都需要Java进行操作,因此还必须安装Open JDK7。