better deploys
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 2m40s
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 2m40s
This commit is contained in:
@@ -103,6 +103,41 @@ jobs:
|
||||
name: code-coverage-report
|
||||
path: .coverage/
|
||||
|
||||
- name: Check for Database Schema Changes
|
||||
env:
|
||||
# Use production database credentials for this check.
|
||||
DB_HOST: ${{ secrets.DB_HOST }}
|
||||
DB_PORT: ${{ secrets.DB_PORT }}
|
||||
DB_USER: ${{ secrets.DB_USER }}
|
||||
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
|
||||
DB_DATABASE: ${{ secrets.DB_DATABASE_PROD }} # Assumes a secret for the production DB name.
|
||||
run: |
|
||||
echo "--- Checking for schema changes ---"
|
||||
# Calculate the hash of the current schema file in the repository.
|
||||
# We normalize line endings to ensure the hash is consistent across different OS environments.
|
||||
CURRENT_HASH=$(cat sql/master_schema_rollup.sql | dos2unix | sha256sum | awk '{ print $1 }')
|
||||
echo "Current Git Schema Hash: $CURRENT_HASH"
|
||||
|
||||
# Query the production database to get the hash of the deployed schema.
|
||||
# The `psql` command requires PGPASSWORD to be set.
|
||||
# `\t` sets tuples-only mode and `\A` unaligns output to get just the raw value.
|
||||
# The `|| echo "none"` ensures the command doesn't fail if the table or row doesn't exist yet.
|
||||
DEPLOYED_HASH=$(PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_DATABASE" -c "SELECT schema_hash FROM public.schema_info WHERE id = 1;" -t -A || echo "none")
|
||||
echo "Deployed DB Schema Hash: $DEPLOYED_HASH"
|
||||
|
||||
if [ "$DEPLOYED_HASH" = "none" ]; then
|
||||
echo "WARNING: No schema hash found in the production database."
|
||||
echo "This is expected for a first-time deployment. The hash will be set after a successful deployment."
|
||||
# We allow the deployment to continue, but a manual schema update is required.
|
||||
# You could choose to fail here by adding `exit 1`.
|
||||
elif [ "$CURRENT_HASH" != "$DEPLOYED_HASH" ]; then
|
||||
echo "ERROR: Database schema mismatch detected!"
|
||||
echo "The schema file in the repository has changed. A manual database migration is required."
|
||||
exit 1 # Fail the deployment pipeline.
|
||||
else
|
||||
echo "✅ Schema is up to date. No changes detected."
|
||||
fi
|
||||
|
||||
# --- Frontend Deployment ---
|
||||
- name: Build React Application
|
||||
# We set the environment variable directly in the command line for this step.
|
||||
@@ -144,6 +179,22 @@ jobs:
|
||||
# We also add `&& pm2 save` to persist the process list across server reboots.
|
||||
pm2 startOrReload ecosystem.config.cjs --env production && pm2 save
|
||||
echo "Backend server reloaded successfully."
|
||||
|
||||
# After a successful deployment, update the schema hash in the database.
|
||||
# This ensures the next deployment will compare against this new state.
|
||||
echo "Updating schema hash in production database..."
|
||||
CURRENT_HASH=$(cat sql/master_schema_rollup.sql | dos2unix | sha256sum | awk '{ print $1 }')
|
||||
PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_DATABASE" -c \
|
||||
"INSERT INTO public.schema_info (id, schema_hash, deployed_at) VALUES (1, '$CURRENT_HASH', NOW())
|
||||
ON CONFLICT (id) DO UPDATE SET schema_hash = EXCLUDED.schema_hash, deployed_at = NOW();"
|
||||
|
||||
# Verify the hash was updated
|
||||
UPDATED_HASH=$(PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_DATABASE" -c "SELECT schema_hash FROM public.schema_info WHERE id = 1;" -t -A)
|
||||
if [ "$CURRENT_HASH" = "$UPDATED_HASH" ]; then
|
||||
echo "✅ Schema hash successfully updated in the database to: $UPDATED_HASH"
|
||||
else
|
||||
echo "ERROR: Failed to update schema hash in the database."
|
||||
fi
|
||||
|
||||
- name: Show PM2 Environment for Production
|
||||
run: |
|
||||
|
||||
Reference in New Issue
Block a user