What is CI/CD?
Continuous Integration/Continuous Deployment automates testing and deployment of your code, ensuring quality and faster releases.
GitHub Actions Workflow
# .github/workflows/ci-cd.yml
name: CI/CD Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Run linting
run: npm run lint
build:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build Docker image
run: docker build -t my-api:${{ github.sha }} .
- name: Push to registry
run: |
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
docker push my-api:${{ github.sha }}
deploy:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Deploy to production
run: |
kubectl set image deployment/api-deployment api=my-api:${{ github.sha }}
GitLab CI Configuration
# .gitlab-ci.yml
stages:
- test
- build
- deploy
test:
stage: test
image: node:18
script:
- npm ci
- npm test
- npm run lint
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:
stage: deploy
image: bitnami/kubectl:latest
script:
- kubectl set image deployment/api-deployment api=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
only:
- main
Jenkins Pipeline
# Jenkinsfile
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'npm ci'
sh 'npm test'
}
}
stage('Build') {
steps {
sh 'docker build -t my-api:${BUILD_NUMBER} .'
}
}
stage('Deploy') {
steps {
sh 'kubectl set image deployment/api-deployment api=my-api:${BUILD_NUMBER}'
}
}
}
}
Best Practices
- Run tests before building
- Use semantic versioning
- Tag Docker images with commit SHA
- Deploy only from main branch
- Use secrets for sensitive data
- Implement rollback strategies