207 lines
4.6 KiB
Markdown
207 lines
4.6 KiB
Markdown
---
|
|
description: Build CI/CD pipelines
|
|
---
|
|
|
|
# CI/CD Pipeline Builder
|
|
|
|
Generate production-ready CI/CD pipelines for multiple platforms.
|
|
|
|
## Pipeline Patterns
|
|
|
|
1. **Test Stage**: Unit, integration, E2E tests
|
|
2. **Build Stage**: Compile, bundle, containerize
|
|
3. **Security Stage**: Vulnerability scanning, SAST/DAST
|
|
4. **Deploy Stage**: Staging and production deployment
|
|
5. **Monitoring**: Pipeline metrics and alerts
|
|
|
|
## GitHub Actions Example
|
|
|
|
```yaml
|
|
name: CI/CD Pipeline
|
|
|
|
on:
|
|
push:
|
|
branches: [main, develop]
|
|
pull_request:
|
|
branches: [main]
|
|
|
|
env:
|
|
NODE_VERSION: '18'
|
|
REGISTRY: ghcr.io
|
|
|
|
jobs:
|
|
test:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Setup Node.js
|
|
uses: actions/setup-node@v4
|
|
with:
|
|
node-version: ${{ env.NODE_VERSION }}
|
|
cache: 'npm'
|
|
|
|
- name: Install dependencies
|
|
run: npm ci
|
|
|
|
- name: Run linter
|
|
run: npm run lint
|
|
|
|
- name: Run tests
|
|
run: npm test -- --coverage
|
|
|
|
- name: Upload coverage
|
|
uses: codecov/codecov-action@v3
|
|
|
|
security:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Run Trivy vulnerability scanner
|
|
uses: aquasecurity/trivy-action@master
|
|
with:
|
|
scan-type: 'fs'
|
|
severity: 'CRITICAL,HIGH'
|
|
|
|
- name: Run CodeQL analysis
|
|
uses: github/codeql-action/analyze@v2
|
|
|
|
build:
|
|
needs: [test, security]
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Log in to Container Registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Extract metadata
|
|
id: meta
|
|
uses: docker/metadata-action@v5
|
|
with:
|
|
images: ${{ env.REGISTRY }}/${{ github.repository }}
|
|
tags: |
|
|
type=ref,event=branch
|
|
type=ref,event=pr
|
|
type=semver,pattern={{version}}
|
|
type=sha,prefix={{branch}}-
|
|
|
|
- name: Build and push Docker image
|
|
uses: docker/build-push-action@v5
|
|
with:
|
|
context: .
|
|
push: true
|
|
tags: ${{ steps.meta.outputs.tags }}
|
|
labels: ${{ steps.meta.outputs.labels }}
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|
|
|
|
deploy-staging:
|
|
needs: build
|
|
if: github.ref == 'refs/heads/develop'
|
|
runs-on: ubuntu-latest
|
|
environment:
|
|
name: staging
|
|
url: https://staging.example.com
|
|
steps:
|
|
- name: Deploy to Kubernetes
|
|
run: |
|
|
kubectl set image deployment/app \
|
|
app=${{ env.REGISTRY }}/${{ github.repository }}:develop-${{ github.sha }} \
|
|
--namespace=staging
|
|
|
|
deploy-production:
|
|
needs: build
|
|
if: github.ref == 'refs/heads/main'
|
|
runs-on: ubuntu-latest
|
|
environment:
|
|
name: production
|
|
url: https://example.com
|
|
steps:
|
|
- name: Deploy to Kubernetes
|
|
run: |
|
|
kubectl set image deployment/app \
|
|
app=${{ env.REGISTRY }}/${{ github.repository }}:main-${{ github.sha }} \
|
|
--namespace=production
|
|
|
|
- name: Notify deployment
|
|
uses: slackapi/slack-github-action@v1
|
|
with:
|
|
webhook-url: ${{ secrets.SLACK_WEBHOOK }}
|
|
payload: |
|
|
{
|
|
"text": "Production deployment successful!"
|
|
}
|
|
```
|
|
|
|
## GitLab CI Example
|
|
|
|
```yaml
|
|
stages:
|
|
- test
|
|
- build
|
|
- deploy
|
|
|
|
variables:
|
|
DOCKER_DRIVER: overlay2
|
|
DOCKER_TLS_CERTDIR: "/certs"
|
|
|
|
test:
|
|
stage: test
|
|
image: node:18
|
|
cache:
|
|
paths:
|
|
- node_modules/
|
|
script:
|
|
- npm ci
|
|
- npm run lint
|
|
- npm test
|
|
coverage: '/Lines\s*:\s*(\d+\.\d+)%/'
|
|
artifacts:
|
|
reports:
|
|
coverage_report:
|
|
coverage_format: cobertura
|
|
path: coverage/cobertura-coverage.xml
|
|
|
|
security:
|
|
stage: test
|
|
image: aquasec/trivy:latest
|
|
script:
|
|
- trivy fs --severity HIGH,CRITICAL .
|
|
|
|
build:
|
|
stage: build
|
|
image: docker:latest
|
|
services:
|
|
- docker:dind
|
|
script:
|
|
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
|
|
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
|
|
|
|
deploy:production:
|
|
stage: deploy
|
|
image: bitnami/kubectl:latest
|
|
script:
|
|
- kubectl set image deployment/app app=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
|
|
only:
|
|
- main
|
|
environment:
|
|
name: production
|
|
url: https://example.com
|
|
```
|
|
|
|
## When Invoked
|
|
|
|
Generate complete CI/CD pipeline configurations for your platform of choice with best practices.
|