From 51e371254afd6cff310ee369ab577e3dc6959ddd Mon Sep 17 00:00:00 2001
From: Ralf Jung <post@ralfj.de>
Date: Sat, 24 Mar 2018 12:41:58 +0100
Subject: [PATCH] add main buildjob script

---
 ansi-colors.sh                  | 19 ++++++++++++
 buildjob                        | 53 +++++++++++++++++++++++++++++++++
 prepare-opam => prepare-opam.sh | 29 ++++++++++--------
 3 files changed, 88 insertions(+), 13 deletions(-)
 create mode 100644 ansi-colors.sh
 create mode 100755 buildjob
 rename prepare-opam => prepare-opam.sh (75%)
 mode change 100755 => 100644

diff --git a/ansi-colors.sh b/ansi-colors.sh
new file mode 100644
index 0000000..53f9144
--- /dev/null
+++ b/ansi-colors.sh
@@ -0,0 +1,19 @@
+# provide some variables to get colorful output
+
+DARKGRAY='\033[1;30m'
+RED='\033[0;31m'    
+LIGHTRED='\033[1;31m'
+GREEN='\033[0;32m'    
+YELLOW='\033[1;33m'
+BLUE='\033[0;34m'    
+PURPLE='\033[0;35m'    
+LIGHTPURPLE='\033[1;35m'
+CYAN='\033[0;36m'    
+WHITE='\033[1;37m'
+RESET='\033[0m'
+
+echo_color() {
+    # $1: the color
+    # $2: the string
+    echo "$1$2$RESET"
+}
diff --git a/buildjob b/buildjob
new file mode 100755
index 0000000..9914eff
--- /dev/null
+++ b/buildjob
@@ -0,0 +1,53 @@
+#!/bin/bash
+set -e
+#set -x
+
+## This runs a default build job.  The following variables are noteworthy:
+## - $OPAM_PINS:  Space-separated list of packages to pin in opam, in the format
+##   "PACKAGE KIND TARGET   PACKAGE KIND TARGET   ..."
+## - $VALIDATE: If non-empty, run `make validate`
+## - $OPAM_UPDATE_SECRET, $OPAM_PKG, $OPAM_PKG_BRANCH: IF $OPAM_PKG is
+##   non-empty, release this commit as a new package on opam when done.
+##   Requires the $OPAM_UPDATE_SECRET variable to be set.  This only happens if
+##   the current branch is $OPAM_PKG_BRANCH, or master if that variable is
+##   empty.
+
+# Parepare
+. ci/ansi-colors
+. ci/prepare-opam $OPAM_PINS
+env | egrep '^(CI_BUILD_REF|CI_RUNNER)' > build-env.txt
+
+# Build
+echo_color "$GREEN" "[buildjob] Perfoming build"
+time make -k -j$CPU_CORES TIMED=y 2>&1 | tee build-log.txt
+if fgrep Axiom build-log.txt >/dev/null; then exit 1; fi
+echo_color "$GREEN" "[buildjob] Build time summary"
+cat build-log.txt | egrep "[a-zA-Z0-9_/-]+ \((real|user): [0-9]" | tee build-time.txt
+
+# maybe validate
+if [[ -n "$VALIDATE" ]]; then
+    echo_color "$GREEN" "[buildjob] Performing validation"
+    make validate;
+fi
+
+# maybe create opam package
+if [[ -z "$OPAM_PKG_BRANCH" ]]; then
+    OPAM_PKG_BRANCH=master
+fi
+if [[ -n "$OPAM_PKG" && "$CI_COMMIT_REF_NAME" == "$OPAM_PKG_BRANCH" ]]; then
+    echo_color "$GREEN" "[buildjob] Releasing package on opam"
+    # determine package name prefix
+    if [[ "$CI_COMMIT_REF_NAME" == master ]]; then
+        OPAM_PKG_PREFIX=dev
+    else
+        OPAM_PKG_PREFIX="branch.$CI_COMMIT_REF_NAME"
+    fi
+    # Trigger opam updater
+    curl --fail -X POST -F "token=$OPAM_UPDATE_SECRET" -F "ref=master" \
+         -F "variables[REPO]=$CI_PROJECT_URL.git" \
+         -F "variables[REF]=$CI_COMMIT_REF_NAME" \
+         -F "variables[SHA]=$CI_COMMIT_SHA" \
+         -F "variables[NAME]=$OPAM_PKG" \
+         -F "variables[OPAM_PREFIX]=$OPAM_PKG_PREFIX" \
+         https://gitlab.mpi-sws.org/api/v4/projects/581/trigger/pipeline
+fi
diff --git a/prepare-opam b/prepare-opam.sh
old mode 100755
new mode 100644
similarity index 75%
rename from prepare-opam
rename to prepare-opam.sh
index bc51df8..c7ad6e0
--- a/prepare-opam
+++ b/prepare-opam.sh
@@ -1,6 +1,3 @@
-#!/bin/bash
-set -e
-set -x
 ## This script installs the build dependencies for CI builds.
 
 # Prepare OPAM configuration
@@ -9,10 +6,16 @@ export OPAMJOBS="$((2*$CPU_CORES))"
 export OPAM_EDITOR="$(which false)"
 
 # Make sure we got a good OPAM.
-test -d "$OPAMROOT" || (mkdir "$OPAMROOT" && opam init --no-setup -y)
+if test -d "$OPAMROOT"; then
+    echo_color "$GREEN" "[prepare-opam] Re-using cached opam root"
+else
+    echo_color "$YELLOW" "[prepare-opam] Creating new opam root"
+    mkdir "$OPAMROOT"
+    opam init --no-setup -y
+fi
 eval `opam conf env`
 
-# Make sure the pin for the builddep package is not stale.
+# Make sure the pin for the builddep package exists and is up-to-date
 make build-dep/opam
 
 # Update repositories
@@ -36,7 +39,7 @@ echo
 # that is not currently possible.
 
 # Install fixed versions of some dependencies.
-echo
+echo_color "$GREEN" "[prepare-opam] Processing pins"
 while (( "$#" )); do # while there are arguments left
     PACKAGE="$1" ; shift
     KIND="$1" ; shift
@@ -45,24 +48,24 @@ while (( "$#" )); do # while there are arguments left
     # Check if the pin is already set
     read -a PIN <<< $(opam pin list | (egrep "^$PACKAGE[. ]"))
     if [[ "${PIN[1]}" == "$KIND" && "${PIN[2]}" == "$VERSION" ]]; then
-        echo "[opam-ci] $PACKAGE already $KIND-pinned to $VERSION"
+        echo_color "$GREEN" "[prepare-opam] $PACKAGE already $KIND-pinned to $VERSION"
     else
-        echo "[opam-ci] $KIND-pinning $PACKAGE to $VERSION"
+        echo_color "$YELLOW" "[prepare-opam] $KIND-pinning $PACKAGE to $VERSION"
         opam pin add -y -k "$KIND" "$PACKAGE" "$VERSION"
     fi
 done
+echo
 
 # Upgrade cached things.
-echo
-echo "[opam-ci] Upgrading opam"
+echo_color "$GREEN" "[prepare-opam] Upgrading opam"
 opam upgrade -y --fixup && opam upgrade -y
+echo
 
 # Install build-dependencies.
-echo
-echo "[opam-ci] Installing build-dependencies"
+echo_color "$GREEN" "[prepare-opam] Installing build-dependencies"
 make build-dep OPAMFLAGS=-y
+echo
 
 # done
-set +x
 echo
 coqc -v
-- 
GitLab