Some checks failed
build-and-deploy / build-and-deploy (push) Failing after 1m6s
69 lines
3.0 KiB
YAML
69 lines
3.0 KiB
YAML
name: build-and-deploy
|
||
|
||
on:
|
||
push:
|
||
branches: [ main, master ]
|
||
|
||
jobs:
|
||
build-and-deploy:
|
||
runs-on: docker
|
||
env:
|
||
REG: ${{ vars.REGISTRY_URL }}
|
||
REPO_OWNER: ${{ github.repository_owner }}
|
||
APP_NAME: ${{ vars.APP_NAME }}
|
||
APP_DOMAIN: ${{ vars.APP_DOMAIN }}
|
||
NUXT_PUBLIC_APP_URL: ${{ vars.NUXT_PUBLIC_APP_URL }}
|
||
# PostgreSQL
|
||
POSTGRES_USER: ${{ vars.POSTGRES_USER || 'seguidor' }}
|
||
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD || 'seguidor_password' }}
|
||
POSTGRES_DB: ${{ vars.POSTGRES_DB || 'seguidor_lotes' }}
|
||
steps:
|
||
- uses: actions/checkout@v3
|
||
|
||
- name: Build, push and deploy
|
||
run: |
|
||
# Login to registry
|
||
docker login $REG -u ${{ secrets.REGISTRY_USERNAME }} -p ${{ secrets.REGISTRY_PASSWORD }}
|
||
|
||
# Build and push
|
||
echo "🔨 Building image..."
|
||
cd nuxt4
|
||
docker build -t $REG/$REPO_OWNER/$APP_NAME:${{ github.sha }} -t $REG/$REPO_OWNER/$APP_NAME:latest .
|
||
echo "📤 Pushing images..."
|
||
docker push $REG/$REPO_OWNER/$APP_NAME:${{ github.sha }}
|
||
docker push $REG/$REPO_OWNER/$APP_NAME:latest
|
||
|
||
# Deploy
|
||
cd ..
|
||
echo "ℹ️ Deploying $APP_NAME"
|
||
echo " Domain: $APP_DOMAIN"
|
||
echo " Image: $REG/$REPO_OWNER/$APP_NAME:latest"
|
||
|
||
# Limpiar despliegue anterior si existe (nombre viejo)
|
||
docker compose --project-name seguidor-de-lotes down -v 2>/dev/null || true
|
||
|
||
docker compose pull
|
||
docker compose --project-name $APP_NAME down
|
||
docker compose --project-name $APP_NAME up -d --remove-orphans --wait
|
||
|
||
# Asegurar autenticación md5 y que la contraseña coincide con la env (cura volúmenes viejos)
|
||
echo "🔐 Sincronizando autenticación PostgreSQL (md5 + password)..."
|
||
ESCAPED_PASSWORD=${POSTGRES_PASSWORD//\'/\'\"\'\"\'}
|
||
docker exec -u postgres $APP_NAME-postgres psql -d $POSTGRES_DB -c "ALTER SYSTEM SET password_encryption = 'md5';"
|
||
docker exec -u postgres $APP_NAME-postgres psql -d $POSTGRES_DB -c "ALTER ROLE \"$POSTGRES_USER\" WITH PASSWORD '${ESCAPED_PASSWORD}';"
|
||
docker exec -u postgres $APP_NAME-postgres psql -d $POSTGRES_DB -c "SELECT pg_reload_conf();"
|
||
|
||
# Inicializar base de datos si es necesario
|
||
echo "🗄️ Inicializando base de datos..."
|
||
# Verificar si las tablas existen
|
||
TABLE_EXISTS=$(docker exec $APP_NAME-postgres psql -U $POSTGRES_USER -d $POSTGRES_DB -tAc "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'lotes');")
|
||
|
||
if [ "$TABLE_EXISTS" = "f" ]; then
|
||
echo "📝 Ejecutando scripts de inicialización..."
|
||
docker exec -i $APP_NAME-postgres psql -U $POSTGRES_USER -d $POSTGRES_DB < nuxt4/server/database/01_schema.sql
|
||
docker exec -i $APP_NAME-postgres psql -U $POSTGRES_USER -d $POSTGRES_DB < nuxt4/server/database/02_seed.sql
|
||
echo "✅ Base de datos inicializada"
|
||
else
|
||
echo "ℹ️ Base de datos ya inicializada"
|
||
fi
|