CI/CDGitHub ActionsDockerDevOps
Modern CI/CD Pipelines with GitHub Actions
·6 min read
Modern CI/CD Pipelines with GitHub Actions
Continuous Integration and Continuous Deployment (CI/CD) are essential practices for modern software development. In this post, I'll show you how to set up a robust pipeline using GitHub Actions.
The Pipeline Overview
Our pipeline will: 1. Run tests on every push 2. Build Docker images 3. Deploy to staging automatically 4. Deploy to production with manual approval
Basic Workflow Structure
# .github/workflows/ci.yml
name: CI/CD Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
test:
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: Run linting
run: npm run lintBuilding and Pushing Docker Images
build:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=maxDeployment with Environment Protection
deploy-production:
needs: build
runs-on: ubuntu-latest
environment: production
steps:
- name: Deploy to Kubernetes
run: |
kubectl set image deployment/app \
app=ghcr.io/${{ github.repository }}:${{ github.sha }}Best Practices
1. Cache Dependencies: Use caching to speed up builds 2. Use Matrix Builds: Test across multiple Node.js versions 3. Implement Environment Protection: Require approvals for production 4. Keep Secrets Secure: Never hardcode credentials
A well-designed CI/CD pipeline can dramatically improve your team's productivity and deployment confidence.