diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 40a21ce..1ad8af5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -44,16 +44,16 @@ deploy: - main variables: CI: "true" # Mark this as a CI run - ENVIRONMENT: dev # Use dev environment settings - PORT: 8081 # Set the port for the application + ENVIRONMENT: test # Use test environment settings + PORT: 8081 script: - - echo "Setting up dependencies..." + - echo "Setting up Python dependencies..." - source $VIRTUAL_ENV/bin/activate - pip install -r requirements.txt - - echo "Verifying SSL files..." - - echo "Running the application with dev SSL certificates on port $PORT..." + - echo "Starting the application to test startup with SSL..." - cd backend - - python3.8 -m main + - python3.8 -m main # App will start, validate, and exit + - echo "Application startup validation completed successfully." release: stage: release diff --git a/backend/main.py b/backend/main.py index 23e9f47..af02a9f 100644 --- a/backend/main.py +++ b/backend/main.py @@ -90,6 +90,8 @@ if __name__ == "__main__": else: # Default behavior: Run the FastAPI server import os + from multiprocessing import Process + from time import sleep # Get environment from an environment variable environment = os.getenv("ENVIRONMENT", "dev") @@ -98,30 +100,39 @@ if __name__ == "__main__": # Paths for SSL certificates if is_ci: - # Use self-signed certificates for CI cert_path = "ssl/cert.pem" key_path = "ssl/key.pem" - host = "127.0.0.1" # In CI, bind to localhost - print("Using self-signed SSL certificates for CI...") + host = "127.0.0.1" + print("Running in CI mode with self-signed certificates...") elif environment == "test": - # Use proper test certificates cert_path = "ssl/mx-aare-test.psi.ch.pem" key_path = "ssl/mx-aare-test.psi.ch.key" - host = "0.0.0.0" # In test, bind to all interfaces + host = "0.0.0.0" print("Using test SSL certificates...") else: - # Use development self-signed certificates cert_path = "ssl/cert.pem" key_path = "ssl/key.pem" - host = "127.0.0.1" # Bind to localhost for dev - print("Using self-signed SSL certificates for development...") + host = "127.0.0.1" + print("Using development SSL certificates...") - # Run the application with appropriate SSL setup - uvicorn.run( - app, - host=host, - port=port, - log_level="debug", - ssl_keyfile=key_path, - ssl_certfile=cert_path, - ) + def run_server(): + uvicorn.run( + app, + host=host, + port=port, + log_level="debug", + ssl_keyfile=key_path, + ssl_certfile=cert_path, + ) + + if is_ci: + # In CI, start server in a subprocess and exit after a short delay + server_process = Process(target=run_server) + server_process.start() + sleep(5) # Wait for 5 seconds to ensure the server starts without errors + server_process.terminate() # Terminate the server process + server_process.join() # Clean up the process + print("CI: Server started successfully and exited.") + else: + # Normal behavior for running the FastAPI server + run_server()