version: 2.1

orbs:
  kurtosis-docs-checker: kurtosis-tech/docs-checker@0.2.3

executors:
  ubuntu_vm:
    machine:
      image: ubuntu-2004:202201-02

parameters:
  should-enable-check-latest-version-workflow:
    type: boolean
    default: false
  # To enable/disabled the check_code workflow execution which will be triggered by the PR's checkers
  should-enable-build-workflow:
    type: boolean
    default: true

# Install go version 1.19 to run our integration tests on the minimal version we support
setup_kurtosis: &setup_kurtosis
  - run: |
      echo "deb [trusted=yes] https://apt.fury.io/kurtosis-tech/ /" | sudo tee /etc/apt/sources.list.d/kurtosis.list
      sudo apt update
      sudo apt install kurtosis-cli
      kurtosis analytics disable
      kurtosis engine restart


# NOTE: Because CircleCI jobs run on separate machines from each other, we duplicate steps (like checkout) between jobs. This is because doing the "correct" DRY
#  refactoring of, "one job for checkout, one job for build Docker image, etc." would require a) persisting files between jobs and b) persisting Docker images between
#  jobs. Both are annoying (saving/loading workspaces require re-downloading the workspace over the network, and there doesn't seem to be a good way to do Docker
#  images), so we run everything inside a single job.
# See also: https://discuss.circleci.com/t/can-docker-images-be-preserved-between-jobs-in-a-workflow-without-a-manual-load-save/23388/12
jobs:
  run_starlark:
    executor: ubuntu_vm
    steps:
      - <<: *setup_kurtosis
      - checkout

      - run: kurtosis run ${PWD}

  lint:
    executor: ubuntu_vm
    steps:
      - <<: *setup_kurtosis
      - checkout

      - run: kurtosis lint ${PWD}

  run_starlark_arm64:
    executor: ubuntu_vm
    resource_class: arm.medium
    steps:
      - <<: *setup_kurtosis
      - checkout

      - run: kurtosis run ${PWD}

  check_latest_version:
    executor: ubuntu_vm
    steps:
      - <<: *setup_kurtosis
      - checkout

      - run: kurtosis run --dry-run ${PWD}

  besu-all:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/besu-all.json)"

  erigon-all:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/erigon-all.json)"

  ethereumjs-all:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout

      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/ethereumjs-all.json)"

  geth-all:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/geth-all.json)"

  lighthouse-all:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/lighthouse-all.json)"

  lodestar-all:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/lodestar-all.json)"

  mix:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/mix.json)"

  nethermind-all:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/nethermind-all.json)"

  nimbus-all:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/nimbus-all.json)"

  prysm-all:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/prysm-all.json)"

  reth-all:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/reth-all.json)"

  teku-all:
    executor: ubuntu_vm
    resource_class: xlarge
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/teku-all.json)"

  mev:
    executor: ubuntu_vm
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/mev.json)"

  complex_mev:
    executor: ubuntu_vm
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/complex-mev.json)"

  nimbus_mev:
    executor: ubuntu_vm
    steps:
      - <<: *setup_kurtosis
      - checkout
      - run: kurtosis run ${PWD} "$(cat ./.circleci/tests/nimbus-mev.json)"

workflows:
  check_latest_version:
    when: << pipeline.parameters.should-enable-check-latest-version-workflow >>
    jobs:
      - check_latest_version
      - besu-all
      - erigon-all
      - ethereumjs-all
      - geth-all
      - lighthouse-all
      - lodestar-all
      - mix
      - nethermind-all
      - nimbus-all
      - prysm-all
      - reth-all
      - teku-all
      - nimbus_mev
      - complex_mev

  build:
    when: << pipeline.parameters.should-enable-build-workflow >>
    jobs:
      # -- PR check jobs ------------------------------------------
      - kurtosis-docs-checker/check-docs:
          should-check-changelog: false
          markdown-link-check-config-json: |
            {
                "ignorePatterns": [
                    {
                        "pattern": "https://github.com/kurtosis-tech/ethereum-package"
                    }
                ]
            }
          filters:
            branches:
              ignore:
                - main
      - run_starlark:
          filters:
            branches:
              ignore:
                - main
      - run_starlark_arm64:
          filters:
            branches:
              ignore:
                - main                
      - lint:
          filters:
            branches:
              ignore:
                - main                
      - mev:
          filters:
            branches:
              ignore:
                - main
