diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index d2abdcd653c3315335c29058a8ca2774dad34577..0eb08796727321a0192f49b4bef60f9ebfead5c5 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -91,6 +91,21 @@ build-testenv:
       - docker build
         -t $CI_REGISTRY_IMAGE_BASE .
       - docker push $CI_REGISTRY_IMAGE_BASE
+      # Determine pylib branch name
+      - if [ -z "$PYLIB" ]; then
+          if echo "$CI_COMMIT_REF_NAME" | grep -c "^f-" ; then
+            echo "Check if pylib has branch $CI_COMMIT_REF_NAME" ;
+            if wget https://gitlab.indiscale.com/api/v4/projects/97/repository/branches/${CI_COMMIT_REF_NAME} ; then
+              PYLIB=$CI_COMMIT_REF_NAME ;
+            fi;
+          fi;
+        fi;
+      - PYLIB=${PYLIB:-dev}
+      - echo $PYLIB
+      - echo $PYLIB  > setup.env
+  artifacts:
+    reports:
+      dotenv: setup.env
 
 cert:
   tags: [docker]
@@ -138,9 +153,11 @@ unittest_py37:
   tags: [docker]
   stage: unittest
   image: python:3.7
+  needs: [build-testenv]
   script: &python_test_script
+    - echo $PYLIB
     - 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
 
@@ -148,18 +165,21 @@ unittest_py38:
   tags: [docker]
   stage: unittest
   image: python:3.8
+  needs: [build-testenv]
   script: *python_test_script
 
 unittest_py310:
   tags: [docker]
   stage: unittest
   image: python:3.10
+  needs: [build-testenv]
   script: *python_test_script
 
 unittest_py311:
   tags: [docker]
   stage: unittest
   image: python:3.11
+  needs: [build-testenv]
   script: *python_test_script
 
 # Build the sphinx documentation and make it ready for deployment by Gitlab Pages