# This yaml is designed to be used until all components listed in
# `build-checks.yaml` are supported
on:
  workflow_dispatch:
    inputs:
      instance:
        default: "riscv-builder"
        description: "Default instance when manually triggering"
  workflow_call:
    inputs:
      instance:
        required: true
        type: string

name: Build checks preview riscv64
jobs:
  check:
    runs-on: ${{ inputs.instance }}
    strategy:
      fail-fast: false
      matrix:
        command:
          - "make vendor"
          - "make check"
          - "make test"
          - "sudo -E PATH=\"$PATH\" make test"
        component:
          - name: agent-ctl
            path: src/tools/agent-ctl
            needs:
              - rust
              - musl-tools
              - protobuf-compiler
              - clang
          - name: trace-forwarder
            path: src/tools/trace-forwarder
            needs:
              - rust
              - musl-tools
          - name: genpolicy
            path: src/tools/genpolicy
            needs:
              - rust
              - musl-tools
              - protobuf-compiler
          - name: runtime
            path: src/runtime
            needs:
              - golang
              - XDG_RUNTIME_DIR
          - name: runtime-rs
            path: src/runtime-rs
            needs:
              - rust

    steps:
      - name: Adjust a permission for repo
        run: |
          sudo chown -R "$USER":"$USER" "$GITHUB_WORKSPACE" "$HOME"
          sudo rm -rf "$GITHUB_WORKSPACE"/* || { sleep 10 && sudo rm -rf "$GITHUB_WORKSPACE"/*; }
          sudo rm -f /tmp/kata_hybrid*  # Sometime we got leftover from test_setup_hvsock_failed()

      - name: Checkout the code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install yq
        run: |
          ./ci/install_yq.sh
        env:
          INSTALL_IN_GOPATH: false
      - name: Install golang
        if: contains(matrix.component.needs, 'golang')
        run: |
          ./tests/install_go.sh -f -p
          echo "/usr/local/go/bin" >> "$GITHUB_PATH"
      - name: Setup rust
        if: contains(matrix.component.needs, 'rust')
        run: |
          ./tests/install_rust.sh
          echo "${HOME}/.cargo/bin" >> "$GITHUB_PATH"
          if [ "$(uname -m)" == "x86_64" ] || [ "$(uname -m)" == "aarch64" ]; then
            sudo apt-get update && sudo apt-get -y install musl-tools
          fi
      - name: Install devicemapper
        if: contains(matrix.component.needs, 'libdevmapper') && matrix.command == 'make check'
        run: sudo apt-get update && sudo apt-get -y install libdevmapper-dev
      - name: Install libseccomp
        if: contains(matrix.component.needs, 'libseccomp') && matrix.command != 'make vendor' && matrix.command != 'make check'
        run: |
          libseccomp_install_dir=$(mktemp -d -t libseccomp.XXXXXXXXXX)
          gperf_install_dir=$(mktemp -d -t gperf.XXXXXXXXXX)
          ./ci/install_libseccomp.sh "${libseccomp_install_dir}" "${gperf_install_dir}"
          echo "Set environment variables for the libseccomp crate to link the libseccomp library statically"
          echo "LIBSECCOMP_LINK_TYPE=static" >> "$GITHUB_ENV"
          echo "LIBSECCOMP_LIB_PATH=${libseccomp_install_dir}/lib" >> "$GITHUB_ENV"
      - name: Install protobuf-compiler
        if: contains(matrix.component.needs, 'protobuf-compiler') && matrix.command != 'make vendor'
        run: sudo apt-get update && sudo apt-get -y install protobuf-compiler
      - name: Install clang
        if: contains(matrix.component.needs, 'clang') && matrix.command == 'make check'
        run: sudo apt-get update && sudo apt-get -y install clang
      - name: Setup XDG_RUNTIME_DIR
        if: contains(matrix.component.needs, 'XDG_RUNTIME_DIR') && matrix.command != 'make check'
        run: |
          XDG_RUNTIME_DIR=$(mktemp -d "/tmp/kata-tests-$USER.XXX" | tee >(xargs chmod 0700))
          echo "XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR}" >> "$GITHUB_ENV"
      - name: Skip tests that depend on virtualization capable runners when needed
        if: inputs.instance == 'riscv-builder'
        run: |
          echo "GITHUB_RUNNER_CI_NON_VIRT=true" >> "$GITHUB_ENV"
      - name: Running `${{ matrix.command }}` for ${{ matrix.component.name }}
        run: |
          cd ${{ matrix.component.path }}
          ${{ matrix.command }}
        env:
          RUST_BACKTRACE: "1"
          SKIP_GO_VERSION_CHECK: "1"