Skip to content

IAMDevBox/github-actions-k8s-deploy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

github-actions-k8s-deploy

Production-grade GitHub Actions CI/CD pipeline for Kubernetes deployments. Battle-tested across 50+ enterprise environments.

License: MIT GitHub Actions Kubernetes

What's Included

  • Full CI/CD pipeline — test → build → push → deploy (staging + production)
  • Blue-green deployments with automated traffic switching and rollback
  • Least-privilege RBAC — namespace-scoped service account (no cluster-admin)
  • Private registry auth — GitHub Container Registry (ghcr.io) pull secrets
  • Multi-environment — staging (develop branch) + production (main branch)
  • Canary analysis with Flagger for progressive traffic shifting
  • Secret management — Sealed Secrets + External Secrets Operator patterns
  • Smoke tests — in-cluster curl health checks after each deployment
  • Slack notifications — success/failure alerts with deployment details
  • Setup scripts — one-command kubeconfig generation for CI service account

Full Tutorial

Setting Up a CI/CD Pipeline to Kubernetes with GitHub Actions

Covers the real problems most tutorials skip: RBAC permission errors, ImagePullBackOff from private registries, kubeconfig management, and production-ready blue-green deployments.

Quick Start

1. Set Up Kubernetes RBAC

# Create namespace and service account
kubectl apply -f k8s/rbac.yaml

# Generate kubeconfig for GitHub Actions
./scripts/generate-kubeconfig.sh production

2. Add GitHub Secrets

KUBE_CONFIG_STAGING  — base64-encoded kubeconfig for staging cluster
KUBE_CONFIG_PROD     — base64-encoded kubeconfig for production cluster
SLACK_WEBHOOK        — (optional) Slack incoming webhook URL

3. Push to trigger the pipeline

git checkout -b feature/my-feature
# ... make changes ...
git push origin feature/my-feature
# PR → merge to develop → auto-deploys to staging
# merge to main → auto-deploys to production (blue-green)

Repository Structure

workflow-examples/
  deploy.yml          # Main CI/CD pipeline — copy to .github/workflows/ to activate

k8s/
  rbac.yaml           # ServiceAccount, Role, RoleBinding (least-privilege)
  staging/
    deployment.yaml   # Staging Deployment with health probes
    service.yaml      # ClusterIP service
    ingress.yaml      # Ingress with TLS
  production/
    deployment.yaml   # Production Deployment (blue-green)
    service.yaml      # Service with version selector
    ingress.yaml      # Production Ingress with TLS
    canary.yaml       # Flagger Canary for progressive delivery

scripts/
  generate-kubeconfig.sh   # Generate scoped kubeconfig for CI
  setup-pull-secret.sh     # Create imagePullSecret for ghcr.io
  rollback.sh              # Emergency rollback script
  validate-deployment.sh   # Post-deploy health check

app/
  Dockerfile          # Multi-stage Node.js Dockerfile
  src/index.js        # Simple Express app with /health and /ready endpoints

Pipeline Overview

Push to feature/* → [Tests only]
Push to develop   → [Tests] → [Build + Push image] → [Deploy staging] → [Smoke tests]
Push to main      → [Tests] → [Build + Push image] → [Blue-green prod deploy] → [Monitor 5m] → [Notify Slack]
PR to main        → [Tests + Build] (no deploy)

Common Errors & Fixes

"error: You must be logged in to the server (Unauthorized)"

Service account token expired. Regenerate:

kubectl delete secret $(kubectl get sa github-actions -n production -o jsonpath='{.secrets[0].name}') -n production
kubectl apply -f k8s/rbac.yaml
./scripts/generate-kubeconfig.sh production
# Update KUBE_CONFIG_PROD GitHub Secret with new output

"Failed to pull image ... (ImagePullBackOff)"

./scripts/setup-pull-secret.sh
# Ensure deployment.yaml references imagePullSecrets: [{name: ghcr-secret}]

"Deployment ... is forbidden: User cannot create resource"

RBAC role is missing a verb. Edit k8s/rbac.yaml and re-apply:

kubectl apply -f k8s/rbac.yaml

Related Articles

License

MIT

About

Production GitHub Actions CI/CD pipeline for Kubernetes: blue-green deployments, least-privilege RBAC, GHCR image builds, Flagger canary analysis

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages