# Generated by `starci compile` from shell-scripts.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 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:
  shellcheck:
    name: "shellcheck"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /scripts/*.sh
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-shellcheck-ba360bc949a2f0ba-${{ hashFiles('scripts/*.sh') }}
      - name: "Tool: shellcheck"
        if: steps.passed.outputs.cache-hit != 'true'
        run: |
          mkdir -p "$RUNNER_TEMP/starci-tools/shellcheck"
          curl -fsSL 'https://github.com/koalaman/shellcheck/releases/download/v0.11.0/shellcheck-v0.11.0.linux.x86_64.tar.xz' -o "$RUNNER_TEMP/shellcheck.download"
          echo "8c3be12b05d5c177a04c29e3c78ce89ac86f1595681cab149b65b97c4e227198  $RUNNER_TEMP/shellcheck.download" | sha256sum -c -
          tar -xf "$RUNNER_TEMP/shellcheck.download" -C "$RUNNER_TEMP/starci-tools/shellcheck"
      - name: "shellcheck"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            shellcheck scripts/*.sh
        run: |
          docker pull -q debian:12-slim
          image_path=$(docker image inspect debian:12-slim --format '{{range .Config.Env}}{{println .}}{{end}}' | sed -n 's/^PATH=//p')
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            -v "$RUNNER_TEMP/starci-tools/shellcheck:/opt/starci/tools/shellcheck:ro" \
            -e "PATH=/opt/starci/tools/shellcheck/shellcheck-v0.11.0:$image_path" \
            debian:12-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"

  json-is-valid:
    name: "json is valid"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /config/*.json
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-json-is-valid-278517b8325745c6-${{ hashFiles('config/*.json') }}
      - name: "Tool: jq"
        if: steps.passed.outputs.cache-hit != 'true'
        run: |
          mkdir -p "$RUNNER_TEMP/starci-tools/jq"
          curl -fsSL 'https://github.com/jqlang/jq/releases/download/jq-1.8.1/jq-linux-amd64' -o "$RUNNER_TEMP/jq.download"
          echo "020468de7539ce70ef1bceaf7cde2e8c4f2ca6c3afb84642aabc5c97d9fc2a0d  $RUNNER_TEMP/jq.download" | sha256sum -c -
          install -m 755 "$RUNNER_TEMP/jq.download" "$RUNNER_TEMP/starci-tools/jq/jq"
      - name: "json is valid"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            for f in config/*.json; do jq empty "$f" || exit 1; done
        run: |
          docker pull -q debian:12-slim
          image_path=$(docker image inspect debian:12-slim --format '{{range .Config.Env}}{{println .}}{{end}}' | sed -n 's/^PATH=//p')
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            -v "$RUNNER_TEMP/starci-tools/jq:/opt/starci/tools/jq:ro" \
            -e "PATH=/opt/starci/tools/jq:$image_path" \
            debian:12-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"
