Continuous integration 是否可以从云构建步骤启动PubSub Emulator

Continuous integration 是否可以从云构建步骤启动PubSub Emulator,continuous-integration,google-cloud-pubsub,google-cloud-build,Continuous Integration,Google Cloud Pubsub,Google 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

正如标题所提到的,我想知道,在云构建步骤中,我是否可以启动并使用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,我想使用模拟器

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

感谢

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

gcloud components install pubsub-emulator

此外,由于大多数Gcloud模拟器都需要java操作,因此有必要安装Open JDK7。

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

gcloud components install pubsub-emulator

另外,安装Open JDK7也是必要的,因为大多数Gcloud模拟器都需要java来运行。

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

根据需要,您需要一个
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 emulator。

当我找到解决方法和解决方案时,我想与您分享解决方案

根据需要,您需要一个
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 emulator