name: KBS Rust Suites

on:
  push:
    branches:
      - "main"
    paths:
      - 'kbs/**'
      - '.github/workflows/kbs-rust.yml'
      - 'Cargo.toml'
  pull_request:
    paths:
      - 'kbs/**'
      - '.github/workflows/kbs-rust.yml'
      - 'Cargo.toml'

env:
  CARGO_TERM_COLOR: always

jobs:
  ci:
    name: Check
    strategy:
      fail-fast: false
    env:
      RUSTC_VERSION: 1.85.0
    runs-on: ubuntu-24.04

    steps:
    - name: Code checkout
      uses: actions/checkout@v4

    - name: Install Rust toolchain (${{ env.RUSTC_VERSION }})
      run: |
        rustup update --no-self-update ${{ env.RUSTC_VERSION }}
        rustup component add --toolchain ${{ env.RUSTC_VERSION }} rustfmt rustc clippy
        rustup target add x86_64-unknown-linux-gnu
        rustup default ${{ env.RUSTC_VERSION }}

    - name: Building dependencies installation
      run: |
        sudo apt-get update && sudo apt-get install -y protobuf-compiler libprotobuf-dev libtss2-dev libudev-dev

    - name: Install TPM dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libtss2-dev

    - name: Install TDX dependencies
      run: |
        sudo curl -L https://download.01.org/intel-sgx/sgx_repo/ubuntu/intel-sgx-deb.key | sudo apt-key add -
        sudo echo 'deb [arch=amd64] https://download.01.org/intel-sgx/sgx_repo/ubuntu jammy main' | sudo tee /etc/apt/sources.list.d/intel-sgx.list
        sudo apt-get update
        sudo apt-get install -y libtdx-attest-dev libsgx-dcap-quote-verify-dev

    - name: KBS Build [Default]
      working-directory: kbs
      run: make TPM_PCA_PLUGIN=true

    - name: KBS Build [gRPC CoCo AS]
      working-directory: kbs
      run: make AS_FEATURE=coco-as-grpc ALIYUN=true

    - name: Lint
      working-directory: kbs
      run: make lint

    - name: Format
      working-directory: kbs
      run: make format

    - name: Test
      working-directory: kbs
      run: make check
