From 62c2ae21212bf5f7881e321e7a61f8ed81bb89e8 Mon Sep 17 00:00:00 2001
From: Jacek Lebioda <jacek.lebioda@uni.lu>
Date: Fri, 27 Mar 2020 10:28:59 +0100
Subject: [PATCH] feat: double deployment

---
 .ci/build_indices.sh     | 32 ++++++++++-----
 .ci/build_indices_new.sh | 19 +++++++++
 .ci/deploy.sh            | 35 +++++++++++++++++
 .gitlab-ci.yml           | 84 +++++++++++++++++++++++++++++++++-------
 4 files changed, 145 insertions(+), 25 deletions(-)
 create mode 100644 .ci/build_indices_new.sh
 create mode 100644 .ci/deploy.sh

diff --git a/.ci/build_indices.sh b/.ci/build_indices.sh
index 0c2d4067..ea563f40 100644
--- a/.ci/build_indices.sh
+++ b/.ci/build_indices.sh
@@ -1,19 +1,31 @@
 #!/bin/bash
 
+DIRECTORY=build
+
 # Install lunr to build the index
-npm install lunr@$LUNR_VERSION
+npm install lunr@2.3.8
 
-# Go to the directory containing the built website...
+# Go to the directory...
 cd $DIRECTORY
 
-echo "Generating the index..."    
+printf "Found the following build targets inside: \n$(ls -d */)\n\n"
+
+# ...and loop through all the build targets
+for branch in $(ls -d */); do
+    echo "Entering: $branch";
+
+    cd "$branch";
+
+    echo "Generating the index..."    
+    cat documents.js | node $CI_PROJECT_DIR/build_index.js > index.js
+    rm documents.js build_index.js
 
-# Pipe documents.js file (which should be generated by Jekyll after building the website) through build script
-cat documents.js | node build_index.js > index.js
+    echo "...should be completed. First 50 characters of the index: "
+    cat index.js | head -c50
+    echo ""
 
-# Show the results (should not be empty)
-echo "...should be completed. First 50 characters of the index: "
-cat index.js | head -c50
-echo ""
+    cd ..
+    
+done
 
-rm documents.js build_index.js
+rm $CI_PROJECT_DIR/build_index.js
diff --git a/.ci/build_indices_new.sh b/.ci/build_indices_new.sh
new file mode 100644
index 00000000..0c2d4067
--- /dev/null
+++ b/.ci/build_indices_new.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+
+# Install lunr to build the index
+npm install lunr@$LUNR_VERSION
+
+# Go to the directory containing the built website...
+cd $DIRECTORY
+
+echo "Generating the index..."    
+
+# Pipe documents.js file (which should be generated by Jekyll after building the website) through build script
+cat documents.js | node build_index.js > index.js
+
+# Show the results (should not be empty)
+echo "...should be completed. First 50 characters of the index: "
+cat index.js | head -c50
+echo ""
+
+rm documents.js build_index.js
diff --git a/.ci/deploy.sh b/.ci/deploy.sh
new file mode 100644
index 00000000..2cf5bfed
--- /dev/null
+++ b/.ci/deploy.sh
@@ -0,0 +1,35 @@
+mkdir -p build
+artefact="latest"
+
+# loop through all the branches
+for branch in $(git for-each-ref --format='%(refname:strip=3)' refs/remotes); do
+    echo $branch;
+    if [[ $branch == "master" ]]; then
+        artefact="stable";
+    elif [[ $branch == "develop" ]]; then
+        artefact="latest";
+    else
+        artefact="unstable";
+    fi
+    echo $artefact;
+    git checkout -f $branch
+    git reset --hard $branch
+    git pull
+
+    # build the website
+    bundle install
+    bundle exec jekyll build --url="https://r3.pages.uni.lu" --baseurl="howto-cards/$artefact" -d "build/$artefact"
+
+    if [[ $branch == "master" ]]; then
+        # set the 404
+        cp howto-cards/$artefact/404.html build/404.html
+    fi
+done
+
+# checkout the current branch
+echo $CI_COMMIT_REF_NAME
+git checkout -f $CI_COMMIT_REF_NAME
+git reset --hard origin/$CI_COMMIT_REF_NAME
+
+# set the auto redirection
+cp .ci/.autoRedirect build/index.html
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 75a250de..83a3ec66 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -6,15 +6,71 @@ stages:
   - generate_index
   - deploy
 
+# !!!!!!!      README      !!!!!
+# We are in the transition period - and the website is deployed two times - to gitlab pages and to a separate VM
+# In order to migrate to a new deployment:
+#  1) remove "Old deployment section" - build_pages, generate the search index and pages tasks
+#  2) rename "new pages" to "pages" in the new deployment section, and "new_public" to "public"
+#  3) remove ".ci/deploy.sh", ".ci/build_indices.sh"
+
+
+# Old deployment =================================================================
+
 build_pages:
   stage: build
+  before_script:
+    - apt-get -qq update
+    - apt-get install -y -qq git-lfs
+    - gem install bundler:2.0.2 && bundle install  
+  script:
+    - bash .ci/deploy.sh
   variables:
     JEKYLL_ENV: production
-    BUNDLER_VERSION: 2.0.2
   artifacts:
     expire_in: 1 day
     paths:
       - build
+
+
+generate the search index:
+  stage: generate_index
+  image: node:13.8.0-slim
+  rules:
+    - if: $CI_COMMIT_BRANCH == "master"
+    - if: $CI_COMMIT_BRANCH == "develop"
+  script:
+    - bash .ci/build_indices.sh
+    - mv build processed_build
+  artifacts:
+    expire_in: 1 day
+    paths:
+      - processed_build
+
+pages:
+  image: alpine:3.11.3
+  stage: deploy
+  rules:
+    - if: $CI_COMMIT_BRANCH == "master"
+    - if: $CI_COMMIT_BRANCH == "develop"
+  script:
+    - mv processed_build public
+  artifacts:
+    expire_in: 1 week
+    paths:
+      - public
+
+
+# New deployment ==================================================================
+
+build pages for VM deployment:
+  stage: build
+  variables:
+    JEKYLL_ENV: production
+    BUNDLER_VERSION: 2.0.2
+  artifacts:
+    expire_in: 1 day
+    paths:
+      - new_build
   before_script:
     - gem install bundler:$BUNDLER_VERSION && bundle install  
   script:
@@ -29,40 +85,38 @@ build_pages:
     - 'echo "Configuration: " && cat ".ci/_config_$CI_COMMIT_REF_NAME.yml"'
 
     # Run Jekyll with custom configuration
-    - bundle exec jekyll build -d build --config "_config.yml,.ci/_config_$CI_COMMIT_REF_NAME.yml"
+    - bundle exec jekyll build -d new_build --config "_config.yml,.ci/_config_$CI_COMMIT_REF_NAME.yml"
 
 
-generate the search index:
+generate the search index for VM deployment:
   stage: generate_index
   image: node:13.8.0-slim
   variables:
     LUNR_VERSION: 2.3.8
-    DIRECTORY: build
+    DIRECTORY: new_build
   script:
-    - bash .ci/build_indices.sh
-    - mv build processed_build
+    - bash .ci/build_indices_new.sh
+    - mv build new_processed_build
   artifacts:
     expire_in: 1 day
     paths:
-      - processed_build
+      - new_processed_build
 
-# Deploy to Gitlab Pages - to be run only by forks
-pages:
+# Deploy to Gitlab Pages - to be run only by forks; change to pages once it's ready
+new pages:
   image: alpine:3.11.3
   stage: deploy
   rules:
     - if: '$CI_PROJECT_NAMESPACE != "R3" && $CI_PROJECT_NAMESPACE != "R3-core"'
   script:
-    # For safety reasons:
-    - sleep 3600 && exit -1
-    - mv processed_buildXXXXX publicXXXX
+    - mv new_processed_build new_public
   artifacts:
     expire_in: 1 week
     paths:
-      - public
+      - new_public
 
 ## Deploys the website to a separate VM
-Deploy to production:
+deploy to production:
   stage: deploy
   image: alpine:3.1
   before_script:
@@ -79,6 +133,6 @@ Deploy to production:
     - if: '$CI_COMMIT_BRANCH == "16-deploy-to-staticpages-vm"'
   script:
     - ssh -p $SSHPORT $SSHCONNECT "mkdir -p ~/sources/public/$CI_COMMIT_BRANCH/$CI_JOB_ID ~/master/public ~/develop/public ~/$CI_COMMIT_BRANCH/public"
-    - scp -P $SSHPORT -r processed_build/* $SSHCONNECT:~/sources/public/$CI_COMMIT_BRANCH/$CI_JOB_ID
+    - scp -P $SSHPORT -r new_processed_build/* $SSHCONNECT:~/sources/public/$CI_COMMIT_BRANCH/$CI_JOB_ID
     - ssh -p $SSHPORT $SSHCONNECT "cd ~/$CI_COMMIT_BRANCH/public && ln -fs ~/sources/public/$CI_COMMIT_BRANCH/$CI_JOB_ID/* . && cd ~/sources/public/$CI_COMMIT_BRANCH && find . -type d -not -newermt '-1 minutes' -exec rm -rf {} +;"
 
-- 
GitLab