k8-mini-app/ci-build.sh

50 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# Script per buildare e pushare le immagini al registry locale
NODE_IP=$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[0].address}')
# Variabili
REGISTRY="localhost:5000"
TAG=$(date +%Y%m%d-%H%M%S)
ARGOCD_SERVER="${NODE_IP}:30080"
# Verifica che il registry sia in esecuzione
if ! curl -s http://${NODE_IP}:5000/v2/ > /dev/null; then
echo "❌ Registry ${NODE_IP}:5000 non raggiungibile."
exit 1
fi
# Build e push delle immagini
echo "🔨 Building backend image..."
docker build -t $REGISTRY/backend:$TAG ./backend
docker push $REGISTRY/backend:$TAG
echo "🔨 Building frontend image..."
docker build -t $REGISTRY/frontend:$TAG ./frontend
docker push $REGISTRY/frontend:$TAG
echo "🔨 Building worker image..."
docker build -t $REGISTRY/worker:$TAG ./worker
docker push $REGISTRY/worker:$TAG
# Aggiorna i file YAML di Kubernetes
echo "📝 Updating Kubernetes manifests..."
sed -i "s|localhost:5000/backend:.*|${REGISTRY}/backend:${TAG}|g" k8s/backend.yaml
sed -i "s|localhost:5000/frontend:.*|${REGISTRY}/frontend:${TAG}|g" k8s/frontend.yaml
sed -i "s|localhost:5000/worker:.*|${REGISTRY}/worker:${TAG}|g" k8s/worker.yaml
# Commit e push a Gitea se necessario
if [ "$1" == "--push" ]; then
echo "🚀 Pushing changes to Gitea..."
git add k8s/
git commit -m "Update images to tag $TAG"
git push
# Sincronizza automaticamente dopo il push
echo "🔄 Sincronizzando automaticamente con ArgoCD..."
argocd app sync k8-mini-app --server=$ARGOCD_SERVER --insecure
fi
echo "✅ Build completato. Immagini taggate come: $TAG"
echo "📋 Per applicare le modifiche manualmente: kubectl apply -f k8s/"
echo "🔄 Per sincronizzare con Argo CD: argocd app sync k8-mini-app --server=$ARGOCD_SERVER --insecure"