From ee2a95f6152a9aecf7f72bd4e01f7b417b33273d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com>
Date: Mon, 16 Oct 2023 20:48:26 +0200
Subject: [PATCH] MAINT: add env setup to pipeline

---
 .gitlab-ci.yml | 106 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 104 insertions(+), 2 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index d2abdcd6..dab81d9d 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -33,6 +33,107 @@ stages:
   - integrationtest
   - deploy
 
+.env: &env
+  - PYLIB=${TMP_PYLIB:-$PYLIB}
+  - PYINT=${TMP_PYINT:-$PYINT}
+  - CPPINT=${TMP_CPPINT:-$CPPINT}
+  - CPPLIB=${TMP_CPPLIB:-$CPPLIB}
+  - SERVER=${TMP_SERVER:-$SERVER}
+  - WEBUI=${TMP_WEBUI:-$WEBUI}
+  - MYSQLBACKEND=${TMP_MYSQLBACKEND:-$MYSQLBACKEND}
+  - echo "F_BRANCH = $F_BRANCH"
+  - echo "SERVER = $SERVER"
+  - echo "MYSQLBACKEND = $MYSQLBACKEND"
+  - echo "CPPINT = $CPPINT"
+  - echo "CPPLIB = $CPPLIB"
+  - echo "PYINT = $PYINT"
+  - echo "PYLIB = $PYLIB"
+  - echo "ADVANCEDUSERTOOLS = $ADVANCEDUSERTOOLS"
+
+
+setup-env:
+  stage: setup
+  tags: [ cached-dind ]
+  needs: [ ]
+  image: python:3
+  script:
+    # Calculate the values first.
+    - *env
+    - pip install requests
+    - if [ "${CI_COMMIT_REF_NAME:0:8}" == release- ] ; then
+        echo "Release branch detected, setting default F_BRANCH to 'main'.";
+        F_BRANCH="${F_BRANCH:-main}";
+      fi
+    - F_BRANCH=${F_BRANCH:-$CI_COMMIT_REF_NAME}
+    - if [ "$F_BRANCH" == "main" ] && [ "$CI_COMMIT_REF_NAME" != "main" ] ; then
+        DEFAULT_BRANCH=main
+        F_BRANCH=;
+        REFTAG="${CI_COMMIT_REF_NAME}_F_main";
+      elif [ "$F_BRANCH" != "$CI_COMMIT_REF_NAME" ] && echo "$F_BRANCH" | grep -c "^f-" ; then
+        REFTAG="${CI_COMMIT_REF_NAME}_F_${F_BRANCH}";
+      elif echo "$CI_COMMIT_REF_NAME" | grep -c "^f-" ; then
+        F_BRANCH="${CI_COMMIT_REF_NAME}";
+        REFTAG="${CI_COMMIT_REF_NAME}";
+      else
+        F_BRANCH=;
+        REFTAG="${CI_COMMIT_REF_NAME}";
+      fi
+
+    - if [ "${CI_COMMIT_REF_NAME}" == "main" ]; then
+        DEFAULT_BRANCH=main;
+      fi
+    - DEFAULT_BRANCH=${DEFAULT_BRANCH:-dev}
+      # if this is main, default to other main branches.
+
+
+    - echo "DEFAULT_BRANCH = $DEFAULT_BRANCH"
+    - echo "CI_COMMIT_REF_NAME = $CI_COMMIT_REF_NAME"
+    - echo "REFTAG = $REFTAG"
+    - echo "F_BRANCH = $F_BRANCH"
+
+      # determine branch for caosdb-pylib (specific branch, f-branch or dev)
+    - echo "PYLIB REF = $PYLIB"
+    - if [ "$PYLIB" == "" ]  && [ "$F_BRANCH" != "" ] && ./utils/branch_exists.py PYLIB "$F_BRANCH" ; then
+        PYLIB="$F_BRANCH";
+      fi
+    - PYLIB=${PYLIB:-$DEFAULT_BRANCH}
+    - if [ "$PYLIB" != "$DEFAULT_BRANCH" ] && [ "$PYLIB" != "$F_BRANCH" ] ; then
+        REFTAG="${REFTAG}_P_${PYLIB}";
+      fi
+    - echo "PYLIB REF = $PYLIB"
+
+      # convert branch to commit
+    - PYLIB=`./utils/ref_to_commit.py PYLIB $PYLIB`;
+    - echo "PYLIB COMMIT = $PYLIB"
+
+      # determine branch for caosdb-pyinttest (specific branch, f-branch or dev)
+    - echo "PYINT REF = $PYINT"
+    - if [ "$PYINT" == "" ]  && [ "$F_BRANCH" != "" ] && ./utils/branch_exists.py PYINT "$F_BRANCH" ; then
+        PYINT="$F_BRANCH";
+      fi
+    - PYINT=${PYINT:-$DEFAULT_BRANCH}
+    - if [ "$PYINT" != "$DEFAULT_BRANCH" ] && [ "$PYINT" != "$F_BRANCH" ] ; then
+        REFTAG="${REFTAG}_IP_${PYINT}";
+      fi
+    - echo "PYINT REF = $PYINT"
+
+    - echo "REFTAG = $REFTAG"
+
+    ########################################
+    # Exporting values to environment file
+    ####
+    - echo "DEFAULT_BRANCH=${DEFAULT_BRANCH}" >> "$DOTENV"
+    - echo "F_BRANCH=${F_BRANCH}" >> "$DOTENV"
+    - echo "REFTAG=${REFTAG}" >> "$DOTENV"
+    - echo "PYLIB=${PYLIB}" >> "$DOTENV"
+    - echo "TMP_PYLIB=${PYLIB}" >> "$DOTENV"
+    - echo "PYINT=${PYINT}" >> "$DOTENV"
+    - echo "TMP_PYINT=${PYINT}" >> "$DOTENV"
+    - cat "$DOTENV"
+  artifacts:
+    reports:
+      dotenv: "$DOTENV"
+
 test:
   tags: [docker]
   services:
@@ -127,8 +228,9 @@ unittest_py39:
   tags: [docker]
   stage: unittest
   image: $CI_REGISTRY_IMAGE
-  needs: [build-testenv]
+  needs: [build-testenv, setup-env]
   script:
+    - *env
     # First verify that system Python actually is 3.9
     - python3 -c "import sys; assert sys.version.startswith('3.9')"
     - python3 -c "import linkahead; print('LinkAhead Version:', linkahead.__version__)"
@@ -140,7 +242,7 @@ unittest_py37:
   image: python:3.7
   script: &python_test_script
     - pip install nose pandas pytest pytest-cov gitignore-parser openpyxl>=3.0.7 xlrd==1.2 h5py
-    - pip install git+https://gitlab.indiscale.com/caosdb/src/caosdb-pylib.git@dev
+    - pip install git+https://gitlab.indiscale.com/caosdb/src/caosdb-pylib.git@$PYLIB
     - pip install .
     - pytest --cov=caosadvancedtools unittests
 
-- 
GitLab