From d1f6df74917448424370a5e79a2103404500b43a Mon Sep 17 00:00:00 2001
From: Deepak Khatri <lorforlinux@beagleboard.org>
Date: Thu, 1 Sep 2022 17:48:44 +0530
Subject: [PATCH] Update GitLab pages build

Thanks to Jason for creating these changes, https://git.beagleboard.org/jkridner/docs.beagleboard.io/-/commit/7c3717bb98f2572505bef8a95468dadbc4bfa1af
---
 .gitlab-ci.yml  |  9 ++++-----
 gitlab-build.sh | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 5 deletions(-)
 create mode 100644 gitlab-build.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 3b6c2965..081d1aea 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -6,11 +6,10 @@ stages:
 pages:
   stage: deploy
   script:
-  - sphinx-build -b html . public
-  - sphinx-build -M latexpdf . public
-  - mv public/latex/beagleboard-docs.pdf public/
+  - ./gitlab-build.sh
+  - export
   artifacts:
     paths:
     - public
-  rules:
-    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
+  variables:
+    CI_DEBUG_TRACE: "true"
diff --git a/gitlab-build.sh b/gitlab-build.sh
new file mode 100644
index 00000000..93223754
--- /dev/null
+++ b/gitlab-build.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+env
+
+if [ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]; then
+
+rm -rf public
+sphinx-build -b html . public/latest/
+sphinx-build -M latexpdf . public/latest/
+mv public/latest/latex/beagleboard-docs.pdf public/latest/
+rm -rf public/latest/latex
+cat <<HERE > public/index.html
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta http-equiv="refresh" content="0; url='latest/'" />
+  </head>
+  <body>
+    <p>Please follow <a href="latest/">this link</a>.</p>
+  </body>
+</html>
+HERE
+
+elif [ "$CI_COMMIT_BRANCH" != "" ]; then
+
+sphinx-build -b html . public/$CI_COMMIT_BRANCH/
+sphinx-build -M latexpdf . public/$CI_COMMIT_BRANCH/
+mv public/$CI_COMMIT_BRANCH/latex/beagleboard-docs.pdf public/$CI_COMMIT_BRANCH/
+rm -rf public/$CI_COMMIT_BRANCH/latex
+
+elif [ "$CI_COMMIT_TAG" != "" ]; then
+
+export GIT_BRANCH=$(git branch -a --contains tags/$CI_COMMIT_TAG | grep origin | sed 's/.*origin\///')
+sphinx-build -b html . public/$GIT_BRANCH/
+sphinx-build -M latexpdf . public/$GIT_BRANCH/
+mv public/$GIT_BRANCH/latex/beagleboard-docs.pdf public/$GIT_BRANCH/beagleboard-docs-$CI_COMMIT_TAG.pdf
+ln -s public/$GIT_BRANCH/latex/beagleboard-docs-$CI_COMMIT_TAG.pdf public/$GIT_BRANCH/beagleboard-docs.pdf
+rm -rf public/$GIT_BRANCH/latex
+
+fi
-- 
GitLab