Skip to content

Commit 2b8a6ba

Browse files
Copilotngocbd
andcommitted
Fix GitHub Actions port conflicts by detecting CI environment
Co-authored-by: ngocbd <439333+ngocbd@users.noreply.github.com>
1 parent 06530d8 commit 2b8a6ba

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

tests/integrate/conftest.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,26 @@ def __init__(self):
5959
self.docker_client = docker.from_env()
6060
self.server_process = None
6161
self.containers_started = []
62+
self.is_github_actions = self._detect_github_actions()
63+
64+
def _detect_github_actions(self) -> bool:
65+
"""Detect if running in GitHub Actions environment"""
66+
return (
67+
os.getenv("GITHUB_ACTIONS") == "true" or
68+
os.getenv("CI") == "true" or
69+
os.getenv("RUNNER_OS") is not None
70+
)
6271

6372
def start_dependencies(self):
6473
"""Start PostgreSQL and Redis using Docker"""
6574
print("Starting test dependencies...")
6675

76+
# Skip Docker container creation in GitHub Actions since services are provided
77+
if self.is_github_actions:
78+
print("Detected GitHub Actions environment - using provided services")
79+
self._wait_for_dependencies()
80+
return
81+
6782
# Start PostgreSQL
6883
try:
6984
postgres_container = self.docker_client.containers.run(
@@ -198,12 +213,16 @@ def stop_server(self):
198213
self.server_process.wait()
199214
print("Server stopped")
200215

201-
for container in self.containers_started:
202-
try:
203-
container.stop()
204-
print(f"Stopped container: {container.id[:12]}")
205-
except docker.errors.NotFound:
206-
pass
216+
# Only stop containers if we started them (not in GitHub Actions)
217+
if not self.is_github_actions:
218+
for container in self.containers_started:
219+
try:
220+
container.stop()
221+
print(f"Stopped container: {container.id[:12]}")
222+
except docker.errors.NotFound:
223+
pass
224+
else:
225+
print("Skipping container cleanup in GitHub Actions environment")
207226

208227
def is_server_running(self) -> bool:
209228
"""Check if the server is running"""

tests/run_tests.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,14 @@ done
111111
cleanup() {
112112
print_status "Cleaning up test environment..."
113113

114-
# Stop any running containers
115-
docker stop test_postgres test_redis 2>/dev/null || true
116-
docker rm test_postgres test_redis 2>/dev/null || true
114+
# Only stop containers if not in GitHub Actions (they're managed by GitHub)
115+
if [ "$GITHUB_ACTIONS" != "true" ] && [ "$CI" != "true" ]; then
116+
# Stop any running containers
117+
docker stop test_postgres test_redis 2>/dev/null || true
118+
docker rm test_postgres test_redis 2>/dev/null || true
119+
else
120+
print_status "Skipping container cleanup in CI environment"
121+
fi
117122

118123
# Kill any running server processes
119124
pkill -f "cargo run" 2>/dev/null || true

0 commit comments

Comments
 (0)