name: Deploy to Production on: workflow_dispatch: inputs: image_tag: description: "Docker image tag to deploy (commit SHA or version tag, e.g. v1.0.0)" required: true default: "latest" type: string env: FRONTEND_IMAGE: git.koppkb.com/kopkb/my-kopkb-fe DEPLOY_DIR: /home/arrahn/Project jobs: deploy: runs-on: mykopkb-production steps: - name: Checkout repository uses: actions/checkout@v4 with: ref: ${{ inputs.image_tag }} - name: Login to Docker registry run: | echo "${{ secrets.REGISTRY_PASSWORD }}" | \ docker login git.koppkb.com \ -u "${{ secrets.REGISTRY_USERNAME }}" \ --password-stdin - name: Verify image exists in registry run: | set -e IMAGE_TAG="${{ inputs.image_tag }}" IMAGE="${FRONTEND_IMAGE}:${IMAGE_TAG}" echo "Checking if image exists: ${IMAGE}" if ! docker manifest inspect "${IMAGE}" > /dev/null 2>&1; then echo "ERROR: Image ${IMAGE} does not exist in registry!" exit 1 fi echo "✓ ${IMAGE} found" - name: Pull Docker image run: | set -e IMAGE_TAG="${{ inputs.image_tag }}" docker pull "${FRONTEND_IMAGE}:${IMAGE_TAG}" echo "✓ Image pulled successfully" - name: Sync compose file and deploy run: | set -e IMAGE_TAG="${{ inputs.image_tag }}" mkdir -p "${DEPLOY_DIR}" cp docker/docker-compose.production.yml "${DEPLOY_DIR}/docker-compose.frontend.yml" docker network inspect mykopkb-production-network >/dev/null 2>&1 \ || docker network create mykopkb-production-network cd "${DEPLOY_DIR}" export IMAGE_TAG docker rm -f mykopkb-fe-production || true docker compose -f docker-compose.frontend.yml -p mykopkb-fe pull docker compose -f docker-compose.frontend.yml -p mykopkb-fe up -d echo "✓ Deployed frontend IMAGE_TAG=${IMAGE_TAG}" - name: Verify deployment run: | set -e cd "${DEPLOY_DIR}" echo "Waiting for the frontend to start..." sleep 10 STATUS=$(docker compose -f docker-compose.frontend.yml -p mykopkb-fe ps --status running --format '{{.Name}}' frontend 2>/dev/null || true) if [ -z "$STATUS" ]; then echo "ERROR: frontend is not running" docker compose -f docker-compose.frontend.yml -p mykopkb-fe ps docker compose -f docker-compose.frontend.yml -p mykopkb-fe logs --tail=50 frontend || true exit 1 fi echo "✓ frontend is running (${STATUS})" FE_PORT=$(grep -E '^FE_PORT=' .env 2>/dev/null | cut -d= -f2- | tr -d '"' || true) FE_PORT=${FE_PORT:-8081} for i in 1 2 3 4 5 6; do if curl -sf "http://127.0.0.1:${FE_PORT}/health" > /dev/null; then echo "✓ frontend health check passed (http://127.0.0.1:${FE_PORT}/health)" exit 0 fi echo "Waiting for frontend... attempt ${i}/6" sleep 5 done echo "ERROR: frontend health check failed (http://127.0.0.1:${FE_PORT}/health)" docker compose -f docker-compose.frontend.yml -p mykopkb-fe logs --tail=50 frontend || true exit 1