diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index a6f739f33551918f07d8c4da66f90d8def297b66..e2f1e8a91582fc639229ad154826ae719b1f5bf3 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -15,15 +15,15 @@ stages:
   - deploy
 
 variables:
-  TARGET_BRANCH: master
   # Parameters for downloading nightlies and building Moore
   BINARY_TAG: x86_64_v2-el9-gcc13-opt
   NO_LBLOGIN: "1" # prevent lbdocker containers to start LbLogin/LbEnv
-  LB_NIGHTLY_SLOT: lhcb-master
 
 check-copyright:
   stage: check
   image: gitlab-registry.cern.ch/ci-tools/ci-worker:cc7
+  before_script:
+    - TARGET_BRANCH=${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-${CI_COMMIT_BRANCH}}
   script:
     - curl -o lb-check-copyright "https://gitlab.cern.ch/lhcb-core/LbDevTools/raw/master/LbDevTools/SourceTools.py?inline=false"
     - python lb-check-copyright origin/${TARGET_BRANCH} --exclude lhcbproject.yml
@@ -33,6 +33,8 @@ check-formatting:
   tags:
     - cvmfs
   image: gitlab-registry.cern.ch/ci-tools/ci-worker:cc7
+  before_script:
+    - TARGET_BRANCH=${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-${CI_COMMIT_BRANCH}}
   script:
     - . /cvmfs/lhcb.cern.ch/lib/LbEnv.sh
     - if [ ! -e .clang-format ] ; then
@@ -62,6 +64,8 @@ build:
   image: gitlab-registry.cern.ch/lhcb-docker/os-base/alma9-devel:latest
   tags:
     - cvmfs
+  before_script:
+    - LB_NIGHTLY_SLOT="lhcb-${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-${CI_COMMIT_BRANCH}}"
   script:
     - . /cvmfs/lhcb.cern.ch/lib/LbEnv.sh
     - build_path=/cvmfs/lhcbdev.cern.ch/nightlies/${LB_NIGHTLY_SLOT}/latest
@@ -93,12 +97,13 @@ build-docs:
     # As the previous stage is allowed to fail, skip this stage if the previous
     # did not complete successfully (i.e. did not create the `run` symlink)
     - test -L run || { echo "WARNING build job failed, stopping"; exit 77; }
+    - TARGET_BRANCH=${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-${CI_COMMIT_BRANCH}}
   script:
     # First try producing just the graphs (gaudirun.py). If that fails, Moore
     # is likely incompatible with the last nightly, so fail with a warning.
     - ./run make -C doc graphs || { echo "WARNING build is incompatible with last nightly, stopping"; exit 77; }
     - reasons=
-    - ./run python doc/make_functor_docs.py "${CI_COMMIT_SHA:0:8}" "master" > doc/selection/thor_functors_reference.generated.rst || reasons+='ERROR failed functor docs build\n'
+    - ./run python doc/make_functor_docs.py "${CI_COMMIT_SHA:0:8}" "${TARGET_BRANCH}" > doc/selection/thor_functors_reference.generated.rst || reasons+='ERROR failed functor docs build\n'
     - ./run make -C doc linkcheck || reasons+='ERROR failed link check\n'
     - ./run make -C doc html || reasons+='ERROR failed html generation\n'
     - if [ -n "$reasons" ]; then echo -e $reasons; exit 1; fi