Changed to postgresql from mysql. #9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: '🚀 Deploy Next.js Docker App' | |
on: | |
push: | |
branches: [main] | |
jobs: | |
build-and-deploy: | |
runs-on: self-hosted | |
name: '🐳 Build & Deploy' | |
steps: | |
- name: '🔍 Checkout Code' | |
uses: actions/checkout@v4 | |
- name: '🔒 Verify Secrets Exist' | |
run: | | |
if [ -z "${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }}" ]; then | |
echo "❌ Critical error: GOOGLE_SERVICES_JSON_BASE64 secret missing!" | |
exit 1 | |
fi | |
echo "✅ All secrets present" | |
- name: '📁 Create google-services.json' | |
run: | | |
echo "$GOOGLE_SERVICES_JSON_BASE64" | base64 --decode > google-services.json | |
echo "🔄 Validating JSON..." | |
if ! jq empty google-services.json; then | |
echo "❌ JSON validation failed!" | |
exit 1 | |
fi | |
env: | |
GOOGLE_SERVICES_JSON_BASE64: ${{ secrets.GOOGLE_SERVICES_JSON_BASE64 }} | |
- name: '⚙️ Create .env File' | |
run: | | |
echo "${{ secrets.ENV_FILE_CONTENT }}" > .env | |
echo "" >> .env | |
# ======================================================= | |
# 🐳 Docker Operations | |
# ======================================================= | |
- name: '🚀 Build, Launch, and Update Services' | |
run: | | |
# Step 1: Explicitly check for the network and create it only if it's missing. | |
if ! docker network ls | grep -q "codebuilder-net"; then | |
echo "Network 'codebuilder-net' not found. Creating it..." | |
docker network create codebuilder-net | |
else | |
echo "Network 'codebuilder-net' already exists. Skipping creation." | |
fi | |
# Step 2: Explicitly check and manage the database container. | |
DB_CONTAINER_NAME="codebuilder-postgres-db" | |
if [ $(docker ps -a -q -f name=^/${DB_CONTAINER_NAME}$) ]; then | |
# The container exists, check if it is running. | |
if [ $(docker ps -q -f name=^/${DB_CONTAINER_NAME}$) ]; then | |
echo "Database container '${DB_CONTAINER_NAME}' is already running." | |
else | |
# The container exists but is stopped, so start it. | |
echo "Database container '${DB_CONTAINER_NAME}' exists but is stopped. Starting it..." | |
docker start ${DB_CONTAINER_NAME} | |
fi | |
else | |
# The container does not exist, so create it for the first time. | |
echo "Database container '${DB_CONTAINER_NAME}' not found. Creating it..." | |
docker compose up -d db | |
fi | |
# Step 3: Wait for the database to be fully ready. | |
echo "Waiting for database to become available on localhost:5434..." | |
while ! nc -z localhost 5434; do | |
sleep 1 | |
done | |
echo "✅ Database is healthy and listening." | |
# Step 4: Build the new webapp image. | |
echo "Building the latest webapp image..." | |
docker compose build webapp | |
# Step 5: Explicitly stop and remove the old webapp container to prevent conflicts. | |
echo "Stopping and removing old webapp container if it exists..." | |
docker compose stop webapp || true | |
docker compose rm -f webapp || true | |
# Step 6: Deploy the new webapp container. | |
echo "Deploying the new webapp container..." | |
docker compose up -d --no-deps webapp | |
- name: '🗑 Prune Old Docker Images' | |
if: always() | |
run: docker image prune -af |