Files
ZitPlane/tests/ci/login_image/build.sh
T

108 lines
4.7 KiB
Bash

#!/bin/sh
# Imported source is compiled entirely inside a run-owned Docker builder.
# Only .next/standalone and the untouched upstream Dockerfile enter the
# temporary local image build context. No source tree write or registry push.
set -eu
: "${GITHUB_RUN_ID:?Missing numeric disposable Gitea run ID}"
case "$GITHUB_RUN_ID" in *[!0-9]*|'') echo LOGIN_CI_INVALID_RUN_ID >&2; exit 1;; esac
builder="zitplane-login-$GITHUB_RUN_ID-builder"
image="zitplane-login-ci:$GITHUB_RUN_ID"
context="/tmp/zitplane-login-build-$GITHUB_RUN_ID"
label="zitplane.login.run=$GITHUB_RUN_ID"
docker info --format '{{.Driver}}' >/dev/null
if docker container inspect "$builder" >/dev/null 2>&1 ||
docker image inspect "$image" >/dev/null 2>&1 ||
[ -e "$context" ] ||
[ -e auth/ui/v2/apps/login/.env ] ||
[ -e auth/ui/v2/apps/login/.env.prod ]; then
echo ZITPLANE_CUSTOM_LOGIN_PREEXISTING_STATE_REFUSED >&2
exit 1
fi
test -r auth/ui/v2/apps/login/Dockerfile
test -r auth/ui/v2/pnpm-lock.yaml
test -r tests/ci/oidc_e2e/cleanup.sh
echo ZITPLANE_CUSTOM_LOGIN_ISOLATED_BUILD_PREFLIGHT_PASS
docker create --name "$builder" --label "$label" node:22-bookworm sleep 3600 >/dev/null
docker start "$builder" >/dev/null
docker exec "$builder" mkdir -p /work
docker cp auth/ui/v2 "$builder:/work/v2"
docker exec -w /work/v2 "$builder" sh -ec '
corepack enable
corepack prepare pnpm@10.30.3 --activate
pnpm install --frozen-lockfile
echo ZITPLANE_CUSTOM_LOGIN_FROZEN_INSTALL_PASS
: > apps/login/.env
export NEXT_PUBLIC_BASE_PATH=/ui/v2/login
export NX_NO_CLOUD=true NX_DAEMON=false CI=true
pnpm exec nx run @zitadel/login:build --skip-nx-cache
rm -f apps/login/.env
test -f apps/login/.next/standalone/apps/login/server.js
test -f apps/login/.next/standalone/entrypoint.sh
test -f apps/login/.next/standalone/healthcheck.mjs
echo ZITPLANE_CUSTOM_LOGIN_STANDALONE_BUILD_PASS
'
mkdir -m 700 -p "$context/.next"
touch "$context/.zitplane-owned-$GITHUB_RUN_ID"
docker cp "$builder:/work/v2/apps/login/.next/standalone" "$context/.next/standalone"
# The immutable upstream Git entrypoint is 100644. The upstream Dockerfile
# directly execs /app/entrypoint.sh as nextjs. Repair only this disposable
# COPY input, never the tracked source, original Dockerfile or checkout.
entry="$context/.next/standalone/entrypoint.sh"
if [ -L "$entry" ] || [ ! -f "$entry" ]; then
echo ZITPLANE_CUSTOM_LOGIN_ENTRYPOINT_NOT_REGULAR >&2
exit 1
fi
if [ "$(head -c 2 "$entry")" != '#!' ]; then
echo ZITPLANE_CUSTOM_LOGIN_ENTRYPOINT_INVALID_SHEBANG >&2
exit 1
fi
mode="$(stat -c '%a' "$entry")"
owner="$(stat -c '%u:%g' "$entry")"
case "$mode:$owner" in *[!0-9:]*|'') echo ZITPLANE_CUSTOM_LOGIN_ENTRYPOINT_STAT_INVALID >&2; exit 1;; esac
echo "ZITPLANE_CUSTOM_LOGIN_ENTRYPOINT_COPY_MODE=$mode OWNER=$owner"
case "$mode" in
644)
[ ! -x "$entry" ] || { echo ZITPLANE_CUSTOM_LOGIN_ENTRYPOINT_UNEXPECTED_EXECUTION_MODE >&2; exit 1; }
chmod 0755 "$entry"
echo ZITPLANE_CUSTOM_LOGIN_TEMP_ENTRYPOINT_MODE_FIXED_PASS
;;
755)
[ -x "$entry" ] || { echo ZITPLANE_CUSTOM_LOGIN_ENTRYPOINT_NOT_EXECUTABLE >&2; exit 1; }
echo ZITPLANE_CUSTOM_LOGIN_TEMP_ENTRYPOINT_ALREADY_EXECUTABLE_PASS
;;
*)
echo ZITPLANE_CUSTOM_LOGIN_ENTRYPOINT_UNEXPECTED_MODE >&2
exit 1
;;
esac
[ ! -L "$entry" ] && [ -f "$entry" ] && [ -x "$entry" ] &&
[ "$(stat -c '%a' "$entry")" = 755 ] || {
echo ZITPLANE_CUSTOM_LOGIN_ENTRYPOINT_COPY_EXECUTABILITY_BLOCKED >&2
exit 1
}
echo ZITPLANE_CUSTOM_LOGIN_ENTRYPOINT_COPY_EXECUTABLE_PASS
cp auth/ui/v2/apps/login/Dockerfile "$context/Dockerfile"
docker build --pull --label "$label" -t "$image" -f "$context/Dockerfile" "$context"
# Probe the final image under its original USER nextjs, with no network,
# credentials, host volumes or entrypoint execution; print no file contents.
docker run --rm --network none --entrypoint /bin/sh "$image" -ec '
entry=/app/entrypoint.sh
[ ! -L "$entry" ] && [ -f "$entry" ] && [ -x "$entry" ] &&
[ "$(stat -c "%a" "$entry")" = 755 ] &&
[ "$(head -c 2 "$entry")" = "#!" ]
' || {
echo ZITPLANE_CUSTOM_LOGIN_BUILT_IMAGE_ENTRYPOINT_BLOCKED >&2
exit 1
}
echo ZITPLANE_CUSTOM_LOGIN_BUILT_IMAGE_ENTRYPOINT_EXECUTABLE_PASS
actual_label="$(docker image inspect --format '{{ index .Config.Labels "zitplane.login.run" }}' "$image")"
image_id="$(docker image inspect --format '{{.Id}}' "$image")"
if [ "$actual_label" != "$GITHUB_RUN_ID" ]; then
echo ZITPLANE_CUSTOM_LOGIN_IMAGE_OWNERSHIP_MISMATCH >&2
exit 1
fi
case "$image_id" in sha256:*) ;; *) echo ZITPLANE_CUSTOM_LOGIN_IMAGE_ID_MISSING >&2; exit 1;; esac
echo ZITPLANE_CUSTOM_LOGIN_LOCAL_IMAGE_BUILT_PASS
# Record only a digest/ID and sanitized ownership metadata (never credentials).
echo "ZITPLANE_CUSTOM_LOGIN_IMAGE_ID=$image_id"