293 lines
12 KiB
YAML
293 lines
12 KiB
YAML
name: Staging Backend Quality Gate
|
|
|
|
on:
|
|
pull_request:
|
|
paths:
|
|
- '.gitea/workflows/backend-quality-gate.yml'
|
|
- '.gitea/workflows/deploy-staging.yml'
|
|
- '.gitea/workflows/migrate-staging-database.yml'
|
|
- 'deploy/**'
|
|
- 'frontend/**'
|
|
- 'jyotish_vedic/**'
|
|
- 'scripts/**'
|
|
- 'tests/**'
|
|
- 'mcp_server.py'
|
|
- 'pyproject.toml'
|
|
- 'requirements*.txt'
|
|
push:
|
|
branches: [staging]
|
|
workflow_dispatch:
|
|
|
|
concurrency:
|
|
group: staging-quality-${{ gitea.ref }}
|
|
cancel-in-progress: true
|
|
|
|
permissions:
|
|
contents: read
|
|
actions: write
|
|
|
|
jobs:
|
|
validate:
|
|
runs-on: manman-linux
|
|
timeout-minutes: 45
|
|
env:
|
|
GITEA_SHA: ${{ gitea.sha }}
|
|
steps:
|
|
- name: Checkout exact Gitea revision
|
|
run: |
|
|
set -euo pipefail
|
|
[[ "$GITEA_SHA" =~ ^[0-9a-f]{40}$ ]]
|
|
git init .
|
|
git remote remove origin 2>/dev/null || true
|
|
git remote add origin https://git.copse.top/root/Jyotisha.git
|
|
git -c http.connectTimeout=15 -c http.lowSpeedLimit=1024 -c http.lowSpeedTime=30 \
|
|
fetch --depth=1 --no-tags origin "$GITEA_SHA"
|
|
git checkout --detach --force "$GITEA_SHA"
|
|
git clean -ffdx
|
|
test "$(git rev-parse HEAD)" = "$GITEA_SHA"
|
|
test -z "$(git status --porcelain --untracked-files=all)"
|
|
|
|
- name: Install and verify Linux runner toolchain
|
|
run: |
|
|
set -euo pipefail
|
|
packages=()
|
|
if ! docker compose version --short 2>/dev/null | grep -Eq '^v?2\.'; then
|
|
packages+=(docker-compose-v2)
|
|
fi
|
|
venv_probe="$(mktemp -d "${RUNNER_TEMP:-/tmp}/jyotisha-venv-probe.XXXXXX")"
|
|
if ! python3 -m venv "$venv_probe/venv" >/dev/null 2>&1; then
|
|
packages+=(python3-venv)
|
|
fi
|
|
rm -rf "$venv_probe"
|
|
if [ "${#packages[@]}" -gt 0 ]; then
|
|
apt-get -o Acquire::Retries=3 -o Acquire::http::Timeout=30 \
|
|
-o Acquire::https::Timeout=30 update
|
|
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
|
|
"${packages[@]}"
|
|
fi
|
|
python3 --version
|
|
curl --version
|
|
git --version
|
|
openssl version
|
|
docker version
|
|
docker compose version
|
|
docker compose version --short | grep -Eq '^v?2\.'
|
|
docker compose --help | grep -q -- '--project-name'
|
|
|
|
- name: Prepare pinned Node tooling
|
|
env:
|
|
NODE_TOOL_SOURCE_IMAGE: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/library/node:22-bookworm-slim@sha256:ef343465b6a14bbdf2ab52f6e100ec0659a792464fcf72c462370d88b3df909c
|
|
NODE_TOOL_IMAGE: node:22-bookworm-slim
|
|
run: |
|
|
set -euo pipefail
|
|
if ! docker image inspect "$NODE_TOOL_SOURCE_IMAGE" >/dev/null 2>&1; then
|
|
for attempt in 1 2 3; do
|
|
if timeout 180 docker pull "$NODE_TOOL_SOURCE_IMAGE"; then
|
|
break
|
|
fi
|
|
if [ "$attempt" -eq 3 ]; then
|
|
echo "Failed to preload $NODE_TOOL_IMAGE after $attempt attempts" >&2
|
|
exit 1
|
|
fi
|
|
sleep $((attempt * 15))
|
|
done
|
|
fi
|
|
docker tag "$NODE_TOOL_SOURCE_IMAGE" "$NODE_TOOL_IMAGE"
|
|
docker image inspect "$NODE_TOOL_IMAGE" >/dev/null
|
|
tool_dir="$(mktemp -d "${RUNNER_TEMP:-/tmp}/jyotisha-node-tools.XXXXXX")"
|
|
cat > "$tool_dir/node" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
workdir="$(pwd -P)"
|
|
docker_args=(
|
|
run --rm --network host
|
|
--user "$(id -u):$(id -g)"
|
|
--group-add "$(stat -c %g /var/run/docker.sock)"
|
|
--volume "$workdir:$workdir"
|
|
--volume /tmp:/tmp
|
|
--volume /var/run/docker.sock:/var/run/docker.sock
|
|
--workdir "$workdir"
|
|
--env HOME=/tmp
|
|
--env "npm_config_cache=/tmp/jyotisha-npm-cache-${GITEA_SHA:-local}"
|
|
--env "DOCKER_CONFIG=/tmp/jyotisha-docker-config-${GITEA_SHA:-local}"
|
|
)
|
|
for command_name in docker git openssl; do
|
|
command_path="$(command -v "$command_name")"
|
|
docker_args+=(--volume "$command_path:/usr/local/bin/$command_name:ro")
|
|
done
|
|
while read -r library; do
|
|
[ -f "$library" ] && docker_args+=(--volume "$library:$library:ro")
|
|
done < <(ldd "$(command -v openssl)" | awk '/lib(ssl|crypto)\.so/ { print $3 }')
|
|
for compose_plugin in \
|
|
/usr/libexec/docker/cli-plugins/docker-compose \
|
|
/usr/lib/docker/cli-plugins/docker-compose; do
|
|
[ -x "$compose_plugin" ] && docker_args+=(--volume "$compose_plugin:$compose_plugin:ro")
|
|
done
|
|
exec docker "${docker_args[@]}" node:22-bookworm-slim "${0##*/}" "$@"
|
|
EOF
|
|
chmod 0755 "$tool_dir/node"
|
|
ln -s node "$tool_dir/npm"
|
|
test -n "${GITHUB_PATH:-}"
|
|
printf '%s\n' "$tool_dir" >> "$GITHUB_PATH"
|
|
export PATH="$tool_dir:$PATH"
|
|
node --version
|
|
npm --version
|
|
|
|
- name: Preload PostgreSQL integration image
|
|
env:
|
|
POSTGRES_TEST_SOURCE_IMAGE: public.ecr.aws/docker/library/postgres:17-alpine@sha256:742f40ea20b9ff2ff31db5458d127452988a2164df9e17441e191f3b72252193
|
|
POSTGRES_TEST_IMAGE: postgres:17-alpine
|
|
run: |
|
|
set -euo pipefail
|
|
if ! docker image inspect "$POSTGRES_TEST_IMAGE" >/dev/null 2>&1; then
|
|
for attempt in 1 2 3; do
|
|
if timeout 180 docker pull "$POSTGRES_TEST_SOURCE_IMAGE"; then
|
|
docker tag "$POSTGRES_TEST_SOURCE_IMAGE" "$POSTGRES_TEST_IMAGE"
|
|
break
|
|
fi
|
|
if [ "$attempt" -eq 3 ]; then
|
|
echo "Failed to preload $POSTGRES_TEST_IMAGE after $attempt attempts" >&2
|
|
exit 1
|
|
fi
|
|
sleep $((attempt * 15))
|
|
done
|
|
fi
|
|
docker image inspect "$POSTGRES_TEST_IMAGE" >/dev/null
|
|
|
|
- name: Install dependencies
|
|
run: |
|
|
set -euo pipefail
|
|
python3 -m venv .venv
|
|
export PATH="$PWD/.venv/bin:$PATH"
|
|
python -m pip install --upgrade pip
|
|
python -m pip install \
|
|
"mcp==1.28.1" \
|
|
"pydantic==2.13.4" \
|
|
"numpy==2.5.1" \
|
|
"pandas==2.3.3" \
|
|
"timezonefinder==8.2.5" \
|
|
-r requirements.txt -r requirements-dev.txt playwright
|
|
python -m playwright install --with-deps chromium
|
|
npm ci --prefix frontend
|
|
|
|
- name: Validate backend, package, frontend, and database contracts
|
|
run: |
|
|
set -euo pipefail
|
|
export PATH="$PWD/.venv/bin:$PATH"
|
|
ruff check scripts/run_quality_gate.py tests/test_varga_bphs.py \
|
|
tests/test_ashtakavarga_invariants.py tests/test_cli_smoke.py \
|
|
tests/test_yoga_rules_integrity.py
|
|
python -m py_compile scripts/*.py jyotish_vedic/*.py mcp_server.py
|
|
python scripts/run_quality_gate.py \
|
|
--profile quick --skip-yoga-logic --skip-frontend-runtime
|
|
python scripts/commercial_privacy_artifact_scan.py --json
|
|
python -m build
|
|
npm test --prefix frontend
|
|
npm run lint --prefix frontend
|
|
npm run build --prefix frontend
|
|
|
|
publish:
|
|
if: gitea.event_name == 'push' && gitea.ref == 'refs/heads/staging'
|
|
needs: validate
|
|
runs-on: manman-linux
|
|
timeout-minutes: 45
|
|
env:
|
|
GITEA_SHA: ${{ gitea.sha }}
|
|
GITEA_RUN_ATTEMPT: ${{ gitea.run_attempt }}
|
|
REGISTRY_HOST: crpi-d1feco6itet73spp.cn-hongkong.personal.cr.aliyuncs.com
|
|
IMAGE_REPOSITORY: crpi-d1feco6itet73spp.cn-hongkong.personal.cr.aliyuncs.com/copse/jyotisha
|
|
steps:
|
|
- name: Checkout exact Gitea revision
|
|
run: |
|
|
set -euo pipefail
|
|
[[ "$GITEA_SHA" =~ ^[0-9a-f]{40}$ ]]
|
|
git init .
|
|
git remote remove origin 2>/dev/null || true
|
|
git remote add origin https://git.copse.top/root/Jyotisha.git
|
|
git -c http.connectTimeout=15 -c http.lowSpeedLimit=1024 -c http.lowSpeedTime=30 \
|
|
fetch --depth=1 --no-tags origin "$GITEA_SHA"
|
|
git checkout --detach --force "$GITEA_SHA"
|
|
git clean -ffdx
|
|
test "$(git rev-parse HEAD)" = "$GITEA_SHA"
|
|
test -z "$(git status --porcelain --untracked-files=all)"
|
|
|
|
- name: Prepare pinned Node tooling
|
|
env:
|
|
NODE_TOOL_SOURCE_IMAGE: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/library/node:22-bookworm-slim@sha256:ef343465b6a14bbdf2ab52f6e100ec0659a792464fcf72c462370d88b3df909c
|
|
NODE_TOOL_IMAGE: node:22-bookworm-slim
|
|
run: |
|
|
set -euo pipefail
|
|
if ! docker image inspect "$NODE_TOOL_SOURCE_IMAGE" >/dev/null 2>&1; then
|
|
for attempt in 1 2 3; do
|
|
if timeout 180 docker pull "$NODE_TOOL_SOURCE_IMAGE"; then
|
|
break
|
|
fi
|
|
if [ "$attempt" -eq 3 ]; then
|
|
echo "Failed to preload $NODE_TOOL_IMAGE after $attempt attempts" >&2
|
|
exit 1
|
|
fi
|
|
sleep $((attempt * 15))
|
|
done
|
|
fi
|
|
docker tag "$NODE_TOOL_SOURCE_IMAGE" "$NODE_TOOL_IMAGE"
|
|
docker image inspect "$NODE_TOOL_IMAGE" >/dev/null
|
|
tool_dir="$(mktemp -d "${RUNNER_TEMP:-/tmp}/jyotisha-node-tools.XXXXXX")"
|
|
cat > "$tool_dir/node" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
workdir="$(pwd -P)"
|
|
exec docker run --rm \
|
|
--user "$(id -u):$(id -g)" \
|
|
--volume "$workdir:$workdir" \
|
|
--workdir "$workdir" \
|
|
--env HOME=/tmp \
|
|
node:22-bookworm-slim "${0##*/}" "$@"
|
|
EOF
|
|
chmod 0755 "$tool_dir/node"
|
|
ln -s node "$tool_dir/npm"
|
|
test -n "${GITHUB_PATH:-}"
|
|
printf '%s\n' "$tool_dir" >> "$GITHUB_PATH"
|
|
export PATH="$tool_dir:$PATH"
|
|
node --version
|
|
npm --version
|
|
|
|
- name: Build and publish exact-SHA ACR images
|
|
env:
|
|
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
|
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
|
|
run: |
|
|
set -euo pipefail
|
|
cleanup() { docker logout "$REGISTRY_HOST" >/dev/null 2>&1 || true; }
|
|
trap cleanup EXIT
|
|
printf '%s' "$REGISTRY_PASSWORD" | docker login "$REGISTRY_HOST" --username "$REGISTRY_USERNAME" --password-stdin
|
|
docker build -f deploy/railway-api.Dockerfile -t "$IMAGE_REPOSITORY:api-$GITEA_SHA" .
|
|
docker build -f deploy/railway-web.Dockerfile -t "$IMAGE_REPOSITORY:web-$GITEA_SHA" .
|
|
docker push "$IMAGE_REPOSITORY:api-$GITEA_SHA"
|
|
docker push "$IMAGE_REPOSITORY:web-$GITEA_SHA"
|
|
|
|
- name: Record immutable linux-amd64 image manifest
|
|
run: |
|
|
set -euo pipefail
|
|
[[ "$GITEA_SHA" =~ ^[0-9a-f]{40}$ ]]
|
|
[[ "$GITEA_RUN_ATTEMPT" =~ ^[0-9]+$ ]]
|
|
select_digest='import json,sys; d=json.load(sys.stdin); xs=d if isinstance(d,list) else [d]; xs=[x for x in xs if isinstance(x,dict) and isinstance(x.get("Descriptor",x),dict)]; x=next((x for x in xs if x.get("Descriptor",x).get("platform",{}).get("os")=="linux" and x.get("Descriptor",x).get("platform",{}).get("architecture")=="amd64"),None); print(x.get("Descriptor",x).get("digest","") if x else "")'
|
|
api_digest="$(docker manifest inspect "$IMAGE_REPOSITORY:api-$GITEA_SHA" --verbose | python3 -c "$select_digest")"
|
|
web_digest="$(docker manifest inspect "$IMAGE_REPOSITORY:web-$GITEA_SHA" --verbose | python3 -c "$select_digest")"
|
|
[[ "$api_digest" =~ ^sha256:[0-9a-f]{64}$ ]]
|
|
[[ "$web_digest" =~ ^sha256:[0-9a-f]{64}$ ]]
|
|
install -d -m 700 artifacts/staging-images
|
|
umask 077
|
|
printf 'git_sha=%s\napi_digest=%s\nweb_digest=%s\n' \
|
|
"$GITEA_SHA" "$api_digest" "$web_digest" \
|
|
> artifacts/staging-images/manifest.env
|
|
node frontend/scripts/staging-image-manifest.mjs \
|
|
artifacts/staging-images/manifest.env "$GITEA_SHA" "$IMAGE_REPOSITORY" >/dev/null
|
|
|
|
- name: Upload immutable staging image manifest
|
|
uses: https://gitea.com/actions/upload-artifact@v4
|
|
with:
|
|
name: staging-image-manifest-${{ gitea.sha }}-${{ gitea.run_attempt }}
|
|
path: artifacts/staging-images/manifest.env
|
|
if-no-files-found: error
|
|
retention-days: 30
|