# Generated by `starci compile --target circleci` from wrapping-actions.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:
#  - Passes are remembered in this project's CircleCI cache, which expires.
#    starci's own store does not.

version: 2.1

jobs:
  web-test:
    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 '/web'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)web' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-web-test-7dd2f9c3a1ad596c-{{ 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
      - restore_cache:
          name: "Cache: npm-22"
          keys:
            - starci-cache-npm-22-
      - run:
          name: "web test"
          environment:
            STARCI_RUN: |-
              npm ci && npm test
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-cache/npm-22:/root/.npm" \
              node:22-bookworm-slim sh -c "$STARCI_RUN"
      - save_cache:
          name: "Save cache: npm-22"
          key: starci-cache-npm-22-{{ .Revision }}
          paths:
            - ~/starci-cache/npm-22
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-web-test-7dd2f9c3a1ad596c-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  api-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-api-test-9155297462b3efdc-{{ 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
      - restore_cache:
          name: "Cache: pip-3-13"
          keys:
            - starci-cache-pip-3-13-
      - run:
          name: "api test"
          environment:
            STARCI_RUN: |-
              pip install -e . && pytest -q
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-cache/pip-3-13:/root/.cache/pip" \
              python:3.13-slim sh -c "$STARCI_RUN"
      - save_cache:
          name: "Save cache: pip-3-13"
          key: starci-cache-pip-3-13-{{ .Revision }}
          paths:
            - ~/starci-cache/pip-3-13
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-api-test-9155297462b3efdc-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  trivy:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "trivy"
          environment:
            STARCI_RUN: |-
              trivy fs --scanners vuln,secret --severity CRITICAL --exit-code 1 .
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              aquasec/trivy:latest sh -c "$STARCI_RUN"

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

  image:
    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-image-11ad3ee921a33f98-{{ 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: "image"
          environment:
            STARCI_RUN: |-
              docker build -t app:ci --build-arg NODE_VERSION=22 -f Dockerfile .
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              docker:27-cli sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-image-11ad3ee921a33f98-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

workflows:
  ci:
    jobs:
      - web-test
      - api-test
      - trivy
      - hadolint
      - image:
          requires: [web-test, api-test, trivy, hadolint]
