fix(release): support environment validation in containers
This commit is contained in:
@@ -21,7 +21,8 @@
|
||||
- `REDIS_URL` 指向生产 Redis。
|
||||
- OSS 配置已确认或明确保持本地 fallback。
|
||||
- `GENERATED_FILES_HOST_DIR` 生产环境不得指向 staging 目录。
|
||||
- 本地校验:`python scripts/validate_release_env.py /var/lib/xiaoxia-saas-production/.env --strict-external`。
|
||||
- 本地校验文件:`python scripts/validate_release_env.py /var/lib/xiaoxia-saas-production/.env --strict-external`。
|
||||
- 容器内校验已注入环境:`docker exec xiaoxia-api-production python /app/scripts/validate_release_env.py --from-environ --strict-external`。
|
||||
- 如开启邮件/session:
|
||||
- `ENABLE_EMAIL_DELIVERY=true` 前先验证 SMTP 凭证。
|
||||
- `ENABLE_REDIS_SESSIONS=true` 前先验证 Redis 连通性。
|
||||
|
||||
@@ -108,7 +108,12 @@ def validate(values: dict[str, str], strict_external: bool) -> list[str]:
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("env_file", type=Path)
|
||||
parser.add_argument("env_file", type=Path, nargs="?", help="Env file to validate. Omit with --from-environ.")
|
||||
parser.add_argument(
|
||||
"--from-environ",
|
||||
action="store_true",
|
||||
help="Validate the current process environment instead of reading an env file.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--strict-external",
|
||||
action="store_true",
|
||||
@@ -116,14 +121,24 @@ def main() -> int:
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
values = parse_env_file(args.env_file)
|
||||
if args.from_environ:
|
||||
import os
|
||||
|
||||
values = dict(os.environ)
|
||||
source = "process environment"
|
||||
else:
|
||||
if args.env_file is None:
|
||||
parser.error("env_file is required unless --from-environ is set")
|
||||
values = parse_env_file(args.env_file)
|
||||
source = str(args.env_file)
|
||||
|
||||
errors = validate(values, args.strict_external)
|
||||
if errors:
|
||||
for error in errors:
|
||||
print(f"ERROR: {error}")
|
||||
return 1
|
||||
|
||||
print(f"OK: {args.env_file} passed release env validation")
|
||||
print(f"OK: {source} passed release env validation")
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user