CircleCI Orbs Container
CircleCI Orbs Container Orchestration Docker Build Kubernetes Deploy Helm Chart ECS EKS GKE Pipeline CI/CD Reusable Package
| Orb | Purpose | Registry | Version |
|---|---|---|---|
| circleci/docker | Docker Build Push | Docker Hub ECR GCR | 2.4.0 |
| circleci/kubernetes | kubectl Deploy | K8s Cluster | 1.3.1 |
| circleci/helm | Helm Chart Deploy | Helm Repository | 2.0.1 |
| circleci/aws-ecs | ECS Fargate Deploy | ECR | 4.0.0 |
| circleci/aws-eks | EKS Deploy | ECR + EKS | 2.2.0 |
| circleci/gcp-gke | GKE Deploy | GCR + GKE | 2.1.0 |
Pipeline Configuration
# === CircleCI config.yml Example ===
# .circleci/config.yml
# version: 2.1
#
# orbs:
# docker: circleci/docker@2.4.0
# kubernetes: circleci/kubernetes@1.3.1
# helm: circleci/helm@2.0.1
#
# workflows:
# build-and-deploy:
# jobs:
# - docker/publish:
# image: myorg/myapp
# tag:
# registry:
# filters:
# branches:
# only: main
# - helm/upgrade-helm-chart:
# chart: ./charts/myapp
# release-name: myapp
# namespace: production
# values-to-override: image.tag=
# requires:
# - docker/publish
# filters:
# branches:
# only: main
from dataclasses import dataclass
@dataclass
class PipelineStage:
stage: str
orb: str
commands: str
duration: str
dependencies: str
stages = [
PipelineStage("Checkout & Test",
"Built-in",
"checkout, run tests, lint, type-check",
"2-5 นาที",
"None"),
PipelineStage("Docker Build & Push",
"circleci/docker@2.4.0",
"docker/publish: build image, tag, push to registry",
"3-10 นาที (cached: 1-3 นาที)",
"Test passed"),
PipelineStage("Security Scan",
"circleci/snyk@1.0 หรือ Custom",
"snyk container test, trivy image scan",
"1-3 นาที",
"Image built"),
PipelineStage("Deploy Staging",
"circleci/helm@2.0.1",
"helm upgrade --install staging, smoke test",
"2-5 นาที",
"Scan passed"),
PipelineStage("Approval Gate",
"Built-in (type: approval)",
"Manual approval จาก Team Lead",
"รอ Approve",
"Staging passed"),
PipelineStage("Deploy Production",
"circleci/helm@2.0.1",
"helm upgrade --install production, health check",
"2-5 นาที",
"Approved"),
]
print("=== Pipeline Stages ===")
for s in stages:
print(f"\n [{s.stage}]")
print(f" Orb: {s.orb}")
print(f" Commands: {s.commands}")
print(f" Duration: {s.duration}")
print(f" Depends: {s.dependencies}")
Docker & Kubernetes Orbs
# === Orb Usage Examples ===
@dataclass
class OrbExample:
orb: str
use_case: str
key_params: str
env_vars: str
tip: str
examples = [
OrbExample("circleci/docker@2.4.0",
"Build Multi-stage Docker Image Push to ECR",
"image, tag, registry, dockerfile, path, extra_build_args",
"DOCKER_LOGIN, DOCKER_PASSWORD หรือ AWS_ACCESS_KEY_ID",
"ใช้ Docker Layer Cache: docker/publish cache_from param"),
OrbExample("circleci/kubernetes@1.3.1",
"kubectl apply Deployment Service Ingress",
"kubeconfig, namespace, resource-file-path",
"KUBECONFIG (base64 encoded)",
"ใช้ kubernetes/install-kubectl ก่อน แล้ว kubectl apply"),
OrbExample("circleci/helm@2.0.1",
"Helm Chart upgrade install สำหรับ Kubernetes",
"chart, release-name, namespace, values-to-override",
"KUBECONFIG, HELM_REPO_URL",
"ใช้ values-to-override แทน values file สำหรับ Dynamic"),
OrbExample("circleci/aws-ecs@4.0.0",
"Update ECS Service Task Definition Fargate",
"family, cluster, service-name, container-image-name-updates",
"AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION",
"ใช้ verify-revision-is-deployed รอจน Deploy เสร็จ"),
OrbExample("circleci/aws-eks@2.2.0",
"Deploy to EKS Cluster kubectl helm",
"cluster-name, aws-region",
"AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY",
"ใช้ aws-eks/update-kubeconfig ก่อน แล้ว kubectl/helm"),
]
print("=== Orb Examples ===")
for e in examples:
print(f"\n [{e.orb}]")
print(f" Use: {e.use_case}")
print(f" Params: {e.key_params}")
print(f" Env: {e.env_vars}")
print(f" Tip: {e.tip}")
Best Practices
# === CI/CD Best Practices ===
@dataclass
class BestPractice:
practice: str
why: str
how: str
impact: str
practices = [
BestPractice("Pin Orb Versions",
"ป้องกัน Breaking Change จาก Orb Update",
"ระบุ @2.4.0 ไม่ใช่ @volatile หรือ latest",
"Stability: ไม่พัง Pipeline กะทันหัน"),
BestPractice("Docker Layer Cache",
"ลดเวลา Build Docker Image จาก 10 นาที เหลือ 1-2 นาที",
"เปิด Docker Layer Caching ใน CircleCI Settings",
"Speed: ลดเวลา Build 70-80%"),
BestPractice("Multi-stage Build",
"ลด Image Size ไม่เอา Build Dependencies เข้า Production",
"FROM node:20 AS builder ... FROM node:20-slim ...",
"Size: ลด Image จาก 1GB เหลือ 100-200MB"),
BestPractice("Image Scanning",
"หา Vulnerability ก่อน Deploy ไป Production",
"ใส่ Trivy/Snyk Scan Stage ก่อน Deploy",
"Security: พบ CVE ก่อน Production"),
BestPractice("Approval for Production",
"ป้องกัน Deploy ที่ไม่ได้ตั้งใจ",
"ใส่ type: approval Job ก่อน Production Deploy",
"Safety: Manual Gate สำหรับ Production"),
BestPractice("Semantic Image Tags",
"ติดตามว่า Image ไหน Deploy อยู่",
"Tag ด้วย Git SHA: myapp:abc1234 ไม่ใช่ latest",
"Traceability: รู้ว่า Commit ไหน Deploy"),
]
print("=== Best Practices ===")
for p in practices:
print(f" [{p.practice}] {p.why}")
print(f" How: {p.how}")
print(f" Impact: {p.impact}")
เคล็ดลับ
- Orbs: ใช้ Certified Orbs ก่อน ปลอดภัยและ Maintain ดี
- Cache: เปิด Docker Layer Cache ลดเวลา Build 70-80%
- Scan: Scan Image ทุกครั้งก่อน Push ไป Registry
- Tag: ใช้ Git SHA เป็น Image Tag ไม่ใช่ latest
- Approval: ใส่ Approval Gate ก่อน Production Deploy
การดูแลระบบในสภาพแวดล้อม Production
การบริหารจัดการระบบ Production ที่ดีต้องมี Monitoring ครอบคลุม ใช้เครื่องมืออย่าง Prometheus + Grafana สำหรับ Metrics Collection และ Dashboard หรือ ELK Stack สำหรับ Log Management ตั้ง Alert ให้แจ้งเตือนเมื่อ CPU เกิน 80% RAM ใกล้เต็ม หรือ Disk Usage สูง
Backup Strategy ต้องวางแผนให้ดี ใช้หลัก 3-2-1 คือ มี Backup อย่างน้อย 3 ชุด เก็บใน Storage 2 ประเภทต่างกัน และ 1 ชุดต้องอยู่ Off-site ทดสอบ Restore Backup เป็นประจำ อย่างน้อยเดือนละครั้ง เพราะ Backup ที่ Restore ไม่ได้ก็เหมือนไม่มี Backup
เรื่อง Security Hardening ต้องทำตั้งแต่เริ่มต้น ปิด Port ที่ไม่จำเป็น ใช้ SSH Key แทน Password ตั้ง Fail2ban ป้องกัน Brute Force อัพเดท Security Patch สม่ำเสมอ และทำ Vulnerability Scanning อย่างน้อยเดือนละครั้ง ใช้หลัก Principle of Least Privilege ให้สิทธิ์น้อยที่สุดที่จำเป็น
CircleCI Orbs คืออะไร
Reusable Package Commands Jobs Executors Docker Kubernetes Helm ECS EKS GKE ติดตั้งง่าย ลดเวลาเขียน Pipeline 60-80%
ใช้กับ Container อย่างไร
Docker Orb Build Push Kubernetes kubectl Helm Chart AWS ECS EKS GCP GKE Kustomize ArgoCD GitOps Registry ECR GCR Docker Hub
ตั้งค่า Pipeline อย่างไร
.circleci/config.yml version 2.1 orbs workflows jobs filters branches env vars circleci local execute Auto-trigger Push
Best Practices คืออะไร
Pin Version Certified Orbs Docker Layer Cache Multi-stage Build Image Scan Trivy Snyk Semantic Tag Approval Gate Context Secrets
สรุป
CircleCI Orbs Container Docker Build Kubernetes Helm ECS EKS GKE Pipeline Cache Scan Approval Semantic Tag CI/CD Best Practices
