From 7852e00a90158ba877625066b075ecbab111cf17 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Tue, 19 Apr 2022 13:26:37 +0200
Subject: [PATCH] refactor(ci): add deploy-version.sh helper script

refs #505
---
 .gitlab-ci.yml             | 75 +++-----------------------------------
 scripts/deploy-version.sh  | 29 +++++++++++++++
 scripts/release-version.sh | 18 +++++++--
 3 files changed, 48 insertions(+), 74 deletions(-)
 create mode 100755 scripts/deploy-version.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 196f45238..edc90f67c 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -26,9 +26,12 @@ variables:
   RELEASES_PATH: "$PROD_PATH/cassiopee-releases"
 
 before_script:
-  # load private key from GitLab CI variable, to deploy on Aubes server
+  # load private key from GitLab CI variable to deploy on Aubes server
   - eval $(ssh-agent -s)
   - ssh-add <(echo "$AUBES_B64_AUTHORIZED_SSH_PRIVATE_KEY" | base64 -d)
+  # load private key from GitLab CI variable to deploy on OVH server
+  - ssh-add <(echo "$OVH_B64_AUTHORIZED_SSH_PRIVATE_KEY" | base64 -d)
+  # disable console prompt for host checking
   - mkdir -p ~/.ssh
   - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
 
@@ -38,11 +41,6 @@ cache:
 
 jalhyd:
   stage: jalhyd
-  only:
-    - pushes
-    - tags
-    - schedules
-    - web
   script:
     - JALHYD_BRANCH=`cat jalhyd_branch`
     - echo "CI_COMMIT_REF_NAME - $CI_COMMIT_REF_NAME"
@@ -59,31 +57,12 @@ jalhyd:
 
 install:
   stage: install
-  only:
-    - pushes
-    - tags
-    - schedules
-    - web
   script:
     - rm -rf node_modules
     - npm ci --force --unsafe-perm
 
-test:
-  stage: test
-  only:
-    - tags
-    - schedules
-    - web
-  script:
-    - npm run e2e
-
 build:
   stage: build
-  only:
-    - pushes
-    - tags
-    - schedules
-    - web
   artifacts:
     expire_in: 10 min
     paths:
@@ -91,53 +70,9 @@ build:
   script:
     - npm run build -- --base-href=/cassiopee/$CI_COMMIT_REF_NAME/
 
-clean-stale-branches:
-  stage: clean-stale-branches
-  only:
-    - pushes
-    - tags
-    - web
-  script:
-    - ./scripts/remove-stale-branches.sh $DEV_LOGIN@$DEV_HOST $DEV_PATH
-
-deploy-dev:
-  stage: deploy-dev
-  only:
-    - pushes
-    - tags
-    - web
-  dependencies:
-    - build
-  script:
-    # Copie de la branche / du tag
-    - rsync --delete -a "dist/" "$DEV_LOGIN@$DEV_HOST:$DEV_PATH/$CI_COMMIT_REF_NAME"
-
 deploy-prod:
   stage: deploy-prod
-  only:
-    variables:
-      - $CI_COMMIT_REF_NAME == "stable"
   dependencies:
     - build
   script:
-    # Copie de la branche production
-    - rsync --delete -a "dist/" "$PROD_LOGIN@$PROD_HOST:$PROD_PATH/"
-    # Modification du dossier base href
-    - ssh $PROD_LOGIN@PROD_HOST "sed -i 's:/cassiopee/stable/:/:g' $PROD_PATH/index.html"
-
-releases-nightly:
-  stage: releases-nightly
-  only:
-    - schedules
-  dependencies: []
-  script:
-    - ./scripts/release-version.sh nightly $PROD_LOGIN $PROD_HOST $RELEASES_PATH
-
-releases-version:
-  stage: releases-version
-  only:
-    variables:
-      - $CI_COMMIT_REF_NAME =~ /^[0-9]+\.[0-9]+\.[0-9]+$/ # version tag
-  dependencies: []
-  script:
-    - ./scripts/release-version.sh $CI_COMMIT_REF_NAME $PROD_LOGIN $PROD_HOST $RELEASES_PATH
+    - ./scripts/deploy-version.sh stable $PROD_LOGIN $PROD_HOST $PROD_PATH $PROD_PASS
diff --git a/scripts/deploy-version.sh b/scripts/deploy-version.sh
new file mode 100755
index 000000000..fc6473fb9
--- /dev/null
+++ b/scripts/deploy-version.sh
@@ -0,0 +1,29 @@
+#!/bin/bash
+
+# deploie l'appli sur le serveur $3 (user $2/mot de passe $5) dans le dossier $4 et met à jour le fichier index.html
+# si la version est "stable"
+
+if (( $# < 4 || $# > 5 )); then
+    echo "usage: $0 <version> <login> <server> <path> [<password>]"
+    exit 1
+fi
+
+VERSION="$1"
+LOGIN="$2"
+HOST="$3"
+DIR="$4"
+PASS="$5"
+
+echo "$(basename $0): deploying version $VERSION in $LOGIN@$HOST:$DIR"
+
+if [[ $VERSION == "stable" ]]; then
+  # Copie de la branche production
+  rsync -a --delete -e "ssh -o StrictHostKeyChecking=no" dist/ ${LOGIN}@${HOST}:${DIR}/
+
+  # Modification du dossier base href
+  echo "updating index.html"
+  ssh $LOGIN@$HOST "sed -i 's:/cassiopee/stable/:/:g' $DIR/index.html"
+else
+  # Copie de la branche / du tag
+  rsync --delete -a "dist/" "$LOGIN@$HOST:$DIR/$VERSION"
+fi
diff --git a/scripts/release-version.sh b/scripts/release-version.sh
index c17a7d311..e5ed23e6d 100755
--- a/scripts/release-version.sh
+++ b/scripts/release-version.sh
@@ -1,10 +1,12 @@
-#!/bin/bash
+#!/bin/bash -xv
 
 # Fabrique les exécutables electron/cordova pour une version de Cassiopée $1, les
 # distribue sur le serveur $2@$3 dans le dossier $4, et met à jour le fichier releases.json
 # et les liens symboliques si la version n'est pas nightly
 
-if [ "$#" -lt 4 ]; then
+echo "args $@"
+
+if (( $# < 4 )); then
     echo "usage: $0 <X.Y.Z|nightly> <login> <server> <releases dir>"
     exit 1
 fi
@@ -24,7 +26,7 @@ if [[ $VERSION == "nightly" ]]; then
   update_latest=0
 fi
 
-echo "deploy-version.sh: building release for version $VERSION, deploying in $HOST_LOGIN:$RELEASES_DIR"
+echo "release-version.sh: building release for version $VERSION, deploying in $HOST_LOGIN:$RELEASES_DIR"
 
 # build releases
 npm run release-all
@@ -59,7 +61,15 @@ if (( $update_latest )); then
 fi
 
 # copy releases to public web directory
-scp "release/Cassiopée Setup $VERSION.exe" "release/fr.irstea.cassiopee_${VERSION}_amd64.deb" "release/cassiopee-$VERSION.apk" "release/Cassiopée-${VERSION}-mac.zip" "$HOST_LOGIN:$RELEASES_DIR/"
+ssh $HOST_LOGIN mkdir -p $RELEASES_DIR
+if [[ $VERSION == nightly ]]; then
+  find release -name "fr.irstea.cassiopee_*.deb" -exec scp "{}" $HOST_LOGIN:$RELEASES_DIR/linux-nightly.deb \;
+  find release -name "Cassio*Setup*.exe" -exec scp "{}" $HOST_LOGIN:$RELEASES_DIR/windows-nightly.exe \;
+  find release -name "Cassio*-mac.zip" -exec scp "{}" $HOST_LOGIN:$RELEASES_DIR/macos-nightly.zip \;
+  find release -name "cassiopee-*.apk" -exec scp "{}" $HOST_LOGIN:$RELEASES_DIR/android-nightly.apk \;
+else
+  scp "release/Cassiopée Setup $VERSION.exe" "release/fr.irstea.cassiopee_${VERSION}_amd64.deb" "release/cassiopee-$VERSION.apk" "release/Cassiopée-${VERSION}-mac.zip" "$HOST_LOGIN:$RELEASES_DIR/"
+fi
 
 # symlink "latest" version for each platform
 
-- 
GitLab