name: Build macos artifacts
description: Build macos artifacts
inputs:
  arch:
    description: Architecture to build
    required: true
  rust-toolchain:
    description: Rust toolchain to use
    required: true
  cargo-profile:
    description: Cargo profile to build
    required: true
  features:
    description: Cargo features to build
    required: true
  version:
    description: Version of the artifact
    required: true
  disable-run-tests:
    description: Disable running integration tests
    required: true
  artifacts-dir:
    description: Directory to store artifacts
    required: true
runs:
  using: composite
  steps:
    - name: Cache cargo assets
      id: cache
      uses: actions/cache@v3
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ inputs.arch }}-build-cargo-${{ hashFiles('**/Cargo.lock') }}

    - name: Install protoc
      shell: bash
      run: |
        brew install protobuf

    - name: Install rust toolchain
      uses: dtolnay/rust-toolchain@master
      with:
        toolchain: ${{ inputs.rust-toolchain }}
        targets: ${{ inputs.arch }}

    - name: Start etcd # For integration tests.
      if: ${{ inputs.disable-run-tests == 'false' }}
      shell: bash
      run: |
        brew install etcd && \
        brew services start etcd

    - name: Install latest nextest release # For integration tests.
      if: ${{ inputs.disable-run-tests == 'false' }}
      uses: taiki-e/install-action@nextest

    - name: Run integration tests
      if: ${{ inputs.disable-run-tests == 'false' }}
      shell: bash
      run: |
        make test sqlness-test

    - name: Upload sqlness logs
      if: ${{ failure() }} # Only upload logs when the integration tests failed.
      uses: actions/upload-artifact@v3
      with:
        name: sqlness-logs
        path: /tmp/greptime-*.log
        retention-days: 3

    - name: Build greptime binary
      shell: bash
      run: |
        make build \
        CARGO_PROFILE=${{ inputs.cargo-profile }} \
        FEATURES=${{ inputs.features }} \
        TARGET=${{ inputs.arch }}

    - name: Upload artifacts
      uses: ./.github/actions/upload-artifacts
      with:
        artifacts-dir: ${{ inputs.artifacts-dir }}
        target-file: target/${{ inputs.arch }}/${{ inputs.cargo-profile }}/greptime
        version: ${{ inputs.version }}
