name: Build
on:
  workflow_dispatch:
  pull_request:
  push:
    branches:
      - dev.v2
jobs:
  # Run the full Tilt build and wait for it to converge
  tilt:
    # in the future, we may want to run cheap lints, tests, and builds before firing up the expensive tilt test.
    # But for now, we'll kick-off everything at once
    # needs: [go-lint-and-tests, node, algorand, ethereum, terra, rust-lint-and-tests]
    runs-on: tilt-kube-public

    # Cancel previous builds on the same branch/ref. Full runs are expensive
    # and capacity is limited, so we want to avoid running multiple builds
    # in parallel even if it means skipping CI runs on permanent branches
    # (unfortunately, we can't differentiate between temporary and permanent
    # refs without duplicating the entire logic).
    concurrency:
      group: ${{ github.workflow }}-tilt-${{ github.ref }}
      cancel-in-progress: true

    steps:
      - uses: actions/checkout@v2
      - name: Expand for link to Tilt dashboard (only available during build)
        run: >
          echo "Tilt progress dashboard: https://$DASHBOARD_URL"
      - run: |
          kubectl config set-context ci --namespace=$DEPLOY_NS
          kubectl config use-context ci

      - run: tilt ci -- --ci --namespace=$DEPLOY_NS --num=2
        timeout-minutes: 60

      # Clean up k8s resources
      - run: kubectl delete --namespace=$DEPLOY_NS service,statefulset,configmap,pod,job --all
        if: always()

  # Verify whether the Makefile builds the node (no dependencies other than Go)
  node:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2
        with:
          go-version: "1.17.5"
      - run: make node

  algorand:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v3
        with:
          python-version: "3.10"
      - run: pip install -r algorand/requirements.txt
      - run: cd algorand && make test

  ethereum:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: "16"
      - run: cd ethereum && make test

  ethereum-upgrade:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: "16"
      - run: cd clients/js && make install
      - run: cd ethereum && make test-upgrade

  terra:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: "16"
      - run: cd terra && make test
  terra-2:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: "16"
      - run: cd cosmwasm && make test

  cli:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: "16"
      - run: cd clients/js && make test

  # Run Go linters, Go tests and other outside-of-Tilt things.
  lint-and-tests:
    # The linter is slow enough that we want to run it on the self-hosted runner
    runs-on: tilt-kube-public
    concurrency:
      group: ${{ github.workflow }}-lint-${{ github.ref }}
      cancel-in-progress: true
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2
        with:
          go-version: "1.17.5"
      - name: Install formatter
        run: go install golang.org/x/tools/cmd/goimports@latest
      - name: Formatting checks
        run: ./scripts/lint.sh -l -g format
      - name: Install linters
        run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.46.2
      - name: Run linters
        run: make generate && ./scripts/lint.sh -g lint

      # The go-ethereum and celo-blockchain packages both implement secp256k1 using the exact same header, but that causes duplicate symbols.
      - name: Run golang tests
        run: cd node && go test -v -ldflags '-extldflags "-Wl,--allow-multiple-definition" ' ./...

  # Run Rust lints and tests
  rust-lint-and-tests:
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        manifest:
          - terra/Cargo.toml
          - sdk/rust/Cargo.toml
    steps:
      - name: Check out source
        uses: actions/checkout@v2

      - name: Install stable rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          default: true

      - name: Run `cargo check`
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --workspace --manifest-path ${{ matrix.manifest }}

      - name: Run `cargo test`
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --workspace --manifest-path ${{ matrix.manifest }}
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Check out source
        uses: actions/checkout@v2
      - run: chmod 755 ./scripts/check-docker-pin.sh
      - run: ./scripts/check-docker-pin.sh
