From a68e1717005c1f5a321eef3f74e8fda751f9b31a Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Tue, 29 Dec 2020 13:37:37 +0100
Subject: [PATCH] gitlab-ci-cd/drupal#1 Optimize code for DB rebuild logic

---
 test-and-deploy.yml | 80 +++++++++++++++++++++++++++++----------------
 1 file changed, 52 insertions(+), 28 deletions(-)

diff --git a/test-and-deploy.yml b/test-and-deploy.yml
index 953bd50..c3b23a8 100644
--- a/test-and-deploy.yml
+++ b/test-and-deploy.yml
@@ -1,9 +1,13 @@
 stages:
+  - validation
   - build
   - prepare
   - test
   - deploy
 
+variables:
+  DBREQUIRED: "no"
+
 .wait:
   script:
     - while [ ! -f /tmp/foo.txt ]; do echo "waiting..."; sleep 1; done
@@ -47,6 +51,33 @@ stages:
   key: build-${COMPOSE_PROJECT_NAME}
   <<: *cache_paths
 
+.prerequisites:
+  stage: validation
+  tags:
+    - default
+  variables:
+    GIT_STRATEGY: none
+  script: |
+    if [[ "$CI_COMMIT_REF_NAME" == "master" ]]; then
+      DBREQUIRED="yes"
+    elif [[ "$CI_COMMIT_REF_NAME" == *"[re-build-db]"* ]]; then
+      DBREQUIRED="yes"
+    elif [[ "$FORCE_DB_UPDATE" == "yes" ]]; then
+      DBREQUIRED="yes"
+    else
+      ID=$(docker container ls --all -q -f name=^${COMPOSE_PROJECT_NAME}_mariadb_1$)
+      if [[ ! -n ${ID} ]]; then
+        DBREQUIRED="yes"
+      else
+        # TODO: check if DB in container is empty.
+        echo "DB check not yet implemented"
+      fi
+    fi
+  except:
+    variables:
+      - $CAE
+      - $DISABLE_CI_TESTS
+
 .build:
   stage: build
   tags:
@@ -91,19 +122,16 @@ stages:
     - ansible
   variables:
     GIT_STRATEGY: none
-  script:
-    - ascr drupal-dump-db ${CI_PROJECT_PATH} ${PWD}/${CI_PROJECT_NAME}.sql master
+  script: |
+    if [[ "$DBREQUIRED" == "yes" ]]; then
+      ascr drupal-dump-db ${CI_PROJECT_PATH} ${PWD}/${CI_PROJECT_NAME}.sql master
+    fi
   cache: {}
   artifacts:
     name: dbdump
     when: always
     paths:
       - ${CI_PROJECT_NAME}.sql
-  only:
-    variables:
-      - $CI_COMMIT_REF_NAME == "master"
-      - $CI_COMMIT_MESSAGE =~ /\[re-build-db\]/i
-      - $FORCE_DB_UPDATE == "yes"
   except:
     refs:
       - tags
@@ -118,19 +146,16 @@ stages:
     - default
   variables:
     GIT_STRATEGY: none
-  script:
-    - docker cp ${CI_PROJECT_NAME}.sql ${COMPOSE_PROJECT_NAME}_php_1:/var/www/html
-    - drush sql:drop --yes
-    - drush sql:query --file=../${CI_PROJECT_NAME}.sql
-    - drush cr
-    - drush updatedb
-    - drush config-import sync
-    - drush cr
-  only:
-    variables:
-      - $CI_COMMIT_REF_NAME == "master"
-      - $CI_COMMIT_MESSAGE =~ /\[re-build-db\]/i
-      - $FORCE_DB_UPDATE == "yes"
+  script: |
+    if [[ "$DBREQUIRED" == "yes" ]]; then
+      docker cp ${CI_PROJECT_NAME}.sql ${COMPOSE_PROJECT_NAME}_php_1:/var/www/html
+      drush sql:drop --yes
+      drush sql:query --file=../${CI_PROJECT_NAME}.sql
+      drush cr
+      drush updatedb
+      drush config-import sync
+      drush cr
+    fi
   except:
     refs:
       - tags
@@ -148,19 +173,18 @@ stages:
     - default
   variables:
     GIT_STRATEGY: none
-  script:
-    - drush cr
-    - drush updatedb
-    - drush config-import sync
-    - drush cr
+  script: |
+    if [[ "$DBREQUIRED" == "no" ]]; then
+      drush cr
+      drush updatedb
+      drush config-import sync
+      drush cr
+    fi
   except:
     refs:
       - master
       - tags
     variables:
-      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
-      - $CI_COMMIT_MESSAGE =~ /\[re-build-db\]/i
-      - $FORCE_DB_UPDATE == "yes"
       - $CAE
       - $DISABLE_CI_TESTS
   dependencies:
-- 
GitLab