🔧 Route dev deployments to Fanyu

This commit is contained in:
eust-w 2026-09-22 01:26:15 +08:00
parent d84b83c94d
commit 6bc3127228
4 changed files with 170 additions and 6 deletions

View File

@ -7,9 +7,10 @@
## Deployment Environment
- The `dev` branch uses the `poc` deployment and test environment.
- Leave `DEPLOY_ENV` unset on `dev`; the deploy scripts should resolve it to `poc`.
- Do not deploy `dev` to `wjzh` unless the user explicitly overrides this rule.
- The `dev` branch uses Fanyu. Leave `DEPLOY_ENV` unset or set it to `fanyu`; the loader rejects other targets, including `poc` and `wjzh`.
- POC-like branches resolve to `poc` and require that target. Feature branches outside these routing rules may select a target explicitly with `DEPLOY_ENV`.
- Supply Fanyu secrets and environment-specific settings through the loader's existing local override files; the tracked preset contains only non-secret defaults.
- Verify the target cluster and namespace before every release, including when the environment is inferred from the branch.
## Git Workflow

View File

@ -0,0 +1,11 @@
#!/usr/bin/env bash
# Non-secret Fanyu defaults. Supply Mongo and other settings via local overrides.
export K8S_NAMESPACE="${K8S_NAMESPACE:-droboticflow-fanyu}"
export REGISTRY_HOST="${REGISTRY_HOST:-ccr-29eug8s3-vpc.cnc.bj.baidubce.com}"
export REGISTRY_SERVER="${REGISTRY_SERVER:-$REGISTRY_HOST}"
export REGISTRY_NAMESPACE="${REGISTRY_NAMESPACE:-fanyu}"
export K8S_IMAGE_PULL_SECRET_NAME="${K8S_IMAGE_PULL_SECRET_NAME:-droboticflow-registry}"
export WORKSPACE_PVC_NAME="${WORKSPACE_PVC_NAME:-droboticflow-workspace}"
export WORKSPACE_MOUNT_PATH="${WORKSPACE_MOUNT_PATH:-/workspace}"
export WORKSPACE_BOS_BUCKET="${WORKSPACE_BOS_BUCKET:-drobodata-fanyu}"

View File

@ -24,14 +24,16 @@ branch_matches_deploy_env() {
branch_requires_poc_env() {
local branch="$1"
[[ "$branch" == "dev" ]] || branch_matches_deploy_env "$branch" "poc"
branch_matches_deploy_env "$branch" "poc"
}
infer_deploy_env_from_branch() {
local branch
branch="$(git -C "$ROOT_DIR" branch --show-current 2>/dev/null || true)"
if branch_requires_poc_env "$branch"; then
if [[ "$branch" == "dev" ]]; then
printf 'fanyu\n'
elif branch_requires_poc_env "$branch"; then
printf 'poc\n'
fi
}
@ -40,8 +42,13 @@ validate_branch_deploy_env() {
local branch
branch="$(git -C "$ROOT_DIR" branch --show-current 2>/dev/null || true)"
if [[ "$branch" == "dev" && "$DEPLOY_ENV" != "fanyu" ]]; then
echo "ERROR: current branch 'dev' is reserved for the fanyu deployment environment; DEPLOY_ENV=$DEPLOY_ENV is not allowed. Use DEPLOY_ENV=fanyu or leave DEPLOY_ENV unset on dev." >&2
return 1
fi
if branch_requires_poc_env "$branch" && [[ "$DEPLOY_ENV" != "poc" ]]; then
echo "ERROR: current branch '$branch' is reserved for the poc deployment environment. Use DEPLOY_ENV=poc or leave DEPLOY_ENV unset on dev/poc branches." >&2
echo "ERROR: current branch '$branch' is reserved for the poc deployment environment. Use DEPLOY_ENV=poc or leave DEPLOY_ENV unset on poc branches." >&2
return 1
fi
}

View File

@ -0,0 +1,145 @@
import pathlib
import shutil
import subprocess
import pytest
ROOT = pathlib.Path(__file__).resolve().parents[2]
@pytest.fixture
def loader(tmp_path):
# Isolate override files and credentials from the user's actual checkout.
scripts = tmp_path / 'scripts'
scripts.mkdir()
shutil.copy2(ROOT / 'scripts/load-deploy-env.sh', scripts)
shutil.copytree(ROOT / 'scripts/deploy-envs', scripts / 'deploy-envs')
bin_dir = tmp_path / 'bin'
bin_dir.mkdir()
git = bin_dir / 'git'
git.write_text('#!/bin/sh\nprintf "%s\\n" "$TEST_BRANCH"\n')
git.chmod(0o755)
def run(branch, target=None, extra=None, argument=''):
env = {
'PATH': f'{bin_dir}:/usr/bin:/bin',
'HOME': str(tmp_path / 'home'),
'XDG_CONFIG_HOME': str(tmp_path / 'config'),
'TEST_BRANCH': branch,
}
if target is not None:
env['DEPLOY_ENV'] = target
env.update(extra or {})
result = subprocess.run(
['bash', '-c', 'source "$1" "$2" || exit $?; /usr/bin/env',
'loader-test', str(scripts / 'load-deploy-env.sh'), argument],
env=env, text=True, capture_output=True,
)
values = dict(line.split('=', 1) for line in result.stdout.splitlines() if '=' in line)
return result, values
return run, tmp_path
def test_dev_defaults_to_minimal_fanyu(loader):
run, _ = loader
result, env = run('dev')
assert result.returncode == 0, result.stderr
expected = {
'DEPLOY_ENV': 'fanyu',
'DROBOTICFLOW_DEPLOY_ENV_LOADED': 'fanyu',
'K8S_NAMESPACE': 'droboticflow-fanyu',
'REGISTRY_HOST': 'ccr-29eug8s3-vpc.cnc.bj.baidubce.com',
'REGISTRY_SERVER': 'ccr-29eug8s3-vpc.cnc.bj.baidubce.com',
'REGISTRY_NAMESPACE': 'fanyu',
'K8S_IMAGE_PULL_SECRET_NAME': 'droboticflow-registry',
'WORKSPACE_PVC_NAME': 'droboticflow-workspace',
'WORKSPACE_MOUNT_PATH': '/workspace',
'WORKSPACE_BOS_BUCKET': 'drobodata-fanyu',
}
for key, value in expected.items():
assert env[key] == value
assert not any(key.startswith(('MONGO', 'POC_')) for key in env)
assert 'WORKER_POLL_INTERVAL_MS' not in env
@pytest.mark.parametrize('target', ['poc', 'wjzh'])
def test_dev_rejects_wrong_target(loader, target):
run, _ = loader
result, _ = run('dev', target)
assert result.returncode != 0
assert "branch 'dev'" in result.stderr
assert 'fanyu' in result.stderr
assert f'DEPLOY_ENV={target} is not allowed' in result.stderr
@pytest.mark.parametrize('branch', ['poc', 'poc-fix', 'poc/fix', 'user/poc', 'user/poc-fix', 'user/poc/fix'])
def test_poc_like_branches_keep_default_and_guard(loader, branch):
run, _ = loader
result, env = run(branch)
assert result.returncode == 0, result.stderr
assert env['DEPLOY_ENV'] == 'poc'
result, _ = run(branch, 'fanyu')
assert result.returncode != 0
assert 'reserved for the poc' in result.stderr
@pytest.mark.parametrize('target', ['fanyu', 'poc', 'wjzh'])
def test_feature_branch_explicit_target(loader, target):
run, _ = loader
result, env = run('feature/deployment-routing', target)
assert result.returncode == 0, result.stderr
assert env['DEPLOY_ENV'] == target
@pytest.mark.parametrize('branch', ['feature/unrelated', 'dev-fix', 'feature/pocket', 'main', ''])
def test_unrelated_branch_has_no_implicit_environment(loader, branch):
run, _ = loader
result, env = run(branch)
assert result.returncode == 0, result.stderr
assert 'DROBOTICFLOW_DEPLOY_ENV_LOADED' not in env
assert 'K8S_NAMESPACE' not in env
def test_explicit_dev_and_argument_selection(loader):
run, _ = loader
for target, argument in [('fanyu', ''), (None, 'fanyu')]:
result, env = run('dev', target, argument=argument)
assert result.returncode == 0, result.stderr
assert env['DEPLOY_ENV'] == 'fanyu'
result, _ = run('dev', argument='poc')
assert result.returncode != 0
def test_existing_override_files_and_kubeconfig(loader):
run, root = loader
config = root / 'config/droboticflow'
(config / 'deploy').mkdir(parents=True)
paths = [root / '.env.fanyu.local', root / '.env.fanyu.secrets',
config / 'deploy/fanyu.env', config / 'deploy/fanyu.secrets.env']
for index, path in enumerate(paths):
path.write_text(f'K8S_NAMESPACE=override-{index}\nOVERRIDE_{index}=loaded\n')
with paths[-1].open('a') as handle:
handle.write('MONGO_DB=explicit-test-db\n')
kubeconfig = config / 'kubeconfigs/fanyu.config'
kubeconfig.parent.mkdir()
kubeconfig.write_text('test fixture\n')
result, env = run('dev')
assert result.returncode == 0, result.stderr
assert env['K8S_NAMESPACE'] == 'override-3'
assert env['MONGO_DB'] == 'explicit-test-db'
assert env['KUBECONFIG'] == str(kubeconfig)
for index in range(4):
assert env[f'OVERRIDE_{index}'] == 'loaded'
def test_environment_values_are_preserved(loader):
run, _ = loader
values = {'K8S_NAMESPACE': 'custom', 'REGISTRY_HOST': 'registry.example',
'WORKSPACE_BOS_BUCKET': 'custom-bucket', 'KUBECONFIG': '/explicit/config'}
result, env = run('dev', extra=values)
assert result.returncode == 0, result.stderr
for key, value in values.items():
assert env[key] == value
assert env['REGISTRY_SERVER'] == 'registry.example'