# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

name: NVIDIA Dynamo Github Validation

on:
  push:
    branches:
    - main
    - release/*.*.*
  pull_request:

# Cancel any previous check runs for the same pull request to avoid redundant workflows.
concurrency:
  group: ${{ github.event_name == 'pull_request' && format('{0}-{1}', github.workflow, github.event.pull_request.number) || format('{0}-{1}', github.workflow, github.run_id) }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
  build-test:
    runs-on:
        group: Fastchecker
    name: Build and Test - dynamo
    env:
      CONTAINER_ID: test_${{ github.run_id }}_${{ github.run_attempt }}_${{ github.job }}_dynamo
      PYTEST_XML_FILE: pytest_test_report.xml
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Login to NGC
        if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'push'
        run: |
          echo "${{ secrets.NGC_CI_ACCESS_TOKEN }}" | docker login nvcr.io -u '$oauthtoken' --password-stdin
      - name: Define Image Tag
        id: define_image_tag
        run: |
          echo "image_tag=dynamo:latest" >> $GITHUB_OUTPUT
      - name: Build image
        env:
          GITHUB_TOKEN: ${{ secrets.CI_TOKEN }}
        run: |
          ./container/build.sh --tag ${{ steps.define_image_tag.outputs.image_tag }} --target dev --framework none
      - name: Start services with docker-compose
        working-directory: ./deploy
        run: |
          docker compose up -d nats-server etcd-server
      - name: Run Rust checks (block-manager + integration tests)
        run: |
          docker run --rm -v ${{ github.workspace }}:/workspace -w /workspace/lib/llm \
            --name ${{ env.CONTAINER_ID }}_rust_checks \
            ${{ steps.define_image_tag.outputs.image_tag }} \
            bash -ec 'rustup component add rustfmt clippy && \
                      cargo fmt -- --check && \
                      cargo clippy --features block-manager --no-deps --all-targets -- -D warnings && \
                      cargo test --locked --all-targets --features=block-manager && \
                      cargo test --locked --features integration -- --nocapture'
      - name: Cleanup services
        if: always()
        working-directory: ./deploy
        run: |
          docker compose down
      - name: Run pytest
        env:
          PYTEST_MARKS: "pre_merge or mypy"
        run: |
          docker run -v ${{ github.workspace }}:/workspace -w /workspace \
            --name ${{ env.CONTAINER_ID }}_pytest \
            ${{ steps.define_image_tag.outputs.image_tag }} \
            bash -c "pytest --basetemp=/tmp --junitxml=${{ env.PYTEST_XML_FILE }} -m \"${{ env.PYTEST_MARKS }}\" "
      - name: Copy test report from test Container
        if: always()
        run: |
          docker cp ${{ env.CONTAINER_ID }}_pytest:/workspace/${{ env.PYTEST_XML_FILE }} .
      - name: Archive test report
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: dynamo-python-test-results
          if-no-files-found: error
          path: |
            ${{ env.PYTEST_XML_FILE }}

  event_file:
    name: "Event File"
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'pull_request' }}
    steps:
      - name: Upload
        uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
        with:
          name: Event File
          path: ${{ github.event_path }}
