Is it possible to start PubSub Emulator from Cloud Build step

1.1k Views Asked by At

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", "./..."]

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.

Thanks

2

There are 2 best solutions below

1
On BEST ANSWER

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

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", "./..."]

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

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.

0
On

It is possible since every step on Cloud Build is executed in a docker container, but the image gcr.io/cloud-builders/gcloud only has a minimum installation of the gcloud components, before start the emulator your need to install the pubsub emulator via gcloud command

gcloud components install pubsub-emulator

Also it is necessary to install Open JDK7 since most of the Gcloud emulators needs java to operate.