Skip to content

changed-files-matrix

A job per service that changed, the usual use of a changed-files action.

The usual use of a changed-files action: build a matrix from it. One job per service that changed, each with its own image and command.

flowchart LR
    s0["test billing"]
    s1["test search"]
    s2["test web"]

The steps this script plans for a push about which nothing is known. An arrow is a needs.

# The usual use of a changed-files action: build a matrix from it. One job per
# service that changed, each with its own image and command.
#
# In a workflow that takes a job to find the changes, write them to an output,
# and read that back in `strategy.matrix`. Here the list is there already, and
# a matrix is a loop.

SERVICES = {
    "billing": {"path": "services/billing", "image": "golang:1.26", "test": "go test ./..."},
    "search": {"path": "services/search", "image": "python:3.13-slim", "test": "pip install -e . && pytest -q"},
    "web": {"path": "services/web", "image": "node:22-bookworm-slim", "test": "npm ci && npm test"},
}

# Changes to these affect every service.
SHARED = ["libs/**", "proto/**", ".ci.star"]

def ci(ctx):
    files = ctx.changed_files
    everything = not files or match_files(files, include = SHARED)

    # A filter for each service: its own directory, except its docs.
    hit = paths_filter(files, {name: [s["path"] + "/**", "!" + s["path"] + "/**/*.md"] for name, s in SERVICES.items()})

    steps = []
    for name in sorted(SERVICES):
        if everything or hit[name]:
            s = SERVICES[name]
            steps.append(step(
                "test " + name,
                image = s["image"],
                run = "cd {} && {}".format(s["path"], s["test"]),
                inputs = [s["path"]] + SHARED[:2],
            ))
    return steps
# Generated by `starci compile` from changed-files-matrix.ci.star. Edit that, not this.
#
# Each job is one step of the plan: the same image, command, tools and
# packages, given only the step's inputs. A job skips its work when it has
# already passed with the same inputs.
#
# What does not carry over:
#  - The script decides by which files a commit changed. A workflow file
#    cannot, so it does what the script does when nothing is known about the
#    changes, on every run.
#  - Passes are remembered in this repository's Actions cache, which GitHub
#    scopes by branch and evicts when old. starci's own store has neither
#    limit.

name: CI

on:
  push:
    branches: ['**']
    tags: ['**']
  pull_request:
  workflow_dispatch:

jobs:
  test-billing:
    name: "test billing"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /libs/**
            /proto/**
            /services/billing
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-test-billing-836f4d77f009068d-${{ hashFiles('libs/**', 'proto/**', 'services/billing', 'services/billing/**') }}
      - name: "test billing"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            cd services/billing && go test ./...
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            golang:1.26 sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"

  test-search:
    name: "test search"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /libs/**
            /proto/**
            /services/search
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-test-search-7236df2961ce5034-${{ hashFiles('libs/**', 'proto/**', 'services/search', 'services/search/**') }}
      - name: "test search"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            cd services/search && pip install -e . && pytest -q
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            python:3.13-slim sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"

  test-web:
    name: "test web"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /libs/**
            /proto/**
            /services/web
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-test-web-3dfffd0cd8693434-${{ hashFiles('libs/**', 'proto/**', 'services/web', 'services/web/**') }}
      - name: "test web"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            cd services/web && npm ci && npm test
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            node:22-bookworm-slim sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"
# Generated by `starci compile --target circleci` from changed-files-matrix.ci.star. Edit that, not this.
#
# Each job is one step of the plan: the same image, command, tools and
# packages, given only the step's inputs. A job skips its work when it has
# already passed with the same inputs.
#
# What does not carry over:
#  - The script reads ctx.changed_files, so its plan depends on the commit.
#    This file is the plan for a commit about which nothing is known, and is
#    the same for every push: steps the script would add or drop for a
#    particular branch or set of changed files are not represented.
#  - Passes are remembered in this project's CircleCI cache, which expires.
#    starci's own store does not.

version: 2.1

jobs:
  test-billing:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/libs/**' '/proto/**' '/services/billing'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,glob)libs/**' ':(top,glob)proto/**' ':(top,literal)services/billing' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-test-billing-836f4d77f009068d-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - run:
          name: "test billing"
          environment:
            STARCI_RUN: |-
              cd services/billing && go test ./...
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              golang:1.26 sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-test-billing-836f4d77f009068d-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  test-search:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/libs/**' '/proto/**' '/services/search'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,glob)libs/**' ':(top,glob)proto/**' ':(top,literal)services/search' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-test-search-7236df2961ce5034-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - run:
          name: "test search"
          environment:
            STARCI_RUN: |-
              cd services/search && pip install -e . && pytest -q
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              python:3.13-slim sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-test-search-7236df2961ce5034-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  test-web:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/libs/**' '/proto/**' '/services/web'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,glob)libs/**' ':(top,glob)proto/**' ':(top,literal)services/web' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-test-web-3dfffd0cd8693434-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - run:
          name: "test web"
          environment:
            STARCI_RUN: |-
              cd services/web && npm ci && npm test
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              node:22-bookworm-slim sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-test-web-3dfffd0cd8693434-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

workflows:
  ci:
    jobs:
      - test-billing
      - test-search
      - test-web

Download: changed-files-matrix.ci.star, GitHub Actions, CircleCI