GitHub Actions for CI/CD
Automate workflows with GitHub Actions.
By EMEPublished: February 20, 2025
github actionsci/cdautomationworkflows
A Simple Analogy
GitHub Actions are like a helpful robot. Every time code changes, the robot automatically runs tests, builds, and deploys.
Why GitHub Actions?
- Native: Built into GitHub
- Free: Unlimited runs for public repos
- Flexible: Run any code
- Secrets: Secure credential management
- Matrices: Test multiple configurations
Basic Workflow
name: CI
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Upload coverage
uses: codecov/codecov-action@v3
Deployment Workflow
name: Deploy
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Deploy to production
run: |
echo "Deploying to Azure..."
az appservice deployment source config-zip \
--resource-group mygroup \
--name myapp \
--src-path ./app.zip
env:
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
Matrix Testing
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x, 18.x, 20.x]
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm test
Secrets & Variables
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy
run: npm run deploy
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
API_KEY: ${{ secrets.API_KEY }}
ENVIRONMENT: ${{ vars.DEPLOYMENT_ENV }}
Best Practices
- Cache dependencies: Speed up runs
- Fail fast: Run quick checks first
- Concurrency: Use matrix for speed
- Status checks: Required before merge
- Notifications: Alert on failures
Related Concepts
- Azure Pipelines
- GitLab CI
- Jenkins
- CircleCI
Summary
Use GitHub Actions to automate testing, building, and deployment. Leverage workflow files for CI/CD pipelines.