Skip to content

Commit

Permalink
fix: add cleanup history
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcossIC committed Nov 24, 2024
1 parent 0a3113d commit 40e38f8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
58 changes: 49 additions & 9 deletions .github/workflows/mainServer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
GATEWAY_SERVICE_PORT: 3000
UPLOAD_SERVICE_NAME: klowhub-upload-api
UPLOAD_SERVICE_PORT: 3003
MAX_IMAGES_TO_KEEP: 2
steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -95,6 +96,41 @@ jobs:
working-directory: ./server
run: docker build --progress plain --build-arg PROJECT_ID=${{ secrets.PROJECT_ID }} --build-arg BUCKET_NAME=${{ secrets.BUCKET_NAME }} -f ./apps/uploadGCould/Dockerfile -t ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.UPLOAD_SERVICE_NAME }}:${{ github.sha }} ./apps/uploadGCould

# 10.5. Limpiar imágenes antiguas
- name: Cleanup Old Images
working-directory: ./server
run: |
for SERVICE in $USERS_SERVICE_NAME $COURSE_SERVICE_NAME $UPLOAD_SERVICE_NAME $GATEWAY_SERVICE_NAME
do
echo "Cleaning up old images for $SERVICE..."
# Obtener lista de tags ordenados por fecha
TAGS=$(gcloud container images list-tags \
${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/$SERVICE \
--format='get(tags)' \
--sort-by=~TIMESTAMP)
TOTAL_IMAGES=$(echo "$TAGS" | wc -l)
# Si hay más imágenes que el límite, eliminar las más antiguas
if [ $TOTAL_IMAGES -gt $MAX_IMAGES_TO_KEEP ]; then
# Cuántas imágenes eliminar
TO_DELETE=$((TOTAL_IMAGES - MAX_IMAGES_TO_KEEP))
echo "Found $TOTAL_IMAGES images, keeping $MAX_IMAGES_TO_KEEP, deleting $TO_DELETE"
# Eliminar las imágenes más antiguas
echo "$TAGS" | tail -n $TO_DELETE | while read TAG; do
if [ ! -z "$TAG" ]; then
echo "Deleting image with tag: $TAG"
gcloud container images delete \
"${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/$SERVICE:$TAG" \
--quiet
fi
done
fi
done
# 11. Subir la imagen de User Service a Artifact Registry
- name: Users Service - Push Docker image
working-directory: ./server
Expand Down Expand Up @@ -239,14 +275,18 @@ jobs:
MONGO_URI: ${{ secrets.MONGO_URI }}

# 18. Aplicando los manifiestos a los kubernetes
- name: Apply Kubernetes manifests
working-directory: ./server
run: kubectl apply -f k8s-rendered/

# 19. Verificando que el Ingress este listo
- name: Verify Ingress Status
- name: Apply Kubernetes manifests and cleanup
working-directory: ./server
run: |
echo "Checking Ingress status..."
kubectl get ingress -A
kubectl get pods -n kube-system -l k8s-app=glbc
kubectl apply -f k8s-rendered/
sleep 10
DEPLOYMENTS=("gateway" "users" "courses" "upload")
echo "Cleaning up revision history for all deployments..."
for DEPLOY_NAME in "${DEPLOYMENTS[@]}"
do
echo "Setting revision history limit for $DEPLOY_NAME"
kubectl patch deployment $DEPLOY_NAME -p '{"spec":{"revisionHistoryLimit":2}}'
kubectl rollout status deployment/$DEPLOY_NAME
echo "Current revision history for $DEPLOY_NAME:"
kubectl rollout history deployment/$DEPLOY_NAME
done
3 changes: 2 additions & 1 deletion server/k8s/courses-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ metadata:
labels:
app: klowhub-courses-api
spec:
replicas: 2
replicas: 1
revisionHistoryLimit: 2
selector:
matchLabels:
app: klowhub-courses-api
Expand Down
3 changes: 2 additions & 1 deletion server/k8s/gateway-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ metadata:
labels:
app: klowhub-gateway-api
spec:
replicas: 2
replicas: 1
revisionHistoryLimit: 2
selector:
matchLabels:
app: klowhub-gateway-api
Expand Down
3 changes: 2 additions & 1 deletion server/k8s/upload-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ metadata:
labels:
app: klowhub-upload-api
spec:
replicas: 2
replicas: 1
revisionHistoryLimit: 2
selector:
matchLabels:
app: klowhub-upload-api
Expand Down
3 changes: 2 additions & 1 deletion server/k8s/users-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ metadata:
labels:
app: klowhub-users-api
spec:
replicas: 2
replicas: 1
revisionHistoryLimit: 2
selector:
matchLabels:
app: klowhub-users-api
Expand Down

0 comments on commit 40e38f8

Please sign in to comment.