mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-28 19:00:53 +08:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9659c8ae1 | ||
|
|
869b44a327 | ||
|
|
91da8341cd |
@@ -19,6 +19,11 @@ POSTGRES_PASSWORD=change-me-please
|
||||
POSTGRES_DB=artex
|
||||
# Prebuilt GHCR image tag for docker-compose.ghcr.yml. Unset uses latest.
|
||||
# ARTEX_IMAGE_TAG=latest
|
||||
# Build identity shown in the UI and the startup banner. update.sh refreshes both
|
||||
# from git before a rebuild; a manual `docker compose build` without them reports
|
||||
# "dev". ARTEX_REVISION is the full commit, ARTEX_VERSION the describe tag/hash.
|
||||
# ARTEX_VERSION=
|
||||
# ARTEX_REVISION=
|
||||
|
||||
# LLM (fill one or both; you can also leave empty and configure in the UI)
|
||||
ANTHROPIC_API_KEY=
|
||||
|
||||
@@ -196,7 +196,9 @@ jobs:
|
||||
push: ${{ github.event_name == 'push' }}
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
build-args: ARTEX_VERSION=${{ github.ref_name }}
|
||||
build-args: |
|
||||
ARTEX_VERSION=${{ github.ref_name }}
|
||||
ARTEX_REVISION=${{ github.sha }}
|
||||
provenance: true
|
||||
sbom: true
|
||||
cache-from: type=gha
|
||||
|
||||
@@ -34,6 +34,7 @@ COPY . .
|
||||
# Reuse the frontend built above; build.sh skips its own frontend step.
|
||||
COPY --from=web /src/web/out ./server/webui/dist
|
||||
ARG ARTEX_VERSION=dev
|
||||
ARG ARTEX_REVISION=
|
||||
ARG TARGETOS
|
||||
ARG TARGETARCH
|
||||
# Same flags as the release binaries job (ARTEX_COMPRESS=0, -trimpath,
|
||||
@@ -43,6 +44,7 @@ RUN ARTEX_SKIP_FRONTEND=1 \
|
||||
ARTEX_COMPRESS=0 \
|
||||
ARTEX_OUTPUT=/out/artex \
|
||||
ARTEX_BUILD_VERSION="${ARTEX_VERSION}" \
|
||||
ARTEX_REVISION="${ARTEX_REVISION}" \
|
||||
bash ./build.sh --target "${TARGETOS:-linux}/${TARGETARCH:-amd64}"
|
||||
|
||||
# ------------------------------------------------------- third-party tools ---
|
||||
@@ -160,6 +162,11 @@ RUN set -eux; \
|
||||
# ---------------------------------------------------------------- runtime ---
|
||||
FROM python:3.12-slim-bookworm@sha256:935c3c5b12357a7e10caa9c1e6ca5692344d97cf229832ef584be2cec9fc03df
|
||||
ARG TARGETARCH
|
||||
ARG ARTEX_VERSION=dev
|
||||
ARG ARTEX_REVISION=
|
||||
LABEL org.opencontainers.image.title="ARTEX" \
|
||||
org.opencontainers.image.version="${ARTEX_VERSION}" \
|
||||
org.opencontainers.image.revision="${ARTEX_REVISION}"
|
||||
# Common tools: ripgrep / curl / vim, plus a set of recon staples (add/remove as needed).
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
ca-certificates ripgrep curl wget vim git jq unzip xz-utils \
|
||||
|
||||
@@ -99,6 +99,14 @@ if [ -z "${ARTEX_BUILD_VERSION:-}" ]; then
|
||||
fi
|
||||
# Release tags are commonly passed with a v prefix; keep the binary version consistent.
|
||||
ARTEX_BUILD_VERSION="${ARTEX_BUILD_VERSION#v}"
|
||||
# Source revision, shown by /health and the UI next to the version. Docker builds
|
||||
# pass it explicitly because the build context has no .git; a local build derives
|
||||
# it from the checkout.
|
||||
ARTEX_REVISION="${ARTEX_REVISION:-}"
|
||||
if [ -z "$ARTEX_REVISION" ] && command -v git >/dev/null 2>&1 \
|
||||
&& git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
ARTEX_REVISION="$(git rev-parse HEAD 2>/dev/null || true)"
|
||||
fi
|
||||
ARTEX_OUTPUT_DIR="${ARTEX_OUTPUT_DIR:-dist}"
|
||||
ARTEX_PACKAGE_DIR="${ARTEX_PACKAGE_DIR:-$ARTEX_OUTPUT_DIR}"
|
||||
ARTEX_UPX_ARGS="${ARTEX_UPX_ARGS:---best --lzma}"
|
||||
@@ -213,7 +221,9 @@ build_target() {
|
||||
fi
|
||||
mkdir -p "$(dirname "$output")"
|
||||
|
||||
info "Compiling ${goos}/${goarch}, version ${ARTEX_BUILD_VERSION}"
|
||||
info "Compiling ${goos}/${goarch}, version ${ARTEX_BUILD_VERSION}${ARTEX_REVISION:+ (${ARTEX_REVISION:0:12})}"
|
||||
LDFLAGS="-s -w -buildid= -X main.version=${ARTEX_BUILD_VERSION}"
|
||||
[ -n "${ARTEX_REVISION:-}" ] && LDFLAGS="${LDFLAGS} -X main.revision=${ARTEX_REVISION}"
|
||||
GOSUMDB="$ARTEX_GOSUMDB" \
|
||||
CGO_ENABLED=0 \
|
||||
GOOS="$goos" \
|
||||
@@ -221,7 +231,7 @@ build_target() {
|
||||
go build \
|
||||
-tags embedui \
|
||||
-trimpath \
|
||||
-ldflags "-s -w -buildid= -X main.version=${ARTEX_BUILD_VERSION}" \
|
||||
-ldflags "${LDFLAGS}" \
|
||||
-o "$output" \
|
||||
./cmd/artex
|
||||
|
||||
|
||||
+7
-2
@@ -61,6 +61,10 @@ func logConfigSnapshot(s configSnapshot) {
|
||||
// -ldflags "-X main.version=<tag>". Defaults to "dev" for local builds.
|
||||
var version = "dev"
|
||||
|
||||
// revision is the full source commit, injected via
|
||||
// -ldflags "-X main.revision=<sha>". Empty when the build had no git context.
|
||||
var revision = ""
|
||||
|
||||
const banner = `
|
||||
_ ____ _____ _______ __
|
||||
/ \ | _ \_ _| ____\ \/ /
|
||||
@@ -117,9 +121,10 @@ func run() int {
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
// hand the build version to the server package so GET /api/health can report it
|
||||
// to the frontend top bar.
|
||||
// hand the build version and source revision to the server package so
|
||||
// GET /api/health can report them to the frontend top bar.
|
||||
server.BuildVersion = version
|
||||
server.BuildCommit = revision
|
||||
|
||||
printBanner(*addr)
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ services:
|
||||
context: .
|
||||
args:
|
||||
ARTEX_VERSION: ${ARTEX_VERSION:-dev}
|
||||
ARTEX_REVISION: ${ARTEX_REVISION:-}
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
image: artex:local
|
||||
|
||||
+40
-2
@@ -9,6 +9,40 @@ warn(){ printf '\033[33m[!]\033[0m %s\n' "$*"; }
|
||||
die(){ printf '\033[31m[x]\033[0m %s\n' "$*" >&2; exit 1; }
|
||||
ask(){ local p="$1" d="${2:-}" a; read -rp "$p${d:+ [$d]}: " a; echo "${a:-$d}"; }
|
||||
rand(){ head -c 18 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 24; }
|
||||
|
||||
# ── Build identity: keep .env and the binary in step with the checkout ─────────
|
||||
BUILD_VERSION="dev"
|
||||
BUILD_REVISION=""
|
||||
|
||||
# set_env replaces KEY=... in .env (or appends it) without touching other lines.
|
||||
set_env(){
|
||||
local key="$1" value="$2" tmp
|
||||
tmp="$(mktemp)"
|
||||
awk -v k="$key" -v v="$value" '
|
||||
$0 ~ "^" k "=" { print k "=" v; found=1; next }
|
||||
{ print }
|
||||
END { if (!found) print k "=" v }
|
||||
' .env > "$tmp"
|
||||
chmod --reference=.env "$tmp" 2>/dev/null || true
|
||||
mv "$tmp" .env
|
||||
}
|
||||
|
||||
# stamp_version records the commit the image or binary is built from. Without it
|
||||
# docker-compose.yml passes ARTEX_VERSION=dev and every local install reports the
|
||||
# same meaningless version in the UI and the startup banner.
|
||||
stamp_version(){
|
||||
if ! command -v git >/dev/null 2>&1 || ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
warn "not a git working copy: leaving ARTEX_VERSION in .env unchanged"
|
||||
return 0
|
||||
fi
|
||||
BUILD_VERSION="$(git describe --tags --always --dirty 2>/dev/null || git rev-parse --short HEAD)"
|
||||
BUILD_REVISION="$(git rev-parse HEAD 2>/dev/null || true)"
|
||||
if [ -f .env ]; then
|
||||
set_env ARTEX_VERSION "$BUILD_VERSION"
|
||||
[ -n "$BUILD_REVISION" ] && set_env ARTEX_REVISION "$BUILD_REVISION"
|
||||
fi
|
||||
ok "Build identity: ${BUILD_VERSION}${BUILD_REVISION:+ (${BUILD_REVISION:0:12})}"
|
||||
}
|
||||
# json_str prints $1 as a JSON string literal: backslash, double quote and the
|
||||
# control characters read -r can return are escaped.
|
||||
json_str(){
|
||||
@@ -121,6 +155,7 @@ install_docker(){
|
||||
else
|
||||
info "reusing existing .env"
|
||||
fi
|
||||
stamp_version
|
||||
info "building the image from this checkout and starting..."
|
||||
docker compose up -d --build
|
||||
ok "started → http://localhost:8787"
|
||||
@@ -168,6 +203,9 @@ JSON
|
||||
# go environment check
|
||||
command -v go >/dev/null 2>&1 || die "Go not detected; please install Go (>=1.26) first: https://go.dev/dl/"
|
||||
ok "Go: $(go version)"
|
||||
stamp_version
|
||||
LDFLAGS="-X main.version=${BUILD_VERSION}"
|
||||
[ -n "${BUILD_REVISION}" ] && LDFLAGS="${LDFLAGS} -X main.revision=${BUILD_REVISION}"
|
||||
|
||||
# embedding the frontend requires node to produce the static assets
|
||||
if command -v npm >/dev/null 2>&1; then
|
||||
@@ -175,10 +213,10 @@ JSON
|
||||
( cd web && npm ci --ignore-scripts && npm run build:static )
|
||||
rm -rf server/webui/dist && mkdir -p server/webui && cp -r web/out server/webui/dist
|
||||
info "compiling embedded single binary..."
|
||||
CGO_ENABLED=0 go build -tags embedui -trimpath -o artex ./cmd/artex
|
||||
CGO_ENABLED=0 go build -tags embedui -trimpath -ldflags "${LDFLAGS}" -o artex ./cmd/artex
|
||||
else
|
||||
warn "npm not detected: building a backend WITHOUT the embedded frontend (run npm run dev separately for the frontend)"
|
||||
CGO_ENABLED=0 go build -o artex ./cmd/artex
|
||||
CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o artex ./cmd/artex
|
||||
fi
|
||||
ok "build complete → ./artex"
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package server
|
||||
|
||||
import "runtime/debug"
|
||||
|
||||
// BuildCommit is the full source commit, injected from cmd/artex (which gets it
|
||||
// from -ldflags "-X main.revision=<sha>"). Empty when the build had no git
|
||||
// context; currentBuildInfo falls back to the VCS stamp Go embeds for local
|
||||
// builds.
|
||||
var BuildCommit = ""
|
||||
|
||||
// buildInfo is the build identity reported by GET /api/health. Version is the
|
||||
// value shown in the UI top bar; Commit lets "dev" builds still name their
|
||||
// source revision.
|
||||
type buildInfo struct {
|
||||
Version string `json:"version"`
|
||||
Commit string `json:"commit,omitempty"`
|
||||
GoVersion string `json:"go_version,omitempty"`
|
||||
BuildTime string `json:"build_time,omitempty"`
|
||||
Modified bool `json:"modified,omitempty"`
|
||||
}
|
||||
|
||||
func currentBuildInfo(version, commit string) buildInfo {
|
||||
bi := buildInfo{Version: version, Commit: commit}
|
||||
if info, ok := debug.ReadBuildInfo(); ok {
|
||||
bi.GoVersion = info.GoVersion
|
||||
for _, s := range info.Settings {
|
||||
switch s.Key {
|
||||
case "vcs.revision":
|
||||
if bi.Commit == "" {
|
||||
bi.Commit = s.Value
|
||||
}
|
||||
case "vcs.time":
|
||||
bi.BuildTime = s.Value
|
||||
case "vcs.modified":
|
||||
bi.Modified = s.Value == "true"
|
||||
}
|
||||
}
|
||||
}
|
||||
return bi
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package server
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCurrentBuildInfoUsesInjectedIdentity(t *testing.T) {
|
||||
bi := currentBuildInfo("main-test", "0123456789abcdef")
|
||||
if bi.Version != "main-test" {
|
||||
t.Fatalf("Version = %q, want main-test", bi.Version)
|
||||
}
|
||||
if bi.Commit != "0123456789abcdef" {
|
||||
t.Fatalf("Commit = %q, want the injected revision", bi.Commit)
|
||||
}
|
||||
if bi.GoVersion == "" {
|
||||
t.Fatal("GoVersion is empty; debug.ReadBuildInfo should report the toolchain")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentBuildInfoDevWithoutRevision(t *testing.T) {
|
||||
bi := currentBuildInfo("dev", "")
|
||||
if bi.Version != "dev" {
|
||||
t.Fatalf("Version = %q, want dev", bi.Version)
|
||||
}
|
||||
// A test binary has no VCS stamp, so the empty commit stays empty rather
|
||||
// than turning into a misleading value.
|
||||
if bi.Commit != "" {
|
||||
t.Fatalf("Commit = %q, want empty when the build has no revision", bi.Commit)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1136,7 +1136,7 @@ func (s *Server) health(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, 200, map[string]any{"ok": true})
|
||||
return
|
||||
}
|
||||
out := map[string]any{"ok": true, "service": "artex", "version": BuildVersion}
|
||||
out := map[string]any{"ok": true, "service": "artex", "version": BuildVersion, "build": currentBuildInfo(BuildVersion, BuildCommit)}
|
||||
// The web UI shows a banner on every page for a degraded isolation (isolation.go).
|
||||
if s.isolation.Mode != "" {
|
||||
out["isolation"] = s.isolation
|
||||
|
||||
@@ -12,21 +12,56 @@ warn(){ printf '\033[33m[!]\033[0m %s\n' "$*"; }
|
||||
die(){ printf '\033[31m[x]\033[0m %s\n' "$*" >&2; exit 1; }
|
||||
ask(){ local p="$1" d="${2:-}" a; read -rp "$p${d:+ [$d]}: " a; echo "${a:-$d}"; }
|
||||
|
||||
# ── Build identity: keep .env and the binary in step with the checkout ─────────
|
||||
BUILD_VERSION="dev"
|
||||
BUILD_REVISION=""
|
||||
|
||||
# set_env replaces KEY=... in .env (or appends it) without touching other lines.
|
||||
set_env(){
|
||||
local key="$1" value="$2" tmp
|
||||
tmp="$(mktemp)"
|
||||
awk -v k="$key" -v v="$value" '
|
||||
$0 ~ "^" k "=" { print k "=" v; found=1; next }
|
||||
{ print }
|
||||
END { if (!found) print k "=" v }
|
||||
' .env > "$tmp"
|
||||
chmod --reference=.env "$tmp" 2>/dev/null || true
|
||||
mv "$tmp" .env
|
||||
}
|
||||
|
||||
# stamp_version records the commit the image is about to be built from. Without
|
||||
# it docker-compose.yml passes ARTEX_VERSION=dev and every local install reports
|
||||
# the same meaningless version in the UI and the startup banner.
|
||||
stamp_version(){
|
||||
if ! command -v git >/dev/null 2>&1 || ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
warn "not a git working copy: leaving ARTEX_VERSION in .env unchanged"
|
||||
return 0
|
||||
fi
|
||||
BUILD_VERSION="$(git describe --tags --always --dirty 2>/dev/null || git rev-parse --short HEAD)"
|
||||
BUILD_REVISION="$(git rev-parse HEAD 2>/dev/null || true)"
|
||||
if [ -f .env ]; then
|
||||
set_env ARTEX_VERSION "$BUILD_VERSION"
|
||||
[ -n "$BUILD_REVISION" ] && set_env ARTEX_REVISION "$BUILD_REVISION"
|
||||
fi
|
||||
ok "Build identity: ${BUILD_VERSION}${BUILD_REVISION:+ (${BUILD_REVISION:0:12})}"
|
||||
}
|
||||
|
||||
# ── Optional: sync the repo to the latest code (compose/scripts/local build sources all update through it) ───────
|
||||
sync_repo(){
|
||||
[ -d .git ] && command -v git >/dev/null 2>&1 || { warn "not a git working copy, skipping git pull"; return; }
|
||||
[ "$(ask 'Pull the latest code (git pull --ff-only)? (y/n)' y)" = y ] || return
|
||||
if ! git pull --ff-only; then
|
||||
# Continuing would rebuild the OLD tree and still print "Build complete":
|
||||
# exactly the silent stale upgrade this script exists to prevent.
|
||||
cat >&2 <<'RECOVER'
|
||||
[ -d .git ] && command -v git >/dev/null 2>&1 || { warn "not a git working copy, skipping git pull"; return 0; }
|
||||
if [ "$(ask 'Pull the latest code (git pull --ff-only)? (y/n)' y)" = y ]; then
|
||||
if ! git pull --ff-only; then
|
||||
# Continuing would rebuild the OLD tree and still print "Build complete":
|
||||
# exactly the silent stale upgrade this script exists to prevent.
|
||||
cat >&2 <<'RECOVER'
|
||||
[!] git pull could not fast-forward. This happens when the checkout has local
|
||||
changes, or when it was cloned before the history rewrite of 2026-10-23.
|
||||
Refusing to rebuild stale code. Recover, then rerun this script:
|
||||
keep your changes: git stash && git fetch origin && git reset --hard origin/main && git stash pop
|
||||
discard them: git fetch origin && git reset --hard origin/main
|
||||
RECOVER
|
||||
die "aborting: nothing was rebuilt"
|
||||
die "aborting: nothing was rebuilt"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -55,16 +90,18 @@ update_local(){
|
||||
command -v go >/dev/null 2>&1 || die "Go (>=1.26) not detected: https://go.dev/dl/"
|
||||
[ -f config.json ] || warn "config.json not found — use ./install.sh instead for a first-time deployment"
|
||||
ok "Go: $(go version)"
|
||||
LDFLAGS="-X main.version=${BUILD_VERSION}"
|
||||
[ -n "${BUILD_REVISION}" ] && LDFLAGS="${LDFLAGS} -X main.revision=${BUILD_REVISION}"
|
||||
|
||||
if command -v npm >/dev/null 2>&1; then
|
||||
info "Rebuilding frontend static assets…"
|
||||
( cd web && npm ci --ignore-scripts && npm run build:static )
|
||||
rm -rf server/webui/dist && mkdir -p server/webui && cp -r web/out server/webui/dist
|
||||
info "Recompiling the embedded single binary…"
|
||||
CGO_ENABLED=0 go build -tags embedui -trimpath -o artex ./cmd/artex
|
||||
info "Recompiling the embedded single binary (version ${BUILD_VERSION})…"
|
||||
CGO_ENABLED=0 go build -tags embedui -trimpath -ldflags "${LDFLAGS}" -o artex ./cmd/artex
|
||||
else
|
||||
warn "npm not detected: building a backend **without the embedded frontend** (run the frontend separately with npm run dev)"
|
||||
CGO_ENABLED=0 go build -o artex ./cmd/artex
|
||||
CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o artex ./cmd/artex
|
||||
fi
|
||||
ok "Build complete → ./artex"
|
||||
warn "Restart the running artex process to take effect (schema migrates automatically on restart)"
|
||||
@@ -76,7 +113,7 @@ echo " 1) Docker update (pull new image and rebuild)"
|
||||
echo " 2) Local update (rebuild with go)"
|
||||
echo "=============================="
|
||||
case "$(ask 'Choose' 1)" in
|
||||
1) sync_repo; update_docker ;;
|
||||
2) sync_repo; update_local ;;
|
||||
1) sync_repo; stamp_version; update_docker ;;
|
||||
2) sync_repo; stamp_version; update_local ;;
|
||||
*) die "invalid choice" ;;
|
||||
esac
|
||||
|
||||
@@ -50,7 +50,11 @@ export function MainContent({ children }: { children: ReactNode }) {
|
||||
api
|
||||
.health()
|
||||
.then((h) => {
|
||||
setVersion((h.version ?? "").replace(/^v(?=\d)/, ""));
|
||||
const v = (h.version ?? "").replace(/^v(?=\d)/, "");
|
||||
// A local "dev" build still identifies its source revision when the Go
|
||||
// VCS stamp (or -X main.revision) is present.
|
||||
const commit = h.build?.commit ?? "";
|
||||
setVersion(v === "dev" && commit ? `${v} (${commit.slice(0, 7)})` : v);
|
||||
const l = h.latest_version ?? "";
|
||||
setLatest(l && getLocalStorageValue(UPDATE_DISMISSED_KEY) !== l ? l : "");
|
||||
setDegraded(h.isolation?.mode === "degraded" ? (h.isolation.reason ?? "") : null);
|
||||
|
||||
@@ -278,12 +278,21 @@ function interceptPageQuery(page: number, size: number, filter: InterceptApprova
|
||||
|
||||
export const api = {
|
||||
// Backend application version (injected via ldflags at release, default "dev").
|
||||
// build.commit fills in the source revision for local builds, so the top bar
|
||||
// can show `dev (abc1234)` instead of a version that identifies nothing.
|
||||
// isolation: result of the startup agent isolation self-test; "degraded" shows a banner on every page.
|
||||
health: () =>
|
||||
get<{
|
||||
ok: boolean;
|
||||
service: string;
|
||||
version: string;
|
||||
build?: {
|
||||
version: string;
|
||||
commit?: string;
|
||||
go_version?: string;
|
||||
build_time?: string;
|
||||
modified?: boolean;
|
||||
};
|
||||
latest_version?: string;
|
||||
isolation?: { mode: "isolated" | "degraded"; reason?: string };
|
||||
}>("/health"),
|
||||
|
||||
Reference in New Issue
Block a user