Skip to content

GitHub Actions and Automation

GitHub Actions and Automation

Overview

Automate tests, linting, builds, and deployments triggered by repository events (pushes, PRs, releases). Use GitHub Actions for CI/CD, gated merges, and other automation.

Core concepts (short)

  • Workflow: YAML file in .github/workflows/ that defines triggers and jobs.
  • Event: what starts a workflow (push, pull_request, schedule, workflow_dispatch).
  • Job: a set of steps run on a runner (jobs run in parallel by default).
  • Step: a command or action inside a job.
  • Action: reusable piece of workflow logic (from Marketplace or your own).
  • Runner: the machine that executes jobs (GitHub-hosted or self-hosted).

Practical examples (improved)

Example: Python CI (matrix, cache, artifacts)

File: .github/workflows/python-ci.yml

name: Python CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.9, 3.10]

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}

      - name: Cache pip
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Run tests
        run: |
          pytest -q

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: pytest-results-${{ matrix.python-version }}
          path: ./reports || ./

Example: Reusable workflow + deployment badge (short)

# .github/workflows/deploy.yml
name: Deploy
on:
  workflow_call:
    inputs:
      env:
        required: true
        type: string

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Deploy step (placeholder)
        run: echo "Deploying to ${{ inputs.env }}"

You can call the reusable workflow from another workflow and expose a deployment badge in your README.

Common patterns & tips

  • Use actions/cache to speed up dependency installs (pip, npm, etc.).
  • Upload build/test artifacts with actions/upload-artifact and download them in downstream jobs with actions/download-artifact.
  • Use concurrency to cancel stale runs on long-lived branches:
concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true
  • Use strategy.matrix to test combinations (OS, language versions, database backends).
  • Use permissions and least-privilege tokens; store secrets in repository secrets and use ${{ secrets.NAME }}.
  • Prefer actions/checkout@v4 and pinned action versions like @v4 to get stable behavior.
  • Keep workflows modular and readable; split into reusable workflows when logic repeats.

Security and secrets

  • Store credentials and tokens in repository or organization secrets, not in workflow files.
  • Use id-token: write and OIDC where supported to avoid storing long-lived publish tokens.
  • Limit permissions for jobs to the minimum required (see GitHub docs on permissions for workflows).

Workflow badges and visibility

  • Add workflow badges to your README to show CI status:
[![Python CI](https://github.com/<owner>/<repo>/actions/workflows/python-ci.yml/badge.svg)](https://github.com/<owner>/<repo>/actions/workflows/python-ci.yml)

Examples above are intended to be adapted for your project. If you'd like, I can add a minimal python-ci.yml or a reusable deploy workflow to this repository and run a quick syntax check on the YAML.