Sıfırdan CI/CD Pipeline Kurulumu: GitLab CI Örneği
Sıfırdan CI/CD Pipeline Kurulumu: GitLab CI Örneği
CI/CD pipeline kurmak zor değil - doğru kurmak zor. Manual deployment yapıyorsanız ve "production'a her deploy korku filmi" diyorsanız, bu yazı tam size.
CI/CD Nedir? (Gerçekten)
Continuous Integration: Her kod push'unda otomatik build + test. Merge conflict'ler erken yakalanır.
Continuous Deployment: Test geçerse otomatik production'a git. Manual approval yok (veya minimal).
Pipeline Aşamaları
# .gitlab-ci.yml
stages:
- build
- test
- security
- deploy
variables:
DOCKER_DRIVER: overlay2
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
build:
stage: build
image: docker:latest
services:
- docker:dind
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $IMAGE_TAG .
- docker push $IMAGE_TAG
only:
- merge_requests
- main
unit-tests:
stage: test
image: node:18
script:
- npm ci
- npm run test:unit
- npm run test:coverage
coverage: '/Lines\s*:\s*(\d+\.?\d*)%/'
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage/cobertura-coverage.xml
integration-tests:
stage: test
image: $IMAGE_TAG
services:
- postgres:14
- redis:7
variables:
DATABASE_URL: postgres://test:test@postgres/test
REDIS_URL: redis://redis:6379
script:
- npm run test:integration
security-scan:
stage: security
image: aquasec/trivy:latest
script:
- trivy image --exit-code 0 --severity HIGH,CRITICAL $IMAGE_TAG
- trivy image --exit-code 1 --severity CRITICAL $IMAGE_TAG
allow_failure: false
deploy-staging:
stage: deploy
image: bitnami/kubectl:latest
script:
- kubectl config use-context staging
- kubectl set image deployment/myapp myapp=$IMAGE_TAG
- kubectl rollout status deployment/myapp
environment:
name: staging
url: https://staging.example.com
only:
- main
deploy-production:
stage: deploy
image: bitnami/kubectl:latest
script:
- kubectl config use-context production
- kubectl set image deployment/myapp myapp=$IMAGE_TAG
- kubectl rollout status deployment/myapp
environment:
name: production
url: https://example.com
when: manual # Manual approval gerekli
only:
- main
Best Practices
1. Fail Fast Principle
Lint → Unit Test → Integration Test → Security sırasıyla. Lint fail ederse integration test çalıştırma (zaman kaybı).
2. Caching Kullan
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- .npm/
node_modules her sefer download etme. Pipeline 10 dakikadan 2 dakikaya düşer.
3. Parallel Jobs
test:unit:
stage: test
script: npm run test:unit
test:integration:
stage: test
script: npm run test:integration
test:e2e:
stage: test
script: npm run test:e2e
3 job paralel çalışır. Toplam süre minimize.
4. Environment Variables - Secrets
GitLab CI/CD Settings → Variables:
- AWS_ACCESS_KEY_ID (Protected + Masked)
- DATABASE_PASSWORD (Masked)
- KUBE_CONFIG (File type)
.gitlab-ci.yml'de plaintext secret asla!
5. Rollback Strategy
rollback:
stage: deploy
image: bitnami/kubectl:latest
script:
- kubectl rollout undo deployment/myapp
when: manual
only:
- main
Monitoring Pipeline Performance
Metrics izleyin:
- Pipeline duration (target: <10 min)
- Success rate (target: >95%)
- Deploy frequency (DORA metrics)
- Lead time (commit → production)
Advanced: Multi-Environment Strategy
Workflow:
- Feature branch → Auto deploy to preview env
- Merge to main → Auto deploy to staging
- Tag release → Deploy to production (manual approval)
Sonuç
CI/CD pipeline production'a güvenle deploy etmenin anahtarı. Automation ile human error eliminate edilir.
Devups CI/CD Setup Service: Pipeline kurulumundan monitoring'e full support. İletişime geçin.