【问题标题】:Is it possible to start PubSub Emulator from Cloud Build step是否可以从 Cloud Build 步骤启动 PubSub 模拟器
【发布时间】:2020-12-22 13:25:48
【问题描述】:

如标题所述,我想知道是否可以从 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,我想使用模拟器。

谢谢

【问题讨论】:

    标签: continuous-integration google-cloud-pubsub google-cloud-build


    【解决方案1】:

    这是可能的,因为 Cloud Build 上的每一步都是在 docker 容器中执行的,但是图像 gcr.io/cloud-builders/gcloud 只安装了最少的 gcloud 组件,在启动模拟器之前,您需要通过 gcloud 命令安装 pubsub 模拟器

    gcloud components install pubsub-emulator
    

    另外需要安装Open JDK7,因为大部分Gcloud模拟器都需要java才能运行。

    【讨论】:

      【解决方案2】:

      当我找到解决方法和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 模拟器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-18
        • 2012-10-15
        • 1970-01-01
        • 1970-01-01
        • 2019-12-21
        • 2022-01-22
        • 1970-01-01
        相关资源
        最近更新 更多