diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index a7247b5..18e8311 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -10,14 +10,16 @@ jobs: runs-on: ubuntu-latest env: + GOOGLE_PROJECT_ID: ${{ vars.GOOGLE_PROJECT_ID }} + GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS: ${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS }} + GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL: ${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL }} + ENVIRONMENT: ${{ vars.ENVIRONMENT }} GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GCP_SA_KEY }} BACKEND_IMAGE_NAME: europe-west3-docker.pkg.dev/thf-climate-cloud/thf-climate/thf-climate FRONTEND_IMAGE_NAME: europe-west3-docker.pkg.dev/thf-climate-cloud/thf-climate-frontend/thf-climate-frontend steps: - # Step 1: Checkout the code - - name: Checkout code - uses: actions/checkout@v2 + - uses: actions/checkout@v3 # Step 2: Decode and Write the Service Account Key to a file properly - name: Set up Google Cloud credentials @@ -43,11 +45,28 @@ jobs: run: | gcloud auth configure-docker europe-west3-docker.pkg.dev - # Step 6: Build and Deploy the backend - - name: Build Docker image (backend) + # Step 6: Build and the run the backend + - name: Build and Run the Docker image (backend) working-directory: ./backend run: | docker buildx build --platform linux/amd64 -t $BACKEND_IMAGE_NAME:latest . + docker run -d -p 8000:8000 --name thf-climate-backend $BACKEND_IMAGE_NAME + - name: Wait for the application to start + working-directory: ./backend + run: | + timeout 30 bash -c 'until curl -s http://127.0.0.1:8000/; do sleep 1; done' + + # Step 7: Run the tests against the local application + - name: Install dependencies and Run tests (backend) + working-directory: ./backend + env: + API_BASE_URL: "http://127.0.0.1:8000" + run: | + python -m pip install --upgrade pip + pip install -r requirements-dev.txt + pytest + + # Step 8: Push and Deploy the backend on Google cloud - name: Push Docker image to Artifact Registry (backend) working-directory: ./backend run: | @@ -64,7 +83,7 @@ jobs: --min-instances=1 \ --max-instances=5 - # Step 7: Build and Deploy the frontend + # Step 9: Build and Deploy the frontend - name: Build Docker image (frontend) working-directory: ./frontend run: | diff --git a/.github/workflows/tests-main.yaml b/.github/workflows/tests-main.yaml index c2180b8..872f6ee 100644 --- a/.github/workflows/tests-main.yaml +++ b/.github/workflows/tests-main.yaml @@ -16,15 +16,24 @@ jobs: ENVIRONMENT: ${{ vars.ENVIRONMENT }} steps: - uses: actions/checkout@v3 - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: '3.11' - - name: Install dependencies + + # Build and the run the backend docker image + - name: Build and Run the Docker image (backend) + working-directory: ./backend + run: | + docker buildx build --platform linux/amd64 -t thf-climate-backend . + docker run -d -p 8000:8000 --name thf-climate-backend thf-climate-backend + - name: Wait for the application to start working-directory: ./backend + run: | + timeout 30 bash -c 'until curl -s http://127.0.0.1:8000/; do sleep 1; done' + + # Run the tests against the local application + - name: Install dependencies and Run tests + working-directory: ./backend + env: + API_BASE_URL: "http://127.0.0.1:8000" run: | python -m pip install --upgrade pip pip install -r requirements-dev.txt - - name: Run tests - working-directory: ./backend - run: pytest + pytest diff --git a/backend/tests/test_api_response.py b/backend/tests/test_api_response.py new file mode 100644 index 0000000..50bfc38 --- /dev/null +++ b/backend/tests/test_api_response.py @@ -0,0 +1,14 @@ +import os +import requests + + +def test_api_response(): + # Make a GET request to the API + response = requests.get(os.getenv("API_BASE_URL")) + + # Verify the response status code + assert response.status_code == 200, f"Expected 200, got {response.status_code}" + + # Verify the response body + expected_response = {"Hello": "World"} + assert response.json() == expected_response, f"Expected {expected_response}, got {response.json()}"