From d9a9c9539c8d9b56d5a219465f0794abaefb195f Mon Sep 17 00:00:00 2001
From: Michael Blaschek <michael.blaschek@univie.ac.at>
Date: Tue, 31 Jan 2023 15:19:22 +0000
Subject: [PATCH] addedgitlab ci for singularity build and push to repo

---
 .gitlab-ci.yml     |  36 +++++++++
 .gitlabci/build.sh | 177 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 213 insertions(+)
 create mode 100644 .gitlab-ci.yml
 create mode 100644 .gitlabci/build.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..536c61f
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,36 @@
+image:
+  name: quay.io/singularity/singularity:v3.7.3
+  entrypoint: ["/bin/sh", "-c"]
+
+build:
+    when: manual
+    script:
+        - /bin/bash .gitlabci/build.sh Singularity
+        # step 1. build the container!
+        # You can add any other sregistry push commands here, and specify a client
+        # (and make sure your define the encrypted environment credentials in gitlab
+        # to push to your storage locations of choice
+
+        - mkdir -p build && cp *.sif build
+        - mkdir -p build && cp Singularity* build
+
+        # Step 2. Take a look at "artifacts" below and add the paths you want added
+        # You can also add the entire build folder. You can also upload to storage
+        # clients defined by sregistry, here are some examples
+        # https://singularityhub.github.io/sregistry-cli/clients
+        # Environment variables must be defined in CI encrypted secrets/settings
+        # https://code.stanford.edu/help/ci/variables/README#variables).
+        #- /bin/bash build.sh --uri collection/container --cli google-storage Singularity
+        #- /bin/bash build.sh --uri collection/container --cli google-drive Singularity
+        #- /bin/bash build.sh --uri collection/container --cli globus Singularity
+        #- /bin/bash build.sh --uri collection/container --cli registry Singularity
+
+    # This is where you can save job artifacts
+    # https://docs.gitlab.com/ee/user/project/pipelines/job_artifacts.html
+    # You can specify the path to containers or the build folder to save.
+    # Don't forget to save your recipes too!
+    artifacts:
+        paths:
+            - build/Singularity.sif
+            - build/Singularity
+
diff --git a/.gitlabci/build.sh b/.gitlabci/build.sh
new file mode 100644
index 0000000..e605a4c
--- /dev/null
+++ b/.gitlabci/build.sh
@@ -0,0 +1,177 @@
+#!/bin/bash
+
+# build.sh will build a Singularity container. It's not overly complicated.
+#
+# USAGE: build.sh --uri collection-name/container-name --cli registry Singularity
+#        build.sh --uri collection-name/container-name --cli registry
+#        build.sh Singularity
+
+# Copyright (C) 2017-2020 Vanessa Sochat.
+
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or (at your
+# option) any later version.
+
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
+# License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+set -o errexit
+set -o nounset
+
+function usage() {
+
+    echo "USAGE: build [recipe] [options]"
+    echo ""
+    echo "OPTIONS:
+
+          Image Format
+              --uri   -u    if uploading, a uri to give to sregistry
+              --cli   -c    the sregistry client to use (if uploading)
+              --help  -h    show this help and exit
+              "
+}
+
+# --- Option processing --------------------------------------------------------
+
+uri=""
+cli=""
+tag=""
+
+while true; do
+    case ${1:-} in
+        -h|--help|help)
+            usage
+            exit 0
+        ;;
+        -u|--uri)
+            shift
+            uri="${1:-}"
+            shift
+        ;;
+        -t|--tag)
+            shift
+            tag="${1:-}"
+            shift
+        ;;
+        -c|--cli)
+            shift
+            cli="${1:-}"
+            shift
+        ;;
+        \?) printf "illegal option: -%s\n" "${1:-}" >&2
+            usage
+            exit 1
+        ;;
+        -*)
+            printf "illegal option: -%s\n" "${1:-}" >&2
+            usage
+            exit 1
+        ;;
+        *)
+            break;
+        ;;
+    esac
+done
+
+################################################################################
+### Recipe File ################################################################
+################################################################################
+
+
+if [ $# == 0 ] ; then
+    recipe="Singularity"
+else
+    recipe=$1
+fi
+
+echo ""
+echo "Image Recipe: ${recipe}"
+
+
+################################################################################
+### Storage Client #############################################################
+################################################################################
+
+is_valid_client () {
+  local e match="$1"
+  shift
+  for e; do [[ "$e" == "$match" ]] && return 0; done
+  return 1
+}
+
+# Test if client is valid
+
+clients=("google-storage" "registry" "globus" "dropbox" "google-drive")
+
+if [ "${cli}" != "" ]; then
+    is_valid_client "${cli}" "${clients[@]}"
+    if [ $? -ne 0 ]; then
+        echo "${cli} is not a valid choice! Choose from ${clients[@]}";
+        exit 1
+    fi
+    echo "Storage Client: ${cli}"
+else
+    echo "Storage Client: none"
+fi
+
+
+################################################################################
+### Build! #####################################################################
+################################################################################
+
+# Continue if the image recipe is found
+
+if [ -f "$recipe" ]; then
+
+    imagefile="${recipe}.sif"
+
+    echo "Creating $imagefile using $recipe..."
+    singularity build $imagefile $recipe
+
+    # If the image is successfully built, test it and upload (examples)
+  
+    if [ -f "${imagefile}" ]; then
+
+        # Example testing using run (you could also use test command)
+
+        echo "Testing the image... Marco!"
+        singularity exec $imagefile echo "Polo!"
+
+        # Example sregistry commands to push to endpoints
+
+        if [ "${cli}" != "" ]; then
+
+            # If the uri isn't provided, he gets a robot name
+            if [ "${uri}" == "" ]; then
+                uri=$(python -c "from sregistry.logger.namer import RobotNamer; bot=RobotNamer(); print(bot.generate())")
+            fi
+
+            # If a tag is provided, add to uri
+            if [ "${tag}" != "" ]; then
+                uri="${uri}:${tag}"
+            fi
+
+            echo "Pushing ${uri} to ${cli}://"
+            # singularity 
+            # echo "SREGISTRY_CLIENT=${cli} sregistry push --name ${uri} ${imagefile}"
+            # SREGISTRY_CLIENT="${cli}" sregistry push --name "${uri}" "${imagefile}"
+
+        else
+            echo "Skipping upload. Image $imagefile is finished!"
+        fi
+
+    fi
+
+else
+
+    echo "Singularity recipe ${recipe} not found!"
+    exit 1
+
+fi
+
-- 
GitLab