Skip to main content

GitHub Actions

This file covers GitHub Actions for CI/CD automation and workflow management.

What are GitHub Actions?​

GitHub Actions is a CI/CD platform that allows you to automate your build, test, and deployment pipeline directly in your GitHub repository.

Basic 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@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "18"
cache: "npm"

- name: Install dependencies
run: npm ci

- name: Run tests
run: npm test

- name: Run linter
run: npm run lint

Common Workflows​

Node.js Application​

name: Node.js CI

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test

Python Application​

name: Python CI

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: [3.8, 3.9, 3.10]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: pytest

Deployment Workflow​

name: Deploy

on:
push:
branches: [main]

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Deploy to staging
run: |
# Deployment commands here
echo "Deploying to staging..."

- name: Run smoke tests
run: |
# Smoke test commands
echo "Running smoke tests..."

Best Practices​

  • Use official actions when available
  • Cache dependencies for faster builds
  • Use matrix builds for multiple environments
  • Store secrets securely
  • Use environment protection rules
  • Implement proper error handling
  • Monitor workflow performance