diff --git a/scripts/run-docs-build b/scripts/run-docs-build
index aa1847d..549032f 100755
--- a/scripts/run-docs-build
+++ b/scripts/run-docs-build
@@ -111,6 +111,11 @@ tar --exclude=eclipse -xJf ${docbookarchive_localpath}
 
 $scriptdir/docs_add_banner.py
 
+# Retrieve the active bitbake releases from the yocto active releases, by using
+# set_versions.py from yocto-docs
+BITBAKE_ACTIVE_RELEASES=$("$ypdocs"/set_versions.py bitbake-active-releases)
+export BITBAKE_ACTIVE_RELEASES
+
 cd $bbdocs
 mkdir -p $outputdir/bitbake
 
@@ -121,7 +126,8 @@ mkdir -p $outputdir/bitbake
 # see the latest releases.
 first_sphinx_commit=84ccba0f4aff91528f764523fe1205a354c889ed
 
-latest_branch=$(git branch --remote --contains "$first_sphinx_commit" --format '%(refname:lstrip=3)' --sort='-version:refname' | grep --max-count=1 "[0-9]*\.[0-9]*")
+# Get the latest branch from the active releases in BITBAKE_ACTIVE_RELEASES
+latest_branch="$(echo "$BITBAKE_ACTIVE_RELEASES" | cut -d',' -f1 | sort -V | tail -n 1)"
 
 for branch in 1.46 $(git branch --remote --contains "$first_sphinx_commit" --format '%(refname:lstrip=3)'); do
     if [ "$branch" = "HEAD" ]; then
@@ -132,7 +138,7 @@ for branch in 1.46 $(git branch --remote --contains "$first_sphinx_commit" --for
     git checkout $branch
     git reset --hard
     git clean -ffdx
-    git checkout origin/master releases.rst
+    git checkout origin/master releases.rst set_versions.py sphinx-static/switchers.js.in Makefile
 
     (
         . $builddir/buildtools/environment-setup*
