From 0c214c0cfb1f0e750deec0c411f180c2ab561cec Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Wed, 13 Apr 2022 14:32:04 -0300 Subject: aux/ci/: Add --- aux/ci/ci-build.sh | 66 +++++++++++++++++++++++++++ aux/ci/git-post-receive.sh | 22 +++++++++ aux/ci/git-pre-push.sh | 22 +++++++++ aux/ci/report.sh | 109 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 219 insertions(+) create mode 100755 aux/ci/ci-build.sh create mode 100755 aux/ci/git-post-receive.sh create mode 100755 aux/ci/git-pre-push.sh create mode 100755 aux/ci/report.sh diff --git a/aux/ci/ci-build.sh b/aux/ci/ci-build.sh new file mode 100755 index 0000000..8d49779 --- /dev/null +++ b/aux/ci/ci-build.sh @@ -0,0 +1,66 @@ +#!/bin/sh +set -eux + +PROJECT="$1" +LOGS_DIR="$2" +SHA="$3" +FILENAME="$(date -Is)-$SHA.log" +LOGFILE="$LOGS_DIR/$FILENAME" + +mkdtemp() { + name="$(echo 'mkstemp(template)' | + m4 -D template="${TMPDIR:-/tmp}/m4-tmpname.")" + rm -f "$name" + mkdir "$name" + echo "$name" +} + +{ + echo "Starting CI job at: $(date -Is)" + + finish() { + STATUS="$?" + printf "\n\n>>> exit status was %s\n" "$STATUS" + echo "Finishing CI job at: $(date -Is)" + cd - + NOTE=$(cat <>>\n>>> CI logs added as Git note.\n>>>\n>>> Run status was %s\n>>>\n\n' \ + "$STATUS" + } + trap finish EXIT + + unset GIT_DIR + REMOTE="$PWD" + cd "$(mkdtemp)" + git clone "$REMOTE" . + git config --global user.email git@euandre.org + git config --global user.name 'EuAndreh CI' + + if [ -e aux/with-container ]; then + RUNNER='sh aux/with-container' + else + RUNNER='sh -c' + fi + + $RUNNER 'make clean dev-check' +} 2>&1 | tee "$LOGFILE" diff --git a/aux/ci/git-post-receive.sh b/aux/ci/git-post-receive.sh new file mode 100755 index 0000000..f813259 --- /dev/null +++ b/aux/ci/git-post-receive.sh @@ -0,0 +1,22 @@ +#!/bin/sh +set -eu + +for n in $(seq 0 $((GIT_PUSH_OPTION_COUNT - 1))); do + opt="$(eval "echo \$GIT_PUSH_OPTION_$n")" + if [ "$opt" = skip-ci ] || [ "$opt" = ci-skip ]; then + printf "\n'%s' option detected, not running ci-build.sh\n\n" \ + "$opt" + exit 0 + fi +done + +# shellcheck disable=2034 +read -r _oldrev SHA _refname + +PROJECT="$(basename "$PWD" | cut -d. -f1)" # remove .git suffix +LOGS_DIR="/opt/ci/$PROJECT/logs" +sh "/opt/ci/$PROJECT/ci-build.sh" "$PROJECT" "$LOGS_DIR" "$SHA" ||: + +echo 'To retrigger the build, run:' +echo "cd /opt/www/$TLD/git/repos/$PROJECT.git/" +echo "sh /opt/www/$TLD/git/ci/$PROJECT/ci-build.sh" "$PROJECT" "$LOGS_DIR" "$SHA" diff --git a/aux/ci/git-pre-push.sh b/aux/ci/git-pre-push.sh new file mode 100755 index 0000000..9883769 --- /dev/null +++ b/aux/ci/git-pre-push.sh @@ -0,0 +1,22 @@ +#!/bin/sh +set -eux + +TLD="$(cat aux/tld.txt)" +. aux/lib.sh + +PROJECT="$(basename "$PWD")" +LOGS_DIR="/opt/www/$TLD/git/ci/$PROJECT/logs" +REMOTE_GIT_DIR="/opt/www/$TLD/git/repos/$PROJECT.git" + +DESCRIPTION="$(mkstemp)" +if [ -f description ] +then + cp description "$DESCRIPTION" +else + git config euandreh.description > "$DESCRIPTION" +fi + +scp "$DESCRIPTION" "$TLD:$REMOTE_GIT_DIR/description" +ssh "$TLD" mkdir -p "$LOGS_DIR" +scp aux/ci/ci-build.sh "$TLD:$(dirname "$LOGS_DIR")/ci-build.sh" +scp aux/ci/git-post-receive.sh "$TLD:$REMOTE_GIT_DIR/hooks/post-receive" diff --git a/aux/ci/report.sh b/aux/ci/report.sh new file mode 100755 index 0000000..e900e26 --- /dev/null +++ b/aux/ci/report.sh @@ -0,0 +1,109 @@ +#!/bin/sh +set -eu + +TLD="$(cat aux/tld.txt)" +. aux/lib.sh + +while getopts 'n:o:' flag; do + case "$flag" in + n) + PROJECT="$OPTARG" + ;; + o) + OUTDIR="$OPTARG" + ;; + *) + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + +assert_arg() { + if [ -z "$1" ]; then + echo "Missing $2" >&2 + exit 2 + fi +} + +assert_arg "${PROJECT:-}" '-n PROJECT' +assert_arg "${OUTDIR:-}" '-o OUTDIR' + +PASS='✅' +FAIL='❌' + +mkdir -p "$OUTDIR/ci-logs" "$OUTDIR/ci-data" + +OUT="$(mkstemp)" +chmod 644 "$OUT" + +for c in $(git notes list | cut -d\ -f2); do + DATA="$(git notes --ref=refs/notes/ci-data show "$c")" + FILENAME="$(echo "$DATA" | cut -d\ -f2)" + echo "$DATA" > "$OUTDIR/ci-data/$FILENAME" + git notes --ref=refs/notes/ci-logs show "$c" \ + > "$OUTDIR/ci-logs/$FILENAME" +done + +{ + cat < + + + + + + + $PROJECT - CI logs + +EOF + + cat aux/workflow/style.css + + cat < + pre { + display: inline; + } + ol { + list-style-type: disc; + } + + + +
+

+ CI logs for + $PROJECT +

+
    +EOF +} > "$OUT" + +for f in $(find "$OUTDIR/ci-data/" -type f | LANG=C.UTF-8 sort -r); do + DATA="$(cat "$f")" + STATUS="$(echo "$DATA" | cut -d\ -f1)" + FILENAME="$(echo "$DATA" | cut -d\ -f2)" + + if [ "$STATUS" = 0 ]; then + STATUS_MARKER="$PASS" + else + STATUS_MARKER="$FAIL" + fi + + cat <> "$OUT" +
  1. + $STATUS_MARKER
    $FILENAME
    +
  2. +EOF +done + +cat <> "$OUT" +
+
+ + +EOF + +mv "$OUT" "$OUTDIR/ci.html" -- cgit v1.2.3