Skip to content

Update .gitignore and improve ci #42

Update .gitignore and improve ci

Update .gitignore and improve ci #42

Workflow file for this run

name: Python Backend CI with Unittests
on:
push:
branches:
- 'test*'
pull_request:
branches:
- 'main'
- 'test*'
jobs:
Unittest:
runs-on: ubuntu-latest
services:
mysql_db:
image: mysql:8.4.0
env:
MYSQL_ROOT_PASSWORD: rootpassword_ci_safe
MYSQL_DATABASE: dbproject_db_test
MYSQL_USER: dbproject_user
MYSQL_PASSWORD: '123456'
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping -h 127.0.0.1 -P 3306 --silent"
--health-interval=10s
--health-timeout=5s
--health-retries=5
defaults:
run:
working-directory: ./src # ⭐ CWD set to src/ for all run steps
steps:
- name: Checkout code
uses: actions/checkout@v4
# This step checks out to github.workspace (project root)
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.13' # Adjust to your project's Python version
- name: Install Python dependencies
# CWD is ./src
run: |
python -m pip install --upgrade pip
pip install -r backend/requirements.txt # Path relative to src/
- name: Set up environment variables for application and tests
# These are set for the job and accessible by subsequent steps
# CWD is ./src, but this step writes to $GITHUB_ENV which is global to the job
run: |
echo "APP_MODE=test" >> $GITHUB_ENV
echo "TEST_DB_HOST=127.0.0.1" >> $GITHUB_ENV
echo "TEST_DB_PORT=3306" >> $GITHUB_ENV
echo "TEST_DB_USER=dbproject_user" >> $GITHUB_ENV
echo "TEST_DB_PASSWORD=123456" >> $GITHUB_ENV
echo "TEST_DB_NAME=dbproject_db_test" >> $GITHUB_ENV
echo "TEST_DB_URL=mysql+pymysql://dbproject_user:123456@127.0.0.1:3306/dbproject_db_test" >> $GITHUB_ENV
# Also set regular DB vars to point to test DB in CI to avoid issues
echo "DB_HOST=127.0.0.1" >> $GITHUB_ENV
echo "DB_PORT=3306" >> $GITHUB_ENV
echo "DB_USER=dbproject_user" >> $GITHUB_ENV
echo "DB_PASSWORD=123456" >> $GITHUB_ENV
echo "DB_NAME=dbproject_db_test" >> $GITHUB_ENV
echo "DB_URL=mysql+pymysql://dbproject_user:123456@127.0.0.1:3306/dbproject_db_test" >> $GITHUB_ENV
echo "SECRET_KEY=ci_super_secret_key_for_tests_v2" >> $GITHUB_ENV
# Add any other env vars your config.py expects via os.getenv()
- name: Wait for MySQL to be fully ready
# CWD is ./src
run: |
echo "Waiting for MySQL to start..."
timeout=60
while ! mysqladmin ping -h"127.0.0.1" -P"3306" -u"dbproject_user" -p"123456" --silent --connect-timeout=2 && [ $timeout -gt 0 ]; do
sleep 3
timeout=$((timeout-3))
echo "Still waiting for MySQL... ($timeout s remaining)"
done
if [ $timeout -le 0 ]; then
echo "MySQL did not start in time."
exit 1
fi
echo "MySQL is up and running."
- name: Run database DDL initialization script (run_all_ddls.py)
# CWD is ./src
# Python will look for 'backend.scripts.run_all_ddls' or 'backend/scripts/run_all_ddls.py'
# If running as a script, Python adds the script's dir to path, and CWD is on path.
# So, `import backend` should work.
run: |
echo "Running database DDL initialization script (run_all_ddls.py)..."
python -m backend.scripts.reset test # Path relative to src/
env: # Pass environment variables explicitly if script needs them directly (though GITHUB_ENV makes them available)
APP_MODE: ${{ env.APP_MODE }}
TEST_DB_HOST: ${{ env.TEST_DB_HOST }}
# ... (include all relevant DB_ and TEST_DB_ vars)
TEST_DB_URL: ${{ env.TEST_DB_URL }} # Ensure your script uses this
- name: Run Unittests
# CWD is ./src
# python -m unittest discover -s backend/test -p "test_*.py"
# The -s path is relative to the CWD (which is src/)
# Imports like `from backend.app...` within tests should work.
run: |
echo "Running unittests..."
python -m unittest discover -s backend/test -p "test_*.py" # Path relative to src/
env:
APP_MODE: ${{ env.APP_MODE }}
TEST_DB_HOST: ${{ env.TEST_DB_HOST }}
TEST_DB_PORT: ${{ env.TEST_DB_PORT }}
TEST_DB_USER: ${{ env.TEST_DB_USER }}
TEST_DB_PASSWORD: ${{ env.TEST_DB_PASSWORD }}
TEST_DB_NAME: ${{ env.TEST_DB_NAME }}
TEST_DB_URL: ${{ env.TEST_DB_URL }}