Files
dashboard-backend/deploy.sh
CloudForge Deploy 6b6db185e8
All checks were successful
Deploy Backend (Manual Build) / deploy (push) Successful in 18s
Deploy cloudforge-dashboard-backend - 2025-12-09 16:21:44
2025-12-09 16:21:44 +08:00

277 lines
6.7 KiB
Bash
Executable File

#!/bin/bash
set -e
# Configuration
REGISTRY_URL="fungtest.duckdns.org/cloudforge-dev/cloudforge-dashboard-backend:latest"
GITEA_DOMAIN="fungtest.duckdns.org"
GITEA_USERNAME="cloudforge-dev"
GITEA_PASSWORD="a7964adcd821be4c6d7c4ca0b05865030a9d0b82"
GITEA_EMAIL="deploy@cloudforge.local"
IMAGE_NAME="cloudforge-dashboard-backend"
NAMESPACE="default"
PORT="5001"
GIT_REMOTE="ssh://gitea-cloudforge-dev/cloudforge-dev/dashboard-backend.git"
REPLICAS=1
TIMEOUT=120
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Functions
log() {
echo -e "${GREEN}${NC} $1"
}
warn() {
echo -e "${YELLOW}⚠️ ${NC} $1"
}
error() {
echo -e "${RED}${NC} $1"
exit 1
}
separator() {
echo ""
echo "════════════════════════════════════════"
echo "$1"
echo "════════════════════════════════════════"
echo ""
}
# Main deployment logic
deploy_all() {
separator "Step 1: Build Docker Image"
build_image
separator "Step 2: Login to Gitea Registry"
login_registry
separator "Step 3: Push Image to Gitea Registry"
push_image
separator "Step 4: Create K3S Resources"
create_namespace
create_secret
create_deployment
create_service
separator "Step 5: Commit & Push to Git"
commit_to_git
separator "Step 6: Wait for Deployment"
wait_for_deployment
separator "✅ Deployment Complete!"
echo "Image: ${REGISTRY_URL}"
echo "Namespace: ${NAMESPACE}"
echo ""
echo "Verify deployment:"
echo " kubectl get pods -n ${NAMESPACE}"
echo " kubectl describe pod -n ${NAMESPACE} -l app=${IMAGE_NAME}"
}
build_image() {
echo "→ Building ${IMAGE_NAME}:latest"
if [ ! -f "Dockerfile" ]; then
error "Dockerfile not found in current directory"
fi
podman build -t ${IMAGE_NAME}:latest . || docker build -t ${IMAGE_NAME}:latest .
log "Build completed"
}
login_registry() {
echo "→ Authenticating with Gitea Registry at ${GITEA_DOMAIN}"
echo "${GITEA_PASSWORD}" | podman login -u ${GITEA_USERNAME} --password-stdin ${GITEA_DOMAIN} 2>/dev/null || \
echo "${GITEA_PASSWORD}" | docker login -u ${GITEA_USERNAME} --password-stdin ${GITEA_DOMAIN} 2>/dev/null || \
error "Failed to authenticate with registry"
log "Registry authentication successful"
}
push_image() {
echo "→ Tagging image for ${REGISTRY_URL}"
podman tag ${IMAGE_NAME}:latest ${REGISTRY_URL} || docker tag ${IMAGE_NAME}:latest ${REGISTRY_URL}
echo "→ Pushing to registry (this may take a few minutes...)"
podman push ${REGISTRY_URL} || docker push ${REGISTRY_URL}
log "Image pushed successfully"
}
create_namespace() {
echo "→ Creating namespace if not exists"
kubectl create namespace ${NAMESPACE} --dry-run=client -o yaml | kubectl apply -f -
log "Namespace ready"
}
create_secret() {
echo "→ Creating image pull secret"
kubectl delete secret gitea-registry -n ${NAMESPACE} 2>/dev/null || true
kubectl create secret docker-registry gitea-registry \
--docker-server=${GITEA_DOMAIN} \
--docker-username=${GITEA_USERNAME} \
--docker-password="${GITEA_PASSWORD}" \
--docker-email=${GITEA_EMAIL} \
-n ${NAMESPACE}
log "Image pull secret created"
}
create_deployment() {
echo "→ Creating deployment"
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: ${IMAGE_NAME}
namespace: ${NAMESPACE}
labels:
app: ${IMAGE_NAME}
spec:
replicas: ${REPLICAS}
selector:
matchLabels:
app: ${IMAGE_NAME}
template:
metadata:
labels:
app: ${IMAGE_NAME}
spec:
imagePullSecrets:
- name: gitea-registry
containers:
- name: ${IMAGE_NAME}
image: ${REGISTRY_URL}
imagePullPolicy: Always
ports:
- containerPort: ${PORT}
protocol: TCP
livenessProbe:
httpGet:
path: /health
port: ${PORT}
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
readinessProbe:
httpGet:
path: /health
port: ${PORT}
initialDelaySeconds: 10
periodSeconds: 5
timeoutSeconds: 3
failureThreshold: 2
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "500m"
EOF
log "Deployment created/updated"
}
create_service() {
echo "→ Creating service"
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: ${IMAGE_NAME}
namespace: ${NAMESPACE}
labels:
app: ${IMAGE_NAME}
spec:
type: ClusterIP
ports:
- port: ${PORT}
targetPort: ${PORT}
protocol: TCP
selector:
app: ${IMAGE_NAME}
EOF
log "Service created/updated"
}
wait_for_deployment() {
echo "→ Waiting for deployment to be ready (max ${TIMEOUT}s)"
if kubectl wait --for=condition=available --timeout=${TIMEOUT}s \
deployment/${IMAGE_NAME} -n ${NAMESPACE} 2>/dev/null; then
log "Deployment is ready!"
else
warn "Deployment not ready within ${TIMEOUT}s, checking status..."
echo ""
echo "Deployment status:"
kubectl get deployment -n ${NAMESPACE} ${IMAGE_NAME}
echo ""
echo "Pod status:"
kubectl get pods -n ${NAMESPACE} -l app=${IMAGE_NAME}
echo ""
echo "Pod details:"
kubectl describe pod -n ${NAMESPACE} -l app=${IMAGE_NAME} | tail -20
fi
}
commit_to_git() {
echo "→ Committing changes to Git"
git config user.email "deploy@cloudforge.local"
git config user.name "CloudForge Deploy"
git add -A
git commit -m "Deploy ${IMAGE_NAME} - $(date '+%Y-%m-%d %H:%M:%S')" || warn "Nothing to commit"
echo "→ Pushing to Gitea"
git push origin main || git push origin master || error "Failed to push to Git"
log "Committed and pushed to Git"
}
# Main logic
case "${1:-all}" in
all)
deploy_all
;;
build)
build_image
;;
login)
login_registry
;;
push)
build_image
login_registry
push_image
;;
deploy)
create_namespace
create_secret
create_deployment
create_service
wait_for_deployment
;;
*)
echo "Usage: ./deploy.sh [all|build|login|push|deploy]"
exit 1
;;
esac