From 24d42ed0b8dfe993dead4ed51026d9b6198e75aa Mon Sep 17 00:00:00 2001 From: Jarian Cottingham Date: Tue, 7 Jul 2026 23:15:11 +0000 Subject: [PATCH] feat: CI auto-detects and shifts port to avoid production conflicts --- .gitea/workflows/ci.yml | 30 ++++++++++++++++++++++++++---- run_archiver.py | 9 ++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index b8c539e..dc9350e 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -102,13 +102,31 @@ jobs: docker rm -f newsarchiver-e2e 2>/dev/null || true docker run -d --name newsarchiver-e2e \ --network newsarchiver-network \ - -p 5000:5000 \ + -e CI=true \ + -e CI_PORT_OFFSET=1 \ -e ADMIN_PASSWORD="" \ -e ARCHIVE_DIR=/data/archives \ -e DISABLE_RSS_FETCH=1 \ jarianc/newsarchiverv2:test \ python run_archiver.py --serve --host 0.0.0.0 --port 5000 + - name: Discover app port + id: port + run: | + sleep 2 + # Read shifted port from container logs (entrypoint prints [ci-port-shift]) + CI_LOG=$(docker logs newsarchiver-e2e 2>&1 | grep "\[ci-port-shift\]" || echo "") + if [ -n "$CI_LOG" ]; then + APP_PORT=$(echo "$CI_LOG" | grep -oP 'to \K[0-9]+') + echo "app_port=${APP_PORT}" >> $GITHUB_OUTPUT + echo "$CI_LOG" + # Re-publish port now that we know the shifted port + echo "Discovered port ${APP_PORT} from CI logs" + else + echo "app_port=5000" >> $GITHUB_OUTPUT + echo "No CI port shift detected, using default 5000" + fi + - name: Seed test data run: | sleep 3 @@ -141,19 +159,23 @@ jobs: - name: Wait for app run: | - sleep 5 + APP_PORT="${{ steps.port.outputs.app_port }}" + sleep 3 + # Health check from inside container (no host port publish needed) for i in $(seq 1 30); do - curl -sf http://localhost:5000/ && echo "App ready" && break + docker exec newsarchiver-e2e curl -sf "http://localhost:${APP_PORT}/" && echo "App ready on port ${APP_PORT}" && exit 0 sleep 2 done + echo "App failed to start" && exit 1 - name: Run Playwright tests run: | + APP_PORT="${{ steps.port.outputs.app_port }}" docker run --rm \ --network newsarchiver-network \ -v $GITHUB_WORKSPACE/tests/playwright:/tests \ -w /tests \ - -e APP_URL=http://newsarchiver-e2e:5000 \ + -e APP_URL=http://newsarchiver-e2e:${APP_PORT} \ mcr.microsoft.com/playwright:v1.51.0-jammy \ npx playwright test diff --git a/run_archiver.py b/run_archiver.py index 464b3e0..3acb66d 100644 --- a/run_archiver.py +++ b/run_archiver.py @@ -255,7 +255,14 @@ Examples: ) args = parser.parse_args() - + + # CI port shift: detect CI env, shift port to avoid conflicts with production + if os.environ.get("CI") == "true" and os.environ.get("SKIP_PORT_SHIFT") != "1": + offset = int(os.environ.get("CI_PORT_OFFSET", "1")) + original_port = args.port + args.port = original_port + offset + print(f"[ci-port-shift] Port shifted from {original_port} to {args.port} (offset: {offset})") + logger = setup_logging(args.verbose) if args.run: