Skip to content

changed-files

match_files and paths_filter: what changed-files and paths-filter actions are for.

What tj-actions/changed-files and dorny/paths-filter are used for, without an action: ask which of the changed files match some globs.

flowchart LR
    s0["api tests"]
    s1["terraform fmt"]

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

# What tj-actions/changed-files and dorny/paths-filter are used for, without
# an action: ask which of the changed files match some globs.
#
#   match_files(files, include = [], exclude = [])   the files that match
#   paths_filter(files, {name: [globs]})             a list of matches per name
#
# Both take a list of paths, here ctx.changed_files. Globs match the whole
# path: * stays inside a directory, ** does not, **/ may match no directory
# at all, {a,b} is either, and a pattern starting with ! takes away what it
# matches from the rest of its list.

FILTERS = {
    "api": ["api/**", "!api/**/*.md"],
    "ui": ["ui/**/*.{ts,tsx,css}", "ui/package*.json"],
    "docs": ["docs/**", "**/*.md"],
    "infra": ["terraform/**", ".github/**"],
}

def ci(ctx):
    # With no list of changes to go on, everything counts as changed.
    changed = ctx.changed_files or ["api/", "ui/", "docs/", "terraform/"]
    hit = paths_filter(changed, FILTERS)
    steps = []

    if hit["api"]:
        steps.append(step("api tests", image = "python:3.13-slim", run = "cd api && pip install -e . && pytest -q", inputs = ["api"]))
    if hit["ui"]:
        steps.append(step("ui tests", image = "node:22-bookworm-slim", run = "cd ui && npm ci && npm test", inputs = ["ui"]))

    # Files, not just whether there were any: check only the Markdown that
    # changed, apart from the generated changelog.
    markdown = match_files(changed, include = ["**/*.md"], exclude = ["CHANGELOG.md"])
    if markdown:
        steps.append(step(
            "markdown lint",
            image = "davidanson/markdownlint-cli2:latest",
            run = "markdownlint-cli2 " + " ".join(markdown),
            inputs = markdown,
        ))

    if hit["infra"]:
        steps.append(step("terraform fmt", image = "hashicorp/terraform:1.9", run = "terraform fmt -check -recursive terraform", inputs = ["terraform"]))
    return steps
# Generated by `starci compile` from changed-files.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:
  api-tests:
    name: "api tests"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /api
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-api-tests-5bc1e5c11433e410-${{ hashFiles('api', 'api/**') }}
      - name: "api tests"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            cd api && 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"

  terraform-fmt:
    name: "terraform fmt"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /terraform
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-terraform-fmt-0e31302fd31c38ab-${{ hashFiles('terraform', 'terraform/**') }}
      - name: "terraform fmt"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            terraform fmt -check -recursive terraform
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            hashicorp/terraform:1.9 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.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:
  api-tests:
    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 '/api'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)api' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-api-tests-5bc1e5c11433e410-{{ 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: "api tests"
          environment:
            STARCI_RUN: |-
              cd api && 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-api-tests-5bc1e5c11433e410-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  terraform-fmt:
    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 '/terraform'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)terraform' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-terraform-fmt-0e31302fd31c38ab-{{ 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: "terraform fmt"
          environment:
            STARCI_RUN: |-
              terraform fmt -check -recursive terraform
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              hashicorp/terraform:1.9 sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-terraform-fmt-0e31302fd31c38ab-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

workflows:
  ci:
    jobs:
      - api-tests
      - terraform-fmt

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