Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

feat(web): show the image commit in the corner (#206)

cocofhucf1348e893da
diff --git a/.dockerignore b/.dockerignore
index 324cf104..1316a80c 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,4 +1,9 @@
+# Only HEAD and refs enter the context so a plain `docker build` can stamp the
+# commit without a --build-arg. The build stage still excludes .git.
.git
+!.git/HEAD
+!.git/refs
+!.git/packed-refs
.env*
.local
upstream.json
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 397edd93..38882f81 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -77,6 +77,7 @@ jobs:
echo "sha=$(git rev-parse HEAD)"
echo "image=${image}"
echo "prerelease=${prerelease}"
+ echo "commit=${tag_commit}"
} >> "$GITHUB_OUTPUT"
# arm64 lets Apple silicon and arm servers pull the image; only the final
@@ -133,6 +134,8 @@ jobs:
context: .
platforms: linux/amd64,linux/arm64
push: true
+ build-args: |
+ LOOPFORGE_COMMIT=${{ steps.release.outputs.commit }}
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.metadata.outputs.labels }}
cache-from: type=gha
diff --git a/Dockerfile b/Dockerfile
index 41483680..1d331349 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,3 +1,4 @@
+# syntax=docker/dockerfile:1
# The chatbot bundle and the Go binaries are architecture independent to build,
# so those stages run on the builder's own platform and Go cross-compiles for
# TARGETARCH. Only the final stage runs as the target (emulated for arm64 on an
@@ -18,7 +19,8 @@ ARG LOOPFORGE_BUILD_JOBS=
ENV LOOPFORGE_BUILD_JOBS=${LOOPFORGE_BUILD_JOBS}
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod go mod download
-COPY . .
+# Ref updates must not invalidate the Go build cache.
+COPY --exclude=.git . .
# Helpers do not embed the web UI, so they compile while the chatbot stage builds.
# The runner and gate binaries are built for every OS/arch regardless; GOOS and
# GOARCH pick the architecture of the binaries that run inside this image.
@@ -36,5 +38,10 @@ COPY --from=build /out/ /app/bin/
COPY LICENSE THIRD_PARTY_NOTICES.md /app/
COPY third_party/go/ /app/third_party/go/
ENV PATH="/app/bin:${PATH}"
+# An explicit LOOPFORGE_COMMIT wins. Otherwise read HEAD from the build context
+# so `docker build` stamps the same corner label as the release workflow.
+ARG LOOPFORGE_COMMIT=
+RUN --mount=type=bind,source=.,target=/ctx,ro \
+ sh /ctx/scripts/image-commit.sh /ctx /app/loopforge-commit
EXPOSE 8080
ENTRYPOINT ["/app/bin/agentflow"]
diff --git a/compose.dev.yaml b/compose.dev.yaml
index 236cbf27..c26fff83 100644
--- a/compose.dev.yaml
+++ b/compose.dev.yaml
@@ -8,5 +8,6 @@ services:
context: .
args:
LOOPFORGE_BUILD_JOBS: ${LOOPFORGE_BUILD_JOBS:-}
+ LOOPFORGE_COMMIT: ${LOOPFORGE_COMMIT:-}
environment:
AGENTFLOW_ISOLATED_IMAGE: loopforge-platform:local
diff --git a/internal/httpapi/studio.go b/internal/httpapi/studio.go
index e23c49fc..140f9895 100644
--- a/internal/httpapi/studio.go
+++ b/internal/httpapi/studio.go
@@ -39,9 +39,60 @@ func stampStudioIndex(static fs.FS, raw []byte) []byte {
tag := fmt.Sprintf(`<meta name="agentflow-app-domain" content="%s"></head>`, htmlpkg.EscapeString(domain))
html = strings.Replace(html, "</head>", tag, 1)
}
+ html = injectBuildCommit(html)
return []byte(html)
}
+// defaultBuildCommitFile is written by docker build from the LOOPFORGE_COMMIT
+// build-arg, or from git HEAD when none was passed.
+const defaultBuildCommitFile = "/app/loopforge-commit"
+
+// buildCommit is the git SHA baked into a published image. A short label in
+// the corner is enough to tell two images apart.
+func buildCommit() (short, full string) {
+ raw := strings.ToLower(strings.TrimSpace(commitSource()))
+ // 64 covers SHA-256 repositories.
+ if len(raw) < 7 || len(raw) > 64 {
+ return "", ""
+ }
+ for _, r := range raw {
+ if (r < '0' || r > '9') && (r < 'a' || r > 'f') {
+ return "", ""
+ }
+ }
+ return raw[:7], raw
+}
+
+func commitSource() string {
+ if raw := strings.TrimSpace(os.Getenv("LOOPFORGE_COMMIT")); raw != "" {
+ return raw
+ }
+ path := strings.TrimSpace(os.Getenv("LOOPFORGE_COMMIT_FILE"))
+ if path == "" {
+ path = defaultBuildCommitFile
+ }
+ body, err := os.ReadFile(path)
+ if err != nil {
+ return ""
+ }
+ return strings.TrimSpace(string(body))
+}
+
+func injectBuildCommit(html string) string {
+ short, full := buildCommit()
+ if short == "" || !strings.Contains(html, "</body>") {
+ return html
+ }
+ // Inline style: login and vscode pages do not share the workbench stylesheet.
+ // The CSP allows style-src 'unsafe-inline' and forbids inline scripts.
+ label := fmt.Sprintf(
+ `<style>.loopforge-build{position:fixed;right:12px;bottom:8px;z-index:35;margin:0;font:11px/1 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;letter-spacing:.02em;color:#7c7c82;user-select:text}</style><span class="loopforge-build" title="%s">%s</span></body>`,
+ htmlpkg.EscapeString("镜像版本 "+full),
+ htmlpkg.EscapeString(short),
+ )
+ return strings.Replace(html, "</body>", label, 1)
+}
+
func studioFileServer(static fs.FS) http.Handler {
files := http.FileServer(http.FS(static))
native, _ := fs.Sub(static, "native-chat")
@@ -52,6 +103,18 @@ func studioFileServer(static fs.FS) http.Handler {
nativeFiles.ServeHTTP(w, r)
return
}
+ if r.URL.Path == "/login.html" {
+ raw, err := fs.ReadFile(static, "login.html")
+ if err != nil {
+ files.ServeHTTP(w, r)
+ return
+ }
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ w.Header().Set("Cache-Control", "no-cache")
+ w.WriteHeader(http.StatusOK)
+ _, _ = w.Write([]byte(injectBuildCommit(string(raw))))
+ return
+ }
if r.URL.Path == "/" || r.URL.Path == "/index.html" || r.URL.Path == "/vscode.html" {
// 每次请求实时读取并重新计算版本哈希,
// 保证前端文件修改后无需重启服务即可生效。
diff --git a/internal/httpapi/studio_test.go b/internal/httpapi/studio_test.go
index 4bed9cf1..c87839cc 100644
--- a/internal/httpapi/studio_test.go
+++ b/internal/httpapi/studio_test.go
@@ -7,6 +7,7 @@ import (
"io/fs"
"net/http"
"net/http/httptest"
+ "os"
"path/filepath"
"strings"
"testing"
@@ -164,6 +165,78 @@ func TestStudioIndexInjectsAppDomain(t *testing.T) {
}
}
+func TestStudioPagesShowShortBuildCommit(t *testing.T) {
+ t.Setenv("LOOPFORGE_COMMIT", "60FC455aabbccddeeff001122334455667788990")
+ static := fstest.MapFS{
+ "index.html": {Data: []byte(`<html><head></head><body><div id="app"></div></body></html>`)},
+ "login.html": {Data: []byte(`<html><body class="login-page"></body></html>`)},
+ "vscode.html": {Data: []byte(`<html><body><div id="workbench"></div></body></html>`)},
+ }
+ h := studioFileServer(static)
+ want := `class="loopforge-build" title="镜像版本 60fc455aabbccddeeff001122334455667788990">60fc455</span>`
+ for _, path := range []string{"/", "/login.html", "/vscode.html"} {
+ rec := httptest.NewRecorder()
+ h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil))
+ if rec.Code != http.StatusOK {
+ t.Fatalf("%s: %d", path, rec.Code)
+ }
+ body := rec.Body.String()
+ if !strings.Contains(body, want) || strings.Contains(body, "<script>") {
+ t.Fatalf("%s should show the short commit without an inline script: %s", path, body)
+ }
+ if !strings.Contains(body, "position:fixed;right:12px;bottom:8px") {
+ t.Fatalf("%s commit label should sit in the corner: %s", path, body)
+ }
+ }
+}
+
+func TestStudioPagesShowCommitBakedByDockerBuild(t *testing.T) {
+ t.Setenv("LOOPFORGE_COMMIT", "")
+ path := filepath.Join(t.TempDir(), "loopforge-commit")
+ if err := os.WriteFile(path, []byte("60fc455aabbccddeeff001122334455667788990\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("LOOPFORGE_COMMIT_FILE", path)
+ static := fstest.MapFS{
+ "index.html": {Data: []byte(`<html><body></body></html>`)},
+ }
+ rec := httptest.NewRecorder()
+ studioFileServer(static).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
+ if !strings.Contains(rec.Body.String(), `>60fc455</span>`) {
+ t.Fatalf("docker-build commit file should show in the corner: %s", rec.Body.String())
+ }
+}
+
+func TestStudioPagesShowSHA256BuildCommit(t *testing.T) {
+ t.Setenv("LOOPFORGE_COMMIT", strings.Repeat("60fc455a", 8))
+ static := fstest.MapFS{
+ "index.html": {Data: []byte(`<html><body></body></html>`)},
+ }
+ rec := httptest.NewRecorder()
+ studioFileServer(static).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
+ if !strings.Contains(rec.Body.String(), `>60fc455</span>`) {
+ t.Fatalf("a 64-character commit should show in the corner: %s", rec.Body.String())
+ }
+}
+
+func TestStudioPagesOmitUnsafeOrMissingBuildCommit(t *testing.T) {
+ t.Setenv("LOOPFORGE_COMMIT_FILE", filepath.Join(t.TempDir(), "missing"))
+ static := fstest.MapFS{
+ "index.html": {Data: []byte(`<html><body></body></html>`)},
+ }
+ h := studioFileServer(static)
+ for _, commit := range []string{"", "60fc45", "<img src=x onerror=alert(1)>", "60fc455;rm", "not-a-sha"} {
+ t.Run(commit, func(t *testing.T) {
+ t.Setenv("LOOPFORGE_COMMIT", commit)
+ rec := httptest.NewRecorder()
+ h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
+ if strings.Contains(rec.Body.String(), "loopforge-build") || strings.Contains(rec.Body.String(), "onerror") {
+ t.Fatalf("commit %q must not be rendered: %s", commit, rec.Body.String())
+ }
+ })
+ }
+}
+
// The OAuth callback lands here in a separate tab; the page must render under
// the global CSP, so its script is external and nothing secret is on it.
func TestStudioServesGongfengOAuthDonePage(t *testing.T) {
diff --git a/scripts/image-commit.sh b/scripts/image-commit.sh
new file mode 100644
index 00000000..7050b05d
--- /dev/null
+++ b/scripts/image-commit.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+# Stamp the commit for an image build.
+# Usage: image-commit.sh <context-dir> <output-file>
+# LOOPFORGE_COMMIT wins. Otherwise resolve .git/HEAD in the context.
+# Objects are not required; a missing or unreadable git dir writes nothing.
+set -eu
+ctx=${1:-.}
+out=${2:?}
+commit=${LOOPFORGE_COMMIT:-}
+if [ -z "$commit" ] && [ -f "$ctx/.git/HEAD" ]; then
+ head=$(tr -d ' \t\r\n' < "$ctx/.git/HEAD")
+ case "$head" in
+ ref:refs/*)
+ ref=${head#ref:}
+ case "$ref" in
+ *..*) ref= ;;
+ esac
+ if [ -n "$ref" ] && [ -f "$ctx/.git/$ref" ]; then
+ commit=$(tr -d ' \t\r\n' < "$ctx/.git/$ref")
+ elif [ -n "$ref" ] && [ -f "$ctx/.git/packed-refs" ]; then
+ commit=$(awk -v r="$ref" '$1 !~ /^[#^]/ && $2 == r { print $1; exit }' "$ctx/.git/packed-refs")
+ fi
+ ;;
+ *)
+ case "$head" in
+ *[!0-9a-fA-F]*) ;;
+ *) commit=$head ;;
+ esac
+ ;;
+ esac
+fi
+case "$commit" in
+ *[!0-9a-fA-F]*) commit= ;;
+esac
+if [ "${#commit}" -lt 7 ] || [ "${#commit}" -gt 64 ]; then
+ commit=
+fi
+printf '%s' "$commit" > "$out"
diff --git a/scripts/install.sh b/scripts/install.sh
index deb553d7..b9465cd1 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -166,6 +166,10 @@ if [[ "${COMPOSE_FILE:-}" != "$dev_compose" ]]; then
docker_access compose --profile platform up -d --no-build --wait --wait-timeout 180
else
step 'Building LoopForge from source (first install takes several minutes; frontend, Go and runtime stages build in parallel)'
+ if [[ -z "${LOOPFORGE_COMMIT:-}" ]]; then
+ LOOPFORGE_COMMIT="$(git rev-parse HEAD 2>/dev/null || true)"
+ export LOOPFORGE_COMMIT
+ fi
# Download MySQL while the platform image builds instead of afterwards.
docker_access compose pull --quiet --policy missing mysql >/dev/null 2>&1 &
mysql_pull=$!