diff --git a/.github/workflows/build_new.yml b/.github/workflows/build_new.yml index e1756a6..8186934 100644 --- a/.github/workflows/build_new.yml +++ b/.github/workflows/build_new.yml @@ -50,7 +50,7 @@ jobs: with: # Pass values from the matrix context and global env project_name: aimingmed-ai - image_repo: ghcr.io/$(echo $GITHUB_REPOSITORY | tr '[A-Z]' '[a-z]') + image_repo: "ghcr.io/$(echo $GITHUB_REPOSITORY | tr '[A-Z]' '[a-z]')" image_name: ${{ matrix.image_config.IMAGE_NAME }} build_context: ${{ matrix.image_config.BUILD_CONTEXT }} dockerfile: ${{ matrix.image_config.DOCKERFILE }} @@ -70,6 +70,7 @@ jobs: with: projectName: aimingmed-ai image_repo: ghcr.io/$(echo $GITHUB_REPOSITORY | tr '[A-Z]' '[a-z]') + testContainerName: tests-aimingmedai # Pass test environment variables as JSON string testEnvs: > [ @@ -92,8 +93,8 @@ jobs: "env": { "ENVIRONMENT": "dev", "TESTING": "1", - "DEEPSEEK_API_KEY": "", - "TAVILY_API_KEY": "" + "DEEPSEEK_API_KEY": "sk-XXXXXXXXXX", + "TAVILY_API_KEY": "tvly-dev-wXXXXXX" } }, diff --git a/.github/workflows/debug_env.yml b/.github/workflows/debug_env.yml deleted file mode 100644 index 4b1c4a4..0000000 --- a/.github/workflows/debug_env.yml +++ /dev/null @@ -1,12 +0,0 @@ -name: Debug Environment - -on: - workflow_dispatch: # Allows manual triggering - -jobs: - debug: - runs-on: ubuntu-latest - steps: - - name: Print GITHUB_REPOSITORY - run: | - echo "GITHUB_REPOSITORY: ${{ github.repository }}" \ No newline at end of file diff --git a/.github/workflows/template_test.yml b/.github/workflows/template_test.yml index 79903e8..30013d1 100644 --- a/.github/workflows/template_test.yml +++ b/.github/workflows/template_test.yml @@ -71,18 +71,18 @@ jobs: sudo rm -rf "/usr/local/share/boost" sudo rm -rf "$AGENT_TOOLSDIRECTORY" + - name: Define Image Repo and other tags + # Define tags consistently using inputs + id: tags + run: | + echo "image_repo_path=${{ inputs.image_repo }}" >> $GITHUB_OUTPUT + - name: Create Docker-Compose File from Inputs id: create_compose shell: pwsh run: | # Load Inputs (parse JSON strings) $imagesJson = '${{ inputs.images }}' - $deepseekApiKey = '${{ secrets.DEEPSEEK_API_KEY }}' - $tavilyApiKey = '${{ secrets.TAVILY_API_KEY }}' - # Substitute secrets *before* parsing JSON using environment variables - # Be very careful with escaping if secrets contain special JSON characters - $imagesJson = $imagesJson -replace '"DEEPSEEK_API_KEY": ""', "`"DEEPSEEK_API_KEY`": `"$deepseekApiKey`"" - $imagesJson = $imagesJson -replace '"TAVILY_API_KEY": ""', "`"TAVILY_API_KEY`": `"$tavilyApiKey`"" Write-Host "Substituted Images JSON: $imagesJson" # Debugging - remove sensitive info if public $images = $imagesJson | ConvertFrom-Json @@ -97,7 +97,7 @@ jobs: foreach ($img in $images) { $serviceName = $img.name $svc = @{} - $svc.image = "${{ env.IMAGE_REPO }}/$($serviceName):${{ env.TAG }}" # Use run_id tag + $svc.image = "${{ steps.tags.outputs.image_repo_path }}/$($serviceName):${{ env.TAG }}" # Use run_id tag if ($img.depends_on) { $svc.depends_on = $img.depends_on @@ -146,8 +146,8 @@ jobs: - name: Run Tests shell: bash run: | - TEST_DIRS='${{ inputs.tests }}' # Get JSON array string - TEST_ENVS_JSON='${{ inputs.testEnvs }}' # Get JSON array string + TEST_DIRS="${{ inputs.tests }}" + TEST_ENVS_JSON="${{ inputs.testEnvs }}" RESULTS_PATH="${{ inputs.testResultsPath }}" STAGING_DIR="${{ runner.temp }}/test-results" # Use runner temp dir for results mkdir -p "$STAGING_DIR" @@ -155,12 +155,14 @@ jobs: # Prepare environment variables for docker run ENV_ARGS="" if [[ "$TEST_ENVS_JSON" != "[]" ]]; then - while IFS= read -r line; do - ENV_ARGS+=" -e \"$line\"" - done <<< $(echo "$TEST_ENVS_JSON" | jq -r '.[]') + # Convert JSON array string to individual env vars + IFS=',' read -r -a env_array <<< $(echo "$TEST_ENVS_JSON" | jq -r '.[][]') + for env in "${env_array[@]}"; do + ENV_ARGS+=" -e \"$env\"" + done else # Add a dummy env var if none are provided, as required by original script logic - ENV_ARGS+=" -e DUMMY_ENV_TEST_RUN_ID=${{ github.run_id }}" + ENV_ARGS+=" -e DUMMY_ENV_TEST_RUN_ID=${{ github.run_id }}" fi echo "Env args: $ENV_ARGS" # Debugging @@ -169,7 +171,7 @@ jobs: echo "Using Network: $COMPOSE_NETWORK_NAME" # Loop through test directories and execute tests - echo "$TEST_DIRS" | jq -c '.[]' | while read test_dir; do + echo "$TEST_DIRS" | jq -r '.[]' | while read test_dir; do test_dir=$(echo $test_dir | sed 's/"//g') # Remove quotes echo "Running test: $test_dir" docker run \ @@ -177,7 +179,7 @@ jobs: $ENV_ARGS \ -v "$STAGING_DIR:$RESULTS_PATH" \ --rm \ - "${{ env.IMAGE_REPO }}/${{ inputs.testContainerName }}:${{ github.run_id }}" \ + "${{ steps.tags.outputs.image_repo_path }}/${{ inputs.testContainerName }}:${{ github.run_id }}" \ "$test_dir" # Add error handling if needed (e.g., exit script if a test run fails) if [ $? -ne 0 ]; then diff --git a/app/backend/Dockerfile b/app/backend/Dockerfile index 8de3d73..061acf8 100644 --- a/app/backend/Dockerfile +++ b/app/backend/Dockerfile @@ -1,5 +1,5 @@ # pull official base image -FROM python:3.11-slim-bullseye +FROM python:3.11-slim-bullseye AS base # create directory for the app user RUN mkdir -p /home/app @@ -16,16 +16,13 @@ WORKDIR $APP_HOME # set environment variables ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 -ENV ENVIRONMENT=prod -ENV TESTING=0 - # add app COPY . $APP_HOME # install python dependencies RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pipenv && rm -rf ~/.cache/pip -RUN pipenv install --deploy --dev +RUN pipenv install --deploy # chown all the files to the app user RUN chown -R app:app $APP_HOME @@ -33,8 +30,29 @@ RUN chown -R app:app $APP_HOME # change to the app user USER app + + +# TEST +FROM base AS test + +ENV ENVIRONMENT=dev +ENV TESTING=1 + +RUN pipenv install --deploy --dev + +# run tests +RUN pipenv run pytest tests --disable-warnings + + + +# BUILD +FROM base AS builder + +ENV ENVIRONMENT=prod +ENV TESTING=0 + # expose the port the app runs on EXPOSE 80 # run uvicorn -CMD ["pipenv", "run", "uvicorn", "main:app", "--reload", "--workers", "1", "--host", "0.0.0.0", "--port", "80"] \ No newline at end of file +CMD ["pipenv", "run", "uvicorn", "main:app", "--reload", "--workers", "1", "--host", "0.0.0.0", "--port", "80"]