Target Flow Baru
Git Push
↓
Build image dengan tag commit
↓
Push registry
↓
Deploy image versi tertentu
↓
Bisa rollback kapan saja
STEP 1 — Update .gitlab-ci.yml
Gunakan:
stages:
- build
- deploy
variables:
IMAGE_NAME: registry.gitlab.com/project7112620/belajar-gitlab/nginx-app
IMAGE_TAG: $CI_COMMIT_SHORT_SHA
build:
image: docker:latest
tags:
- main-runner
stage: build
services:
- docker:dind
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_CERTDIR: ""
script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.gitlab.com
- docker build -t $IMAGE_NAME:$IMAGE_TAG .
- docker push $IMAGE_NAME:$IMAGE_TAG
deploy:
image: alpine:latest
tags:
- main-runner
stage: deploy
before_script:
- apk add --no-cache curl sed
- curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
- chmod +x kubectl
- mv kubectl /usr/local/bin/
variables:
KUBECONFIG: /kubeconfig
script:
- sed -i "s|IMAGE_TAG|$IMAGE_TAG|g" deployment.yaml
- kubectl apply -f deployment.yaml
STEP 2 — Update deployment.yaml
Jangan hardcode latest.
Gunakan placeholder:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app
spec:
replicas: 1
selector:
matchLabels:
app: nginx-app
template:
metadata:
labels:
app: nginx-app
spec:
imagePullSecrets:
- name: gitlab-registry
containers:
- name: nginx-app
image: registry.gitlab.com/project7112620/belajar-gitlab/nginx-app:IMAGE_TAG
imagePullPolicy: Always
ports:
- containerPort: 80
Bisa ambil code dari -> https://github.com/kyuby13/belajar-gitlab-versioning
Yang Akan Terjadi
Misal commit:
a1b2c3d
pipeline otomatis build:
registry.gitlab.com/project7112620/belajar-gitlab/nginx-app:a1b2c3d
Dan Deploy
Deployment otomatis menjadi:
image: registry.gitlab.com/project7112620/belajar-gitlab/nginx-app:a1b2c3d
STEP 3 — Push
git add .
git commit -m "add version tagging"
git push
STEP 4 — Cek Registry
Di GitLab Container Registry nanti akan muncul banyak versi:
a1b2c3d
f9e8d7c
91ab234
Cara Rollback
SUPER MUDAH 🔥
OPTION 1 — Kubernetes Rollback
Lihat history:
kubectl rollout history deployment nginx-app
Rollback:
kubectl rollout undo deployment nginx-app
atau ke revisi tertentu:
kubectl rollout undo deployment nginx-app --to-revision=2
OPTION 2 — Deploy Image Lama
Manual:
kubectl set image deployment/nginx-app \
nginx-app=registry.gitlab.com/project7112620/belajar-gitlab/nginx-app:a1b2c3d
Cara Cek Versi Yang Sedang Running
kubectl describe deployment nginx-app
atau:
kubectl get deployment nginx-app -o yaml
Kenapa Ini Penting?
Karena production real-world:
- deploy bisa gagal
- bug bisa muncul
- harus rollback cepat
Makanya versioned image itu standard DevOps modern.
