Search code examples
continuous-integrationgoogle-cloud-pubsubgoogle-cloud-buildgoogle-cloud-pubsub-emulator

Is it possible to start PubSub Emulator from Cloud Build step


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


Solution

  • 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.