# Generated by `starci compile --target circleci` from placement.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:
#  - `worker`, `prefer` and `avoid` have no equivalent here and are dropped
#    (test, benchmark, migrate staging): CircleCI chooses machines by resource
#    class, not by label. Every job uses medium.
#  - Passes are remembered in this project's CircleCI cache, which expires.
#    starci's own store does not.

version: 2.1

jobs:
  test:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top)' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-test-234d278c02bd2642-{{ 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"
          environment:
            STARCI_RUN: |-
              ./scripts/test.sh
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              alpine:3 sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-test-234d278c02bd2642-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  benchmark:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top)' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-benchmark-fdbe6488f44082da-{{ 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: "benchmark"
          environment:
            STARCI_RUN: |-
              ./scripts/bench.sh
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              alpine:3 sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-benchmark-fdbe6488f44082da-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  migrate-staging:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "migrate staging"
          environment:
            STARCI_RUN: |-
              ./scripts/migrate.sh
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              alpine:3 sh -c "$STARCI_RUN"

workflows:
  ci:
    jobs:
      - test
      - benchmark
      - migrate-staging
