name: concrete-compiler test linux-cpu-distributed

on:
  workflow_dispatch:
  # pull_request:
  #   paths:
  #     - .github/workflows/concrete_compiler_test_cpu_distributed.yml
  #     - compilers/concrete-compiler/**
  # push:
  #   branches:
  #     - 'main'
  #     - 'release/*'

env:
  ACTION_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
  SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }}
  SLACK_USERNAME: ${{ secrets.BOT_USERNAME }}
  SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}

concurrency:
  group: concrete_compiler_test_cpu_distributed_${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  start-instance:
    runs-on: ubuntu-latest
    steps:
      - name: Start instance
        id: start-instance
        uses: zama-ai/slab-github-runner@f26b8d611b2e695158fb0a6980834f0612f65ef8 # v1.4.0
        with:
          mode: start
          github-token: ${{ secrets.SLAB_ACTION_TOKEN }}
          slab-url: ${{ secrets.SLAB_BASE_URL }}
          job-secret: ${{ secrets.JOB_SECRET }}
          backend: aws
          profile: slurm-cluster

  build-and-run-test:
    # The distributed-ci runner is registered on the instance configured in the slurm-cluster profile.
    # It's why we need to start-instance
    needs: start-instance
    runs-on: distributed-ci
    steps:
      - name: Instance cleanup
        run: |
          sudo rm -rf /home/ubuntu/actions-runner/_work/concrete/concrete
          mkdir -p /home/ubuntu/actions-runner/_work/concrete/concrete
          docker system prune -af

      - name: Fetch repository
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0
          submodules: recursive

      - name: Set up home
        # "Install rust" step require root user to have a HOME directory which is not set.
        run: |
          echo "HOME=/shared" >> "${GITHUB_ENV}"

      - name: Setup rust toolchain for concrete-cpu
        uses: ./.github/actions/setup_rust_toolchain_for_concrete_cpu

      - name: Build end-to-end distributed test
        run: |
          cd compilers/concrete-compiler/compiler
          rm -rf /shared/build
          cargo install cxxbridge-cmd
          make BUILD_DIR=/shared/build CCACHE=ON DATAFLOW_EXECUTION_ENABLED=ON BINDINGS_PYTHON_ENABLED=OFF build-end-to-end-tests

      - name: Run end-to-end distributed test
        run: |
          cd compilers/concrete-compiler/compiler
          rm -rf /shared/KeyCache
          make BUILD_DIR=/shared/build KEY_CACHE_DIRECTORY=/shared/KeyCache run-end-to-end-distributed-tests

      - name: Slack Notification
        if: ${{ failure() && github.ref == 'refs/heads/main' }}
        continue-on-error: true
        uses: rtCamp/action-slack-notify@c33737706dea87cd7784c687dadc9adf1be59990
        env:
          SLACK_COLOR: ${{ job.status }}
          SLACK_MESSAGE: "build-and-run-test finished with status: ${{ job.status }}. (${{ env.ACTION_RUN_URL }})"

  stop-instance:
    needs: [ start-instance, build-and-run-test ]
    if: ${{ always() && needs.start-instance.result != 'skipped' }}
    runs-on: ubuntu-latest
    steps:
      - name: Stop instance
        id: stop-instance
        uses: zama-ai/slab-github-runner@f26b8d611b2e695158fb0a6980834f0612f65ef8 # v1.4.0
        with:
          mode: stop
          github-token: ${{ secrets.SLAB_ACTION_TOKEN }}
          slab-url: ${{ secrets.SLAB_BASE_URL }}
          job-secret: ${{ secrets.JOB_SECRET }}
          label: distributed-ci

      - name: Slack Notification
        if: ${{ failure() }}
        continue-on-error: true
        uses: rtCamp/action-slack-notify@c33737706dea87cd7784c687dadc9adf1be59990
        env:
          SLACK_COLOR: ${{ job.status }}
          SLACK_MESSAGE: "Stopping instance finished with status: ${{ job.status }}. (${{ env.ACTION_RUN_URL }})"
