GITHUB_ACTIONS_OPTIMIZATION_GUIDE_Version2 - thesavant42/retrorecon GitHub Wiki

# 1. Remove Duplication and Overlap

# Avoid running the same lint or test steps in multiple workflows.
# Example: Remove duplicated CSS linting from both ci.yml and stylelint.yml unless intentional.

# 2. Use Composite Actions or Reusable Workflows

# Use composite actions for repeated steps:
# Example: Create .github/actions/setup-node-composite/action.yml to setup Node and install deps.

# Use reusable workflows for common routines:
# Example caller:
jobs:
  call-reusable:
    uses: ./.github/workflows/my-reusable-workflow.yml
    with:
      param: value

# 3. Optimize Job and Step Execution

# Example: Caching pip and npm dependencies for speed.
- name: Cache pip
  uses: actions/cache@v4
  with:
    path: ~/.cache/pip
    key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}

- name: Cache node modules
  uses: actions/cache@v4
  with:
    path: frontend/node_modules
    key: ${{ runner.os }}-node-${{ hashFiles('frontend/package-lock.json') }}

# 4. Make Workflows Modular and Elegant

# Minimal, focused jobs. Example:
jobs:
  python-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: '3.x'
      - name: Cache pip
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
      - name: Install Python deps
        run: pip install -r requirements.txt
      - name: Run unit tests
        run: pytest -q

  frontend-lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: '20'
      - name: Cache node modules
        uses: actions/cache@v4
        with:
          path: frontend/node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('frontend/package-lock.json') }}
      - name: Install frontend deps
        run: npm --prefix frontend install
      - name: Lint CSS
        run: npm --prefix frontend run lint

# 5. Matrix Builds

# Example for multiple Python versions:
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.8, 3.11]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}

# 6. Documentation and Maintenance

# - Add comments to explain non-obvious steps.
# - Use workflow_dispatch for manual triggers.
# - Regularly prune unused steps or secrets.

# 7. Summary Table

# | Practice             | Benefit                     |
# |----------------------|----------------------------|
# | Remove duplication   | Cleaner, easier maintenance|
# | Use caching          | Faster builds              |
# | Modular jobs/workflows | Reusable, more elegant   |
# | Parallelize jobs     | Faster feedback            |
# | Use composite actions| DRY, easier updates        |
⚠️ **GitHub.com Fallback** ⚠️