gcpug / nouhau

Google Cloud Platformのノウハウを共有するRepository
https://gcpug.jp
MIT License
654 stars 23 forks source link

CircleCI 2.0 で Google Cloud * Emulator を使う #8

Open apstndb opened 6 years ago

apstndb commented 6 years ago

Cloud PubSub を使ったソフトウェアの CI を Circle CI 2.0 上で Google Cloud Pub/Sub Emulator でテストしたくなることがある。 emulator を Secondary Container として動かすことでわりとうまく行ったので共有する。 ここでは pubsub-emulator を使ったが、 cloud-datastore-emulator などの他のエミュレータでも同様にできるはず。

emulator が立ち上がる前にテストが始まると fail してしまうが sleep の時間を決めるのは大変なので jwilder/dockerize で待つようにしている。

version: 2
jobs:
  test:
    docker:
      - image: google/cloud-sdk:174.0.0
      - image: google/cloud-sdk:174.0.0
        command: [gcloud, beta, emulators, pubsub, start, '--host-port=localhost:8085']
    environment:
       PUBSUB_EMULATOR_HOST: localhost:8085
    steps:
      # 省略
      - run:
          name: install dockerize
          command: curl -sfL https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz | tar -C /usr/local/bin -xzv
          environment:
            DOCKERIZE_VERSION: v0.3.0
      - run:
          name: Wait for pubsub emulator
          command: dockerize -wait tcp://${PUBSUB_EMULATOR_HOST} -timeout 1m
      - run:
          name: goapp test
          command: goapp test ${PKG}
apstndb commented 6 years ago

雑なので全部入りの google/cloud-sdk イメージを使っているけど、必要に応じて小さいイメージを作った方がエコ

apstndb commented 6 years ago

どうせイメージ作るなら dockerize も入れてしまったほうがよい

vvakame commented 6 years ago

Cloud Datastore Emulator の例 https://github.com/mercari/datastore/blob/master/.circleci/config.yml セットアップに時間が結構かかるのでサボって dockerize というのは入れていない