
Continuous Integration and Deployment (CI/CD) used to require dedicated servers and complex configurations. GitHub Actions changed that by bringing automation directly into your repository. Now, with a simple YAML file, you can run tests, build Docker images, deploy to production, and more—all triggered automatically by pushes, pull requests, or schedules. I've migrated multiple projects to GitHub Actions, and the productivity boost is real. This guide walks you through building a practical CI/CD pipeline from scratch.
- Why GitHub Actions?
- Workflow Basics and Syntax
-
Building Your First CI Pipeline
- Running Tests Automatically
- Building and Pushing Docker Images
- Deploying to Production
- Managing Secrets and Environment Variables
- Advanced Patterns and Optimization
- Troubleshooting Common Issues
1. Why GitHub Actions?
GitHub Actions is free for public repos and offers generous minutes for private ones. It's deeply integrated with GitHub—no third-party service needed. The marketplace has thousands of pre-built actions, and workflows are version-controlled alongside your code.
Compared to Jenkins or CircleCI, Actions is simpler to set up and maintain. For most projects, it's the best choice.
2. Workflow Basics and Syntax
Workflows live in .github/workflows/ as YAML files. Each workflow has triggers, jobs, and steps:
name: CI Pipeline
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run tests
run: npm test
Triggers define when workflows run. Jobs run in parallel by default. Steps execute commands or actions.
3. Building Your First CI Pipeline
3.1 Running Tests Automatically
Start with a simple test workflow for Node.js:
name: Test
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm ci
- run: npm test
This runs on every push and PR. Replace npm with your package manager.
3.2 Building and Pushing Docker Images
Build and push to Docker Hub or GitHub Container Registry:
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
push: true
tags: user/repo:latest
3.3 Deploying to Production
Deploy to AWS, Vercel, or your own server using SSH:
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_HOST }}
username: deploy
key: ${{ secrets.SSH_KEY }}
script: |
cd /app
git pull
docker-compose up -d
4. Managing Secrets and Environment Variables
Store sensitive data like API keys in repository secrets (Settings → Secrets). Access them with ${{ secrets.SECRET_NAME }}:
env:
API_KEY: ${{ secrets.API_KEY }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
Never commit secrets to code. Use environment-specific secrets for staging vs production.
5. Advanced Patterns and Optimization
Speed up workflows with caching and matrix builds:
- uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
strategy:
matrix:
node-version: [16, 18, 20]
Use if conditions to skip steps, and concurrency to cancel outdated runs.
6. Troubleshooting Common Issues
Check the Actions tab for logs. Common problems:
- Secrets not working: Ensure they're set in the right repo/environment.
- Builds failing: Check dependency versions and Node/Python versions.
- Timeout errors: Increase
timeout-minutesor optimize steps. - Permission denied: Add
permissionsblock to the workflow.
The GitHub Actions community forum and Stack Overflow are great for debugging.
GitHub Actions simplifies CI/CD so much that there's no excuse not to automate. Start small—run tests first, then add deployment. You'll wonder how you lived without it. What's the coolest workflow you've built? Share your setup in the comments!
Post a Comment