commit 0775b6dcf489acb431c1c14e1b638ae6c2f56f1f
parent 364a0c810162475d8fa27aac126e3d17589c6e3e
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Fri, 11 Sep 2026 22:38:20 +0530
parent 364a0c810162475d8fa27aac126e3d17589c6e3e
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Fri, 11 Sep 2026 22:38:20 +0530
Add minci handoff Jobfile runner
2 files changed, 376 insertions(+), 137 deletions(-)
M
|
233
+++++++++++++++++++++++++++++++++----------------------------------------------
A
|
280
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/docs/minci-handoff.md b/docs/minci-handoff.md @@ -1,196 +1,155 @@ -# minci-handoff design +# minci-handoff -`minci-handoff` is the next shape of the current `fpkgs-ci.sh` glue. It stays specific to minci as the report sink, but it is provider-neutral and project-neutral. +`minci-handoff` is the provider-neutral runner for minci. It accepts a small shell event from any git provider, sources one APKBUILD-like Jobfile, executes selected jobs, publishes artifacts, and reports job results to minci. -## Replacement map +## Event handoff format -Current foundationpkgs-specific pieces should move like this: +Thin adapters emit shell variables. They may use lowercase directly because the event is sourced into the Jobfile environment. -| Current | Replace with | Why | -| --- | --- | --- | -| `scripts/fpkgs-ci.sh` | standalone `minci-handoff` executable | Current script knows APKBUILD buckets and foundationpkgs artifact layout. | -| `FPKGS_*` env vars | server config + generic handoff event vars | The event format should be reusable across gitolite, webhooks, and manual runs. | -| Built-in `list_apkbuilds()` planner | `Jobfile` job function | The repo/server Jobfile decides what to build. | -| Built-in artifact path policy | `ARTIFACT_PATH`, `ARTIFACT_DIR`, and `ARTIFACT_URL` | Keeps publishing generic. | -| Built-in minci report per APKBUILD | minci reporter around each declared job | minci remains the reporting protocol, not the build planner. | -| `scripts/build.sh` / `functions.sh` | job implementation for APKBUILD projects | These stay useful, but should not live in the runner core. | -| `scripts/pacstall-install.sh` | optional job helper | Runtime APKBUILD → pacscript conversion stays project/tool-specific. | +```sh +event=git-push +repo=nply/foundationpkgs +repo_url=ssh://git@nply.org/nply/foundationpkgs.git +repo_path=/home/git/repositories/nply/foundationpkgs.git +ref=refs/heads/main +oldrev=0000000000000000000000000000000000000000 +newrev=... +actor=gitolite +``` + +Uppercase aliases (`REPO_PATH`, `REF`, `NEWREV`, etc.) are also accepted for hook adapters that prefer environment-style names. ## Server-level Jobfile first -Before requiring every repo to carry an `integ` branch, use one server-level Jobfile: +For now, use a common server Jobfile: ```txt /etc/minci-handoff/Jobfile ``` -This lets us work out runner semantics against `foundationpkgs` without forcing every repo to restructure. - -Later lookup order: - -1. server override: `/etc/minci-handoff/repos.d/<repo>.Jobfile` -2. repo config branch: `refs/heads/integ:.config/minci/Jobfile` -3. repo default branch: `.config/minci/Jobfile` -4. common fallback: `/etc/minci-handoff/Jobfile` - -For now, use only the common fallback. - -## Handoff event format - -Adapters produce capital shell variables, deliberately generic and without a `MINCI_` prefix: - -```sh -EVENT=git-push -REPO_NAME=nply/foundationpkgs -REPO_URL=ssh://git@nply.org/nply/foundationpkgs.git -REPO_PATH=/home/git/repositories/nply/foundationpkgs.git -REF=refs/heads/main -OLD_REV=0000000000000000000000000000000000000000 -NEW_REV=... -ACTOR=gitolite -``` +Later lookup order should be: -A gitolite adapter is just a translator from `post-receive` stdin into this event format. GitHub/GitLab/webhook adapters can produce the same format later. +1. `/etc/minci-handoff/repos.d/<repo>.Jobfile` +2. `refs/heads/integ:.config/minci/Jobfile` +3. default branch `.config/minci/Jobfile` +4. `/etc/minci-handoff/Jobfile` ## Jobfile format -A Jobfile is POSIX shell, APKBUILD-like: variables plus functions. - -Expected variables: - -```sh -PROJECT=foundationpkgs -TARGETS="native" -JOBS="packages smoke" -ARTIFACT_PATH="foundationpkgs" - -# Branch/ref routing. Left side is a shell case pattern matched against REF; -# right side is the jobs to run for that ref. -TRIGGER_MAP='refs/heads/main: packages -refs/heads/release/*: packages smoke -refs/heads/integ: selftest' -``` - -`TRIGGER_MAP` replaces a flat trigger list when branch-specific jobs are needed. The runner picks the first matching line unless the Jobfile sets: +A Jobfile is POSIX shell, borrowing APKBUILD conventions: lowercase variables, whitespace lists, function hooks, and `!option` negation. ```sh -TRIGGER_MODE=all -``` +project=foundationpkgs +targets="native" +artifact_path=foundationpkgs -With `TRIGGER_MODE=all`, every matching line contributes jobs. +triggers=" +refs/heads/main: packages +refs/heads/integ: selftest +refs/tags/v*: packages release +" -Runner-provided variables while executing a job: +default_options="report artifacts failfast" +options_selftest="!artifacts" +options_release="artifacts report !failfast" -```sh -CODE_DIR=/var/tmp/minci-handoff/checkouts/<rev>/code -CONFIG_DIR=/etc/minci-handoff -WORK_DIR=/var/tmp/minci-handoff/build/<job-key> -ARTIFACT_DIR=/var/tmp/minci-handoff/build/<job-key>/artifacts -ARTIFACT_URL=https://nply.org/dl/<ARTIFACT_PATH>/<TARGET>/<job-key>/ -TARGET=x86_64-unknown-linux-gnu -REF=refs/heads/main -OLD_REV=... -NEW_REV=... -REPO_NAME=nply/foundationpkgs -REPO_URL=... -REPO_PATH=... -ACTOR=... +depends_release="packages" ``` -Function contract: - -```sh -prepare() { :; } # optional, once per run -<job>() { ...; } # required for each name in selected jobs -<job>_artifacts() { # optional; default is find "$ARTIFACT_DIR" -type f - find "$ARTIFACT_DIR" -type f -} -<job>_inputs() { # optional; contributes to the job key - find . -type f | sort -} -cleanup() { :; } # optional -``` - -## Trigger map parsing - -The runner should parse `TRIGGER_MAP` as newline-separated routing rules: +`triggers` is newline-separated: ```txt <ref-pattern>: <job> [<job>...] ``` -Example: +The pattern is a shell `case` pattern matched against `ref`. Jobs run in the order they appear. If `depends_<job>` is set, those jobs must have already completed successfully before `<job>` runs. This gives Ant-like ordered execution by default, with explicit dependencies when the order is not enough. + +## Runner-provided variables + +These intentionally resemble APKBUILD variables and stay general enough for apps, packages, blogs, docs, and static sites: ```sh -TRIGGER_MAP='refs/heads/main: packages -refs/heads/docs/*: docs -refs/tags/v*: release packages' +srcdir # checked-out source/content tree +confdir # directory containing the selected Jobfile +workdir # per-job scratch directory +pkgdir # per-job staging directory +destdir # per-job artifact/output directory +artifact_url # public URL for this job's published artifacts +target # target triplet/environment +ref +oldrev +newrev +repo +repo_url +repo_path +actor ``` -Selection algorithm: +## Function contract ```sh -selected_jobs() { - printf '%s\n' "$TRIGGER_MAP" | while IFS= read -r line; do - pattern=${line%%:*} - jobs=${line#*:} - case "$REF" in - $pattern) - printf '%s\n' $jobs - [ "${TRIGGER_MODE:-first}" = all ] || break - ;; - esac - done +prepare() { :; } # optional, once per run +<job>() { ...; } # required for each selected job +<job>_inputs() { ...; } # optional, contributes to job key +<job>_artifacts() { # optional; default is find "$destdir" -type f + find "$destdir" -type f } +cleanup() { :; } # optional, once per run ``` -A flat default remains possible: +## Options -```sh -JOBS="build test" -TRIGGER_MAP='refs/heads/main: build test' -``` +Options are APKBUILD-like. `!foo` disables `foo`. -## Common server Jobfile for foundationpkgs +- `report`: submit this job to minci. +- `!report`: run without minci submission. +- `artifacts`: publish artifacts. +- `!artifacts`: keep logs only. +- `failfast`: stop the run on this job failure. +- `!failfast`: continue after this job failure. +- `allow_failure`: report the failure but treat the dependency as satisfied for later jobs. -This is the first target Jobfile to install at `/etc/minci-handoff/Jobfile`: +## Foundationpkgs server Jobfile ```sh -# POSIX sh -PROJECT=foundationpkgs -TARGETS="native" -ARTIFACT_PATH="foundationpkgs" -TRIGGER_MAP='refs/heads/main: packages' +project=foundationpkgs +targets="native" +artifact_path=foundationpkgs +triggers="refs/heads/main: packages" +default_options="report artifacts failfast" packages() { - cd "$CODE_DIR" + cd "$srcdir" find Linux Unix noarch any -name APKBUILD -type f | sort | while IFS= read -r apk; do pkg=$(awk -F= '$1 == "pkgname" { v = $2; gsub(/^"|"$/, "", v); print v; exit }' "$apk") [ -n "$pkg" ] || pkg=$(dirname "$apk" | sed 's#.*/##') - out="$ARTIFACT_DIR/$pkg" + out="$destdir/$pkg" mkdir -p "$out" - scripts/build.sh -o "$out" -t "$TARGET" "$apk" + scripts/build.sh -o "$out" -t "$target" "$apk" done } -packages_artifacts() { - find "$ARTIFACT_DIR" -type f -} - packages_inputs() { find Linux Unix noarch any scripts -type f | sort } + +packages_artifacts() { + find "$destdir" -type f +} ``` -## Build key rule +## Current implementation -`minci-handoff` should compute a job key from: +The bootstrap implementation lives at: -- event data: `REPO_NAME`, `REF`, `NEW_REV`, `TARGET` -- selected Jobfile content -- job name -- files declared by the Jobfile, if it provides `<job>_inputs()` -- build environment: `MAKEFLAGS`, `CFLAGS`, `CPPFLAGS`, `LDFLAGS`, `PKG_CONFIG_PATH`, `CC` -- tool versions visible to the runner +```txt +scripts/minci-handoff.sh +``` + +It is standalone POSIX shell and accepts: -That keeps rebuild criteria in config instead of runner internals. +```sh +scripts/minci-handoff.sh handle event.env +scripts/minci-handoff.sh run REPO_PATH REF NEWREV [OLDREV] +scripts/minci-handoff.sh post-receive REPO_PATH REPO +```
diff --git a/scripts/minci-handoff.sh b/scripts/minci-handoff.sh @@ -0,0 +1,280 @@ +#!/bin/sh +# minci-handoff.sh - provider-neutral handoff runner for minci. +# POSIX sh. Jobfile is APKBUILD-like: lowercase variables and shell functions. +set -eu + +SELF=$0 +SELF_DIR=$(cd "$(dirname "$SELF")" && pwd) +ROOT=$(cd "$SELF_DIR/.." && pwd) + +if [ -f "$SELF_DIR/functions.sh" ]; then + # shellcheck disable=SC1091 + . "$SELF_DIR/functions.sh" +else + msg() { printf '> %s\n' "$*" >&2; } + warning() { printf '! WARNING: %s\n' "$*" >&2; } + die() { printf '! %s\n' "$*" >&2; exit 1; } + guess_triplet() { uname -m | sed 's/x86_64/x86_64-unknown-linux-gnu/'; } +fi + +MINCI_HANDOFF_CONF=${MINCI_HANDOFF_CONF:-/etc/minci-handoff} +MINCI_HANDOFF_WORK=${MINCI_HANDOFF_WORK:-/var/tmp/minci-handoff} +MINCI_HANDOFF_DL_ROOT=${MINCI_HANDOFF_DL_ROOT:-/srv/http/dl.nply.org} +MINCI_HANDOFF_DL_URL=${MINCI_HANDOFF_DL_URL:-https://nply.org/dl} +MINCI_HANDOFF_CONFIG_REF=${MINCI_HANDOFF_CONFIG_REF:-refs/heads/integ} +MINCI_HANDOFF_JOBFILE=${MINCI_HANDOFF_JOBFILE:-} + +usage() { + cat >&2 <<EOF +usage: $SELF handle EVENTFILE | run REPO_PATH REF NEWREV [OLDREV] | post-receive REPO_PATH REPO +EOF + exit 1 +} + +md5_text() { openssl dgst -md5 -hex | sed 's!^[^=]*= !!'; } +md5_file() { openssl dgst -md5 -hex "$1" | sed 's!^[^=]*= !!'; } +sha256_file() { + if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}'; + else shasum -a 256 "$1" | awk '{print $1}'; fi +} + +field() { + name=$1 + eval "printf '%s\n' \${$name:-}" +} + +native_target() { guess_triplet; } +expand_targets() { + for t in ${targets:-native}; do + case "$t" in native) native_target ;; *) printf '%s\n' "$t" ;; esac + done +} + +selected_jobs() { + [ -n "${triggers:-}" ] || return 0 + printf '%s\n' "$triggers" | while IFS= read -r line; do + case "$line" in ''|'#'*) continue ;; esac + pattern=${line%%:*} + jobs=${line#*:} + pattern=$(printf '%s' "$pattern" | sed -e 's/^[ ]*//' -e 's/[ ]*$//') + case "$ref" in + $pattern) + printf '%s\n' $jobs + [ "${trigger_mode:-first}" = all ] || break + ;; + esac + done +} + +has_opt() { + opt=$1 + shift + present=1 + for o in "$@"; do + case "$o" in + "$opt") present=0 ;; + "!$opt") present=1 ;; + esac + done + return "$present" +} + +job_options() { + job=$1 + eval "extra=\${options_$job:-}" + printf '%s\n' ${default_options:-report artifacts failfast} $extra +} + +job_key() { + job=$1 + { + printf 'repo=%s\nref=%s\noldrev=%s\nnewrev=%s\ntarget=%s\njob=%s\n' \ + "$repo" "$ref" "$oldrev" "$newrev" "$target" "$job" + [ -f "$jobfile" ] && printf 'jobfile=%s\n' "$(sha256_file "$jobfile")" + if command -v "${job}_inputs" >/dev/null 2>&1; then + (cd "$srcdir" && "${job}_inputs") | while IFS= read -r f; do + [ -f "$srcdir/$f" ] && printf 'input %s %s\n' "$f" "$(sha256_file "$srcdir/$f")" + done + fi + for v in MAKEFLAGS CFLAGS CPPFLAGS LDFLAGS PKG_CONFIG_PATH CC; do + eval val=\${$v:-} + printf '%s=%s\n' "$v" "$val" + done + (command -v cc >/dev/null 2>&1 && cc --version 2>/dev/null | sed 1q) || true + (command -v make >/dev/null 2>&1 && make --version 2>/dev/null | sed 1q) || true + (command -v bmake >/dev/null 2>&1 && bmake -V MAKE_VERSION 2>/dev/null) || true + } | if command -v sha256sum >/dev/null 2>&1; then sha256sum | awk '{print $1}'; else shasum -a 256 | awk '{print $1}'; fi +} + +checkout() { + mkdir -p "$MINCI_HANDOFF_WORK/checkouts" + srcdir=$MINCI_HANDOFF_WORK/checkouts/$newrev/src + if [ ! -d "$srcdir/.git" ]; then + rm -rf "$MINCI_HANDOFF_WORK/checkouts/$newrev" + mkdir -p "$MINCI_HANDOFF_WORK/checkouts/$newrev" + git clone -q "$repo_path" "$srcdir" + (cd "$srcdir" && git checkout -q "$newrev") + fi +} + +find_jobfile() { + if [ -n "$MINCI_HANDOFF_JOBFILE" ]; then + jobfile=$MINCI_HANDOFF_JOBFILE + elif [ -f "$MINCI_HANDOFF_CONF/repos.d/$repo.Jobfile" ]; then + jobfile=$MINCI_HANDOFF_CONF/repos.d/$repo.Jobfile + elif [ -f "$MINCI_HANDOFF_CONF/Jobfile" ]; then + jobfile=$MINCI_HANDOFF_CONF/Jobfile + else + die "no Jobfile for $repo" + fi + confdir=$(cd "$(dirname "$jobfile")" && pwd) +} + +report_minci() { + job=$1 + log=$2 + ok=$3 + [ -n "${minci_url:-}" ] || return 0 + [ -n "${minci_key:-}" ] || return 0 + [ -n "${minci_secret:-}" ] || return 0 + now=$(date +%s) + start=$now; env=$now; depend=$now + if [ "$ok" = 1 ]; then build=$now; test=$now; install=$now; distcheck=$now; else build=0; test=0; install=0; distcheck=0; fi + loghash=$(md5_file "$log") + unamem=$(uname -m); unamen=$target; unamer=$(uname -r); unames=$(uname -s); unamev=$(uname -v) + p=${project:-$repo} + case "$p" in *:*) project_name=$p/$job ;; *) project_name=$p:$job ;; esac + query="project-name=$project_name&report-build=$build&report-distcheck=$distcheck&report-env=$env&report-fetchhead=$newrev&report-depend=$depend&report-install=$install&report-log=$loghash&report-start=$start&report-test=$test&report-unamem=$unamem&report-unamen=$unamen&report-unamer=$unamer&report-unames=$unames&report-unamev=$unamev&user-apisecret=$minci_secret" + sig=$(printf '%s' "$query" | md5_text) + curl -sS -F "report-log=@$log" -F "project-name=$project_name" \ + -F "report-start=$start" -F "report-env=$env" -F "report-depend=$depend" \ + -F "report-build=$build" -F "report-test=$test" -F "report-install=$install" \ + -F "report-distcheck=$distcheck" -F "report-unamem=$unamem" \ + -F "report-unamen=$unamen" -F "report-unamer=$unamer" -F "report-unames=$unames" \ + -F "report-unamev=$unamev" -F "report-fetchhead=$newrev" \ + -F "user-apikey=$minci_key" -F "signature=$sig" "$minci_url" >/dev/null || \ + warning "minci report failed: $project_name" +} + +publish_job() { + job=$1 + key=$2 + out=$MINCI_HANDOFF_DL_ROOT/${artifact_path:-$repo}/$target/$newrev/$job/$key + tmp=$out.tmp.$$ + rm -rf "$tmp" + mkdir -p "$tmp" + if command -v "${job}_artifacts" >/dev/null 2>&1; then + artifacts=$workdir/artifacts.list + "${job}_artifacts" >"$artifacts" + else + find "$destdir" -type f >"$workdir/artifacts.list" + artifacts=$workdir/artifacts.list + fi + while IFS= read -r f; do + [ -f "$f" ] || continue + cp "$f" "$tmp/" + done <"$artifacts" + cp "$log" "$tmp/build.log" + { + printf 'project=%s\njob=%s\nrepo=%s\nref=%s\nnewrev=%s\ntarget=%s\nkey=%s\n' \ + "${project:-$repo}" "$job" "$repo" "$ref" "$newrev" "$target" "$key" + printf 'artifact_url=%s/%s/%s/%s/%s/%s/\n' "$MINCI_HANDOFF_DL_URL" "${artifact_path:-$repo}" "$target" "$newrev" "$job" "$key" + } >"$tmp/manifest" + mkdir -p "$(dirname "$out")" + rm -rf "$out" + mv "$tmp" "$out" +} + +run_job() { + job=$1 + key=$(job_key "$job") + workdir=$MINCI_HANDOFF_WORK/build/$key + pkgdir=$workdir/pkg + destdir=$workdir/artifacts + log=$workdir/build.log + artifact_url=$MINCI_HANDOFF_DL_URL/${artifact_path:-$repo}/$target/$newrev/$job/$key/ + export srcdir confdir workdir pkgdir destdir artifact_url target ref oldrev newrev repo repo_url repo_path actor + rm -rf "$workdir" + mkdir -p "$pkgdir" "$destdir" + opts=$(job_options "$job") + msg "job $job target $target" + if "$job" >"$log" 2>&1; then + if has_opt artifacts $opts; then publish_job "$job" "$key"; fi + if has_opt report $opts; then report_minci "$job" "$log" 1; fi + return 0 + fi + if has_opt report $opts; then report_minci "$job" "$log" 0; fi + warning "job failed: $job (see $log)" + has_opt allow_failure $opts && return 0 + return 1 +} + +run_all() { + checkout + find_jobfile + # shellcheck disable=SC1090 + . "$jobfile" + jobs=$(selected_jobs) + [ -n "$jobs" ] || { msg "no jobs selected for $ref"; return 0; } + if command -v prepare >/dev/null 2>&1; then prepare; fi + for target in $(expand_targets); do + done_jobs= + for job in $jobs; do + eval "deps=\${depends_$job:-}" + missing= + for dep in $deps; do + list_has "$dep" $done_jobs || missing="$missing $dep" + done + if [ -n "$missing" ]; then + log=$MINCI_HANDOFF_WORK/skipped-$job-$$.log + mkdir -p "$MINCI_HANDOFF_WORK" + printf 'skipped: missing job dependencies:%s\n' "$missing" >"$log" + report_minci "$job" "$log" 0 + warning "skipped $job: missing dependencies:$missing" + continue + fi + if run_job "$job"; then + done_jobs="$done_jobs $job" + else + opts=$(job_options "$job") + has_opt failfast $opts && return 1 + fi + done + done + if command -v cleanup >/dev/null 2>&1; then cleanup; fi +} + +handle() { + # shellcheck disable=SC1090 + . "$1" + repo=${repo:-${REPO:-}} + repo_path=${repo_path:-${REPO_PATH:-}} + repo_url=${repo_url:-${REPO_URL:-$repo_path}} + ref=${ref:-${REF:-}} + oldrev=${oldrev:-${OLDREV:-}} + newrev=${newrev:-${NEWREV:-}} + actor=${actor:-${ACTOR:-}} + [ -n "$repo" ] || repo=$(basename "$repo_path" .git) + [ -n "$repo_path" ] || die "repo_path required" + [ -n "$ref" ] || die "ref required" + [ -n "$newrev" ] || die "newrev required" + oldrev=${oldrev:-0000000000000000000000000000000000000000} + run_all +} + +cmd=${1:-} +case "$cmd" in +handle) [ $# -eq 2 ] || usage; handle "$2" ;; +run) + [ $# -ge 4 ] || usage + repo_path=$2; ref=$3; newrev=$4; oldrev=${5:-0000000000000000000000000000000000000000} + repo=$(basename "$repo_path" .git); repo_url=$repo_path; actor=manual + run_all + ;; +post-receive) + [ $# -eq 3 ] || usage + repo_path=$2; repo=$3; repo_url=$repo_path; actor=gitolite + while read -r oldrev newrev ref; do run_all; done + ;; +*) usage ;; +esac
: nply/foundationpkgs