CI/CD Automation
Jenkins pipelines, GitHub Actions workflows, and GitLab CI automation for continuous deployment
GitHub Actions Workflow
# .github/workflows/deploy.yml
name: Build and Deploy
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Build
run: npm run build
- name: Deploy to Vercel
if: github.ref == 'refs/heads/main'
uses: amondnet/vercel-action@v25
with:
vercel-token: ${{ secrets.VERCEL_TOKEN }}
vercel-org-id: ${{ secrets.ORG_ID }}
vercel-project-id: ${{ secrets.PROJECT_ID }}
vercel-args: '--prod'Jenkins Pipeline
// Jenkinsfile
pipeline {
agent any
environment {
DOCKER_IMAGE = 'myapp'
REGISTRY = 'registry.example.com'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh 'npm ci'
sh 'npm run build'
}
}
stage('Test') {
steps {
sh 'npm test'
}
post {
always {
junit 'test-results/*.xml'
}
}
}
stage('Docker Build') {
steps {
sh "docker build -t ${REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} ."
}
}
stage('Deploy') {
when {
branch 'main'
}
steps {
sh "docker push ${REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}"
sh "kubectl set image deployment/myapp myapp=${REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}"
}
}
}
}GitLab CI Configuration
# .gitlab-ci.yml
stages:
- build
- test
- deploy
variables:
DOCKER_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
build:
stage: build
image: node:20
script:
- npm ci
- npm run build
artifacts:
paths:
- dist/
test:
stage: test
image: node:20
script:
- npm ci
- npm test
coverage: '/Coverage: \d+\.\d+%/'
deploy_staging:
stage: deploy
script:
- docker build -t $DOCKER_IMAGE .
- docker push $DOCKER_IMAGE
- kubectl apply -f k8s/staging/
environment:
name: staging
only:
- develop
deploy_production:
stage: deploy
script:
- docker build -t $DOCKER_IMAGE .
- docker push $DOCKER_IMAGE
- kubectl apply -f k8s/production/
environment:
name: production
only:
- main
when: manualCI/CD Best Practices
- • Keep pipelines fast with parallel jobs
- • Use caching for dependencies
- • Implement automated rollbacks
- • Store secrets securely in vault
- • Use infrastructure as code
Deployment Strategies
- • Blue-green deployments
- • Canary releases
- • Rolling updates
- • Feature flags
- • A/B testing pipelines