name: Build MAGMA for Windows

on:
  push:
    branches:
      main
    paths:
      - .github/scripts/windows/*
      - .github/workflows/build-magma-windows.yml
  pull_request:
    paths:
      - .github/scripts/windows/*
      - .github/workflows/build-magma-windows.yml

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}-${{ github.event_name == 'workflow_dispatch' }}
  cancel-in-progress: true

jobs:
  build-windows-magma:
    if: github.repository_owner == 'pytorch'
    runs-on: windows-2022
    strategy:
      matrix:
        cuda_version: ["129", "128", "126"]
        config: ["Release", "Debug"]
    env:
      CUDA_VERSION: ${{ matrix.cuda_version }}
      CONFIG: ${{ matrix.config }}
      VC_YEAR: "2022"
    steps:
      - name: Checkout pytorch/pytorch
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - name: Enable MSVC dev commands to enable cl.exe  # FYI incompatible with shell: bash
        uses: ilammy/msvc-dev-cmd@dd5e2fa0a7de1e7929605d9ecc020e749d9856a3
      - name: Install CUDA Toolkit
        run: .ci/pytorch/windows/internal/cuda_install.bat
      - name: Build MAGMA and push to S3
        run: .github/scripts/windows/build_magma.bat
      - name: Save as artifact
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          path: magma_*_cuda*_*.7z
          name: artifact_${{ matrix.cuda_version }}_${{ matrix.config }}
  push-windows-magma:
    if: github.repository_owner == 'pytorch'
    runs-on: ubuntu-22.04
    permissions:
      id-token: write
    needs: build-windows-magma
    steps:
      - name: Checkout PyTorch
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - name: Download all artifacts
        uses: actions/download-artifact@95815c38cf2ff2164869cbab79da8d1f422bc89e # v4.2.1
      - name: Configure AWS credentials(PyTorch account)
        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
        uses: aws-actions/configure-aws-credentials@ececac1a45f3b08a01d2dd070d28d111c5fe6722 # v4.1.0
        with:
          role-to-assume: arn:aws:iam::308535385114:role/gha_workflow_s3_ossci_linux_windows_read_write
          aws-region: us-east-1
      - name: Set DRY_RUN
        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
        shell: bash
        run: |
          echo "DRY_RUN=disabled" >> "$GITHUB_ENV"
      - name: Upload binaries
        shell: bash
        env:
            PKG_DIR: "."
            TARGET_OS: "windows"
            PKG_INCLUDE: "magma_*_cuda*_*.7z"
        run: |
            set -ex
            bash .github/scripts/upload_aws_ossci.sh
