name: chipyard-ci-gpu

on:
  push:
    branches:
      - graphics
      - graphics-ci

defaults:
  run:
    shell: bash -leo pipefail {0}

env:
  tools-cache-version: v17
  CI_DIR: ${{ secrets.BUILDDIR }}
  JVM_OPTS: -Xmx3200m # Customize the JVM maximum heap limit
  conda-env-name-no-time: cy-${{ github.run_id }}
  workflow-timestamp: ${{ github.event.pull_request.updated_at }}
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  # SSH_AUTH_SOCK: /tmp/ssh_agent_github.sock

jobs:
  cancel-prior-workflows:
    name: cancel-prior-workflows
    runs-on: self-hosted
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.11.0
        with:
          access_token: ${{ github.token }}

  # Set up a set of boolean conditions to control which branches of the CI
  # workflow will execute This is based off the conditional job execution
  # example here: https://github.com/dorny/paths-filter#examples
  change-filters:
    name: filter-jobs-on-changes
    runs-on: self-hosted
    # Queried by downstream jobs to determine if they should run.
    outputs:
      needs-rtl: ${{ steps.filter.outputs.all_count != steps.filter.outputs.skip-rtl_count }}

    steps:
      - uses: actions/checkout@v3
      - name: Git workaround
        uses: ./.github/actions/git-workaround
      - uses: dorny/paths-filter@v2
        id: filter
        with:
          filters: |
            all:
              - '**'

            # If any of the files changed match, do a doc build
            docs: &docs-filter
              - 'docs/**'
              - '.readthedocs.yml'

            # If all files match to this filter, skip the main ci pipeline
            skip-rtl:
              - *docs-filter
              - '**/*.md'
              - '**/.gitignore'
              - '.github/ISSUE_TEMPLATE/**'

  create-conda-env:
    name: create-conda-env
    needs: [change-filters, cancel-prior-workflows]
    if: needs.change-filters.outputs.needs-rtl == 'true'
    runs-on: self-hosted
    steps:
      - name: Delete old checkout
        run: |
            ls -alh .
            rm -rf ${{ github.workspace }}/* || true
            rm -rf ${{ github.workspace }}/.* || true
            ls -alh .
      - name: Run ssh-agent
        run: |
            ssh-add -l >/dev/null
      - name: Checkout
        uses: actions/checkout@v3
      - name: Git workaround
        uses: ./.github/actions/git-workaround
      - name: Cleanup conda
        uses: ./.github/actions/cleanup-conda
      - name: Create conda env
        uses: ./.github/actions/create-conda-env

  # setup-repo:
  #   name: setup-repo
  #   needs: create-conda-env
  #   if: needs.change-filters.outputs.needs-rtl == 'true'
  #   runs-on: self-hosted
  #   steps:
  #     - name: Delete old checkout
  #       run: |
  #           ls -alh .
  #           rm -rf ${{ github.workspace }}/* || true
  #           rm -rf ${{ github.workspace }}/.* || true
  #           ls -alh .
  #     - uses: actions/checkout@v3
  #     - name: Setup repo copy
  #       run: |
  #         git clone $GITHUB_WORKSPACE ${{ env.REMOTE_WORK_DIR }}

  # Sentinel job to simplify how we specify which that basic setup is complete
  #
  # When adding new prep jobs, please add them to `needs` below
  setup-complete:
    name: setup-complete
    needs: [create-conda-env]
    runs-on: self-hosted
    steps:
      - name: Set up complete
        run: echo Set up is complete!

  ##########################################################################

  # prepare-chipyard-cores:
  #   name: prepare-chipyard-cores
  #   needs: setup-complete
  #   runs-on: self-hosted
  #   steps:
  #     - name: Delete old checkout
  #       run: |
  #           ls -alh .
  #           rm -rf ${{ github.workspace }}/* || true
  #           rm -rf ${{ github.workspace }}/.* || true
  #           ls -alh .
  #     - name: Checkout
  #       uses: actions/checkout@v3
  #     - name: Git workaround
  #       uses: ./.github/actions/git-workaround
  #     - name: Create conda env
  #       uses: ./.github/actions/create-conda-env
  #     - name: Build RTL on self-hosted
  #       uses: ./.github/actions/prepare-rtl
  #       with:
  #         group-key: "group-cores"

  prepare-chipyard-gpu:
    name: prepare-chipyard-gpu
    needs: setup-complete
    runs-on: self-hosted
    steps:
      - name: Delete old checkout
        run: |
            ls -alh .
            rm -rf ${{ github.workspace }}/* || true
            rm -rf ${{ github.workspace }}/.* || true
            ls -alh .
      - name: Checkout
        uses: actions/checkout@v3
      - name: Git workaround
        uses: ./.github/actions/git-workaround
      - name: Create conda env
        uses: ./.github/actions/create-conda-env
      - name: Prepare GPU binaries
        uses: ./.github/actions/prepare-gpu
      - name: Build RTL on self-hosted
        uses: ./.github/actions/prepare-rtl
        with:
          group-key: "group-gpu"

  ##########################################################################

  chipyard-radiance-run-tests:
    name: chipyard-radiance-run-tests
    needs: prepare-chipyard-gpu
    runs-on: self-hosted
    steps:
      - name: Delete old checkout
        run: |
            ls -alh .
            rm -rf ${{ github.workspace }}/* || true
            rm -rf ${{ github.workspace }}/.* || true
            ls -alh .
      - name: Checkout
        uses: actions/checkout@v3
      - name: Git workaround
        uses: ./.github/actions/git-workaround
      - name: Create conda env
        uses: ./.github/actions/create-conda-env
      - name: Run tests
        uses: ./.github/actions/run-tests
        with:
          group-key: "group-gpu"
          project-key: "chipyard-radiance"

  chipyard-radiance-fuzzer-run-tests:
    name: chipyard-radiance-fuzzer-run-tests
    needs: prepare-chipyard-gpu
    runs-on: self-hosted
    steps:
      - name: Delete old checkout
        run: |
            ls -alh .
            rm -rf ${{ github.workspace }}/* || true
            rm -rf ${{ github.workspace }}/.* || true
            ls -alh .
      - name: Checkout
        uses: actions/checkout@v3
      - name: Git workaround
        uses: ./.github/actions/git-workaround
      - name: Create conda env
        uses: ./.github/actions/create-conda-env
      - name: Run tests
        uses: ./.github/actions/run-tests
        with:
          group-key: "group-gpu"
          project-key: "chipyard-radiance-fuzzer"

  chipyard-coalescer-run-tests:
    name: chipyard-coalescer-run-tests
    needs: prepare-chipyard-gpu
    runs-on: self-hosted
    steps:
      - name: Delete old checkout
        run: |
            ls -alh .
            rm -rf ${{ github.workspace }}/* || true
            rm -rf ${{ github.workspace }}/.* || true
            ls -alh .
      - name: Checkout
        uses: actions/checkout@v3
      - name: Git workaround
        uses: ./.github/actions/git-workaround
      - name: Create conda env
        uses: ./.github/actions/create-conda-env
      - name: Run tests
        uses: ./.github/actions/run-tests
        with:
          group-key: "group-gpu"
          project-key: "chipyard-coalescer"

  coalescer-run-tests:
    name: coalescer-run-tests
    needs: prepare-chipyard-gpu
    runs-on: self-hosted
    steps:
      - name: Delete old checkout
        run: |
            ls -alh .
            rm -rf ${{ github.workspace }}/* || true
            rm -rf ${{ github.workspace }}/.* || true
            ls -alh .
      - name: Checkout
        uses: actions/checkout@v3
      - name: Git workaround
        uses: ./.github/actions/git-workaround
      - name: Create conda env
        uses: ./.github/actions/create-conda-env
      - name: Run tests
        uses: ./.github/actions/run-tests
        with:
          group-key: "group-gpu"
          project-key: "coalescer"

  # Sentinel job to simplify how we specify which checks need to pass in branch
  # protection and in Mergify
  #
  # When adding new top level jobs, please add them to `needs` below
  all_tests_passed:
    name: "all tests passed"
    needs: [
            chipyard-radiance-run-tests,
            chipyard-radiance-fuzzer-run-tests,
            chipyard-coalescer-run-tests,
            coalescer-run-tests,
            ]
    runs-on: self-hosted
    steps:
      - run: echo Success!
