Blog Article DevOps & CI/CD

Building a Lightweight, Zero-Downtime GitOps Workflow on Linux

Building a Lightweight, Zero-Downtime GitOps Workflow

Modern infrastructure does not always require heavy Kubernetes clusters. For small-to-medium enterprise workloads, a containerized GitOps engine running over optimized Linux instances provides incredible speed, zero downtime, and massive cost savings.


Core Tenets of our Setup

  1. Declarative State in Git: Every environment variable, docker compose definition, and Nginx reverse proxy configuration lives in version-controlled repositories.
  2. Blue/Green Socket Swapping: Zero dropped connections during new code deployments.
  3. Automated Health Probes: Containers must pass HTTP 200 checks before traffic is cut over.
sequenceDiagram
    autonumber
    actor Dev as Developer
    participant Git as GitHub Repo
    participant Hook as Webhook Agent
    participant Node as Production Server
    participant Nginx as Nginx Proxy

    Dev->>Git: git push origin main
    Git->>Hook: Trigger Deployment Webhook
    Hook->>Node: Pull Images & Launch Container (Green)
    Node->>Node: Run Database Migrations & Healthcheck
    Node->>Nginx: Reload Upstream (Traffic -> Green)
    Node->>Node: Teardown Old Container (Blue)
    Node-->>Dev: Deployment Success Notification

The Deployment Automation Script

bash
#!/usr/bin/env bash
set -euo pipefail

TARGET_PORT=8081
if curl -sf http://127.0.0.1:8081/health > /dev/null; then
  TARGET_PORT=8082
fi

echo "🚀 Deploying new version to port ${TARGET_PORT}..."
docker compose run -d -p ${TARGET_PORT}:8000 web_app

# Wait for healthy response
until curl -sf http://127.0.0.1:${TARGET_PORT}/health; do
  echo "Waiting for app warmup..."
  sleep 2
done

# Switch Nginx Upstream
sed -i "s/127.0.0.1:[0-9]*/127.0.0.1:${TARGET_PORT}/" /etc/nginx/conf.d/upstream.conf
nginx -s reload

echo "✨ Cutover complete! Zero downtime achieved."
Tags: #gitops #docker #cicd #automation #linux