是否可以从 Cloud Build 步骤启动 PubSub 模拟器 [英] Is it possible to start PubSub Emulator from Cloud Build step

查看:18
本文介绍了是否可以从 Cloud Build 步骤启动 PubSub 模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所述,我想知道是否可以从 Cloud Build 步骤开始并使用 pubsub 模拟器?

As the title mentions, I would like to know if, from a Cloud Build step, I can start and use the pubsub emulator?

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,我想使用模拟器.

For a test I need it, it works locally and instead of using a dedicated pubsub from cloud build, I want to use an emulator.

谢谢

推荐答案

当我找到一个解决方法和一个 有趣的 git 存储库,我想和你分享解决方案.

As I found a workaround and an interesting git repository, I wanted to share with you the solution.

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

As required, you need a cloud-build.yaml and you want to add a step where the emulator will get launched:

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 命令,该命令将实际启动模拟器.

As you can see, I run a docker-compose command which will actually start the emulator.

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 模拟器.

It is important to set the container name as well as the network, otherwise you won't be able to access the pubsub emulator from another cloud build step.

这篇关于是否可以从 Cloud Build 步骤启动 PubSub 模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆