From b43f88aa432fc9199facea26d58d3363f28950bf Mon Sep 17 00:00:00 2001
From: Tom Wilkie <tom.wilkie@gmail.com>
Date: Tue, 5 Jun 2018 12:51:48 +0100
Subject: [PATCH] Update circle config, deploy master branch automatically

Signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
---
 .circleci/config.yml | 71 +++++++++++++++++++-------------------------
 Makefile             |  3 ++
 2 files changed, 33 insertions(+), 41 deletions(-)

diff --git a/.circleci/config.yml b/.circleci/config.yml
index 5cba34a7..84123f12 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -6,20 +6,29 @@ workflows:
     jobs:
     - test
     - build
-    - deploy:
+    - publish:
         requires:
         - test
         - build
+    - deploy:
+        requires:
+        - publish
+        filters:
+          branches:
+            only: master
+
+# https://circleci.com/blog/circleci-hacks-reuse-yaml-in-your-circleci-config-with-yaml/
+defaults: &defaults
+  docker:
+    - image: gcr.io/metrictank-gcr/logish-build-image:master-b5b6999
+      auth:
+        username: _json_key
+        password: $GCR_CREDS
+  working_directory: /go/src/github.com/grafana/logish
 
 jobs:
   test:
-    docker:
-      - image: gcr.io/metrictank-gcr/logish-build-image:master-017c0b4
-        auth:
-          username: _json_key
-          password: $GCR_CREDS
-
-    working_directory: /go/src/github.com/grafana/logish
+    <<: *defaults
     steps:
       - checkout
 
@@ -30,26 +39,11 @@ jobs:
             make BUILD_IN_CONTAINER=false test
 
   build:
-    docker:
-      - image: gcr.io/metrictank-gcr/logish-build-image:master-017c0b4
-        auth:
-          username: _json_key
-          password: $GCR_CREDS
-
-    working_directory: /go/src/github.com/grafana/logish
+    <<: *defaults
     steps:
       - checkout
       - setup_remote_docker
 
-      - run:
-          name: Install Docker client
-          command: |
-            set -x
-            VER="17.03.0-ce"
-            curl -L -o /tmp/docker-$VER.tgz https://download.docker.com/linux/static/stable/x86_64/docker-$VER.tgz
-            tar -xz -C /tmp -f /tmp/docker-$VER.tgz
-            mv /tmp/docker/* /usr/bin
-
       - run:
           name: Build Images
           command: |
@@ -67,27 +61,12 @@ jobs:
           paths:
           - images/
 
-  deploy:
-    docker:
-      - image: gcr.io/metrictank-gcr/logish-build-image:master-017c0b4
-        auth:
-          username: _json_key
-          password: $GCR_CREDS
-
-    working_directory: /go/src/github.com/grafana/logish
+  publish:
+    <<: *defaults
     steps:
       - checkout
       - setup_remote_docker
 
-      - run:
-          name: Install Docker client
-          command: |
-            set -x
-            VER="17.03.0-ce"
-            curl -L -o /tmp/docker-$VER.tgz https://download.docker.com/linux/static/stable/x86_64/docker-$VER.tgz
-            tar -xz -C /tmp -f /tmp/docker-$VER.tgz
-            mv /tmp/docker/* /usr/bin
-
       - restore_cache:
           key: v1-logish-{{ .Branch }}-{{ .Revision }}
 
@@ -102,3 +81,13 @@ jobs:
           command: |
             docker login -u _json_key -p "$GCR_CREDS" https://gcr.io &&
             make push-images
+
+  deploy:
+    <<: *defaults
+    steps:
+      - run: |
+          images=$(make images)
+          curl -s --header "Content-Type: application/json" \
+            --data "{\"build_parameters\": {\"CIRCLE_JOB\": \"deploy\", \"IMAGE_NAMES\": \"$(make images-names)\"}}" \
+            --request POST \
+            https://circleci.com/api/v1.1/project/github/raintank/deployment_tools/tree/master?circle-token=$CIRCLE_TOKEN
diff --git a/Makefile b/Makefile
index 6bc848de..480c3ad8 100644
--- a/Makefile
+++ b/Makefile
@@ -125,6 +125,9 @@ shell: build-image/$(UPTODATE)
 
 endif
 
+images:
+	echo $(IMAGE_NAMES)
+
 save-images:
 	@mkdir -p images
 	for image_name in $(IMAGE_NAMES); do \
-- 
GitLab