Problem Statement
Explain advanced CI/CD patterns including multi-stage pipelines, pipeline orchestration, trunk-based development workflows, and deployment strategies integration.
Explanation
Multi-stage pipelines organize CI/CD into logical phases with progression gates. Example GitLab:
```yaml
stages:
- build
- test
- security
- staging
- production
build:
stage: build
script: make build
artifacts:
paths: [dist/]
unit_test:
stage: test
script: make test
integration_test:
stage: test
script: make integration-test
needs: [build]
sast:
stage: security
script: semgrep scan
deploy_staging:
stage: staging
script: deploy staging
environment:
name: staging
url: https://staging.example.com
deploy_production:
stage: production
script: deploy production
when: manual
only: [main]
environment:
name: production
url: https://example.com
```
Pipeline orchestration coordinates multiple pipelines across repositories. Parent-child pipelines in GitLab:
```yaml
# Parent pipeline
trigger_backend:
stage: test
trigger:
project: team/backend
strategy: depend # Wait for triggered pipeline
trigger_frontend:
stage: test
trigger:
project: team/frontend
```
GitHub Actions workflow dependencies:
```yaml
# Backend workflow triggers frontend
- name: Trigger Frontend
uses: peter-evans/repository-dispatch@v2
with:
token: ${{ secrets.PAT }}
repository: org/frontend
event-type: backend-updated
```
Trunk-based development workflow with short-lived feature branches:
```yaml
# GitHub Actions
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
pr-checks:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm test
- run: npm run lint
deploy:
if: github.ref == 'refs/heads/main'
needs: pr-checks
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh production
```
Feature flag integration for trunk-based development:
```yaml
deploy:
stage: deploy
script:
- deploy --feature-flags=new-ui:50%
environment: production
```
Blue-green deployment pattern:
```yaml
# GitLab
stages:
- deploy
- switch
- cleanup
deploy_green:
stage: deploy
script:
- deploy green-environment
- run-smoke-tests green
environment:
name: production-green
switch_traffic:
stage: switch
script:
- switch-load-balancer blue green
when: manual
cleanup_blue:
stage: cleanup
script:
- cleanup blue-environment
when: manual
```
Canary deployment with progressive rollout:
```yaml
# GitHub Actions
- name: Deploy Canary
run: |
kubectl set image deployment/app app=myapp:${{ github.sha }}
kubectl patch deployment app -p '{"spec":{"replicas":1}}'
- name: Monitor Canary
run: ./monitor-metrics.sh
timeout-minutes: 10
- name: Full Rollout
if: success()
run: kubectl scale deployment/app --replicas=10
- name: Rollback
if: failure()
run: kubectl rollout undo deployment/app
```
Rollback mechanism:
```yaml
rollback_production:
stage: rollback
script:
- kubectl rollout undo deployment/app
when: manual
only: [main]
environment:
name: production
action: rollback
```
Approval gates for production:
```yaml
# GitLab
deploy_prod:
stage: production
script: deploy production
when: manual
only: [main]
environment:
name: production
# GitHub Actions
production:
runs-on: ubuntu-latest
environment:
name: production
required-reviewers: ['ops-team']
steps:
- run: ./deploy.sh
```
Post-deployment verification:
```yaml
verify_deployment:
stage: verify
script:
- curl https://production.example.com/health
- run-integration-tests production
retry:
max: 2
when: script_failure
```
Monitoring integration:
```yaml
- name: Send Deployment Event
run: |
curl -X POST https://monitoring.example.com/api/events \
-d '{"type":"deployment","version":"${{ github.sha }}"}'
- name: Check Error Rate
run: |
ERROR_RATE=$(query-prometheus)
if [ $ERROR_RATE -gt 1 ]; then
echo "High error rate detected"
exit 1
fi
```
Dynamic environment creation (review apps):
```yaml
review_app:
stage: review
script:
- deploy review-app-$CI_MERGE_REQUEST_IID
environment:
name: review/$CI_MERGE_REQUEST_IID
url: https://review-$CI_MERGE_REQUEST_IID.example.com
on_stop: stop_review_app
only:
- merge_requests
stop_review_app:
stage: review
script:
- destroy review-app-$CI_MERGE_REQUEST_IID
environment:
name: review/$CI_MERGE_REQUEST_IID
action: stop
when: manual
```
Understanding advanced patterns enables building sophisticated CI/CD workflows supporting modern development practices and deployment strategies.