From 4b292b1d34ccf8c6d9f8d1d5d6d07be8597d767f Mon Sep 17 00:00:00 2001
From: Varac <varac@varac.net>
Date: Thu, 19 Sep 2019 12:36:15 +0200
Subject: [PATCH] Terminate droplet only after MR is merged

Closes: #301
---
 .gitlab-ci.yml | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5696eb065..160006bff 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -171,11 +171,25 @@ behave-grafana:
       - test/**/*
       - openappstack/**/*
 
-terminate:
+# Remove droplet after merge
+terminate_droplet_after_merge:
+  stage: cleanup
+  script: |
+    if [ "$(git cat-file -p HEAD | grep -c '^parent')" -gt 1 ]
+    then
+      pipeline=$(git cat-file -p 893aed0e1f1ce02a77b8f2a66ad03c24f09915d9 | grep 'Merge branch' | tr -dc '0-9')
+      echo "Current HEAD is a merge commit, removing droplet from related merge request pipeline #${pipeline}."
+      python3 -c "import greenhost_cloud; greenhost_cloud.terminate_droplets_by_name(\"^ci-${pipeline}\.\")"
+    else
+      echo "Current HEAD is NOT a merge commit, nothing to do."
+    fi
+  only:
+    refs:
+      - master
+
+terminate_old_droplets:
   stage: cleanup
   script:
-    # Remove droplet after successful tests
-    - echo "$CI_COMMIT_MESSAGE" | grep '!ci_dont_terminate' && echo 'Termination of droplet disabled in commit message.' || python3 -m openappstack $HOSTNAME --terminate
     # Remove droplet older than 2 days
     - python3 -c "import greenhost_cloud; greenhost_cloud.terminate_droplets_by_name(\"^ci-\", 2)"
   only:
@@ -186,7 +200,8 @@ terminate:
       - test/**/*
       - openappstack/**/*
 
-# This trivial job works around a Gitlab bug: if no job runs at all due to
+# We need one job that run every time (without any `only:` limitation).
+# This works around a Gitlab bug: if no job runs at all due to
 # `only`, Gitlab gets confused and doesn't allow you to merge the MR:
 # https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html#limitations
 gitlab-merge-workaround:
-- 
GitLab