DevOps Lesson 6: CI/CD with GitHub Actions

⚙️ DevOps CourseLesson 6 of 10 · 60% complete

CI/CD automates: run tests, build, and deploy on every code push. GitHub Actions is free for public repos and 2000 minutes/month for private.

Your First Workflow

# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4          # checkout code
      
      - uses: actions/setup-node@v4        # setup Node.js
        with:
          node-version: "20"
          cache: "npm"
      
      - run: npm ci                         # install dependencies
      - run: npm run lint                   # lint code
      - run: npm test                       # run tests
      - run: npm run build                  # build

Full CI/CD Pipeline

# Deploy job runs AFTER test passes
deploy:
  needs: test  # wait for test to complete
  runs-on: ubuntu-latest
  if: github.ref == 'refs/heads/main'  # only on main branch
  
  steps:
    - uses: actions/checkout@v4
    
    - name: Build Docker image
      run: docker build -t myapp:${{ github.sha }} .
    
    - name: Push to Docker Hub
      run: |
        echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
        docker push myapp:${{ github.sha }}
    
    - name: Deploy to Railway
      run: npx railway up
      env:
        RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}

🏋️ Practice Task

Set up a GitHub Actions workflow for your Node.js API: (1) Run on every push to main. (2) Install dependencies. (3) Run tests (create at least one test with Jest). (4) If tests pass, deploy to Railway automatically. Add secrets in GitHub Settings → Secrets.

💡 Hint: npm install -D jest. Create app.test.js. Add “test”:”jest” to package.json scripts.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *