{% import 'common.yml.j2' as common %}
{% import 'upload.yml.j2' as upload %}

{%- block name -%}
# Template is at:    .github/templates/windows_binary_build_workflow.yml.j2
# Generation script: .github/scripts/generate_ci_workflows.py
name: !{{ build_environment }}
{%- endblock %}

{%- macro set_runner_specific_vars() -%}
      # NOTE: These environment variables are put here so that they can be applied on every job equally
      #       They are also here because setting them at a workflow level doesn't give us access to the
      #       runner.temp variable, which we need.
      - name: Populate binary env
        shell: bash
        run: |
          echo "BINARY_ENV_FILE=${RUNNER_TEMP}/env" >> "${GITHUB_ENV}"
          echo "PYTORCH_FINAL_PACKAGE_DIR=${RUNNER_TEMP}/artifacts" >> "${GITHUB_ENV}"
          echo "WIN_PACKAGE_WORK_DIR=${RUNNER_TEMP}"
{%- endmacro %}

on:
  push:
    {%- if branches == "nightly" %}
    # NOTE: Meta Employees can trigger new nightlies using: https://fburl.com/trigger_pytorch_nightly_build
    {%- endif %}
    branches:
      - !{{ branches }}
    {%- if branches == "nightly" %}
    tags:
      # NOTE: Binary build pipelines should only get triggered on release candidate builds
      # Release candidate tags look like: v1.11.0-rc1
      - v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+
    {%- endif %}
{%- for label in ciflow_config.labels | sort %}
    {%- if loop.first and branches != "nightly" %}
    tags:
    {%- endif %}
      - '!{{ label }}/*'
{%- endfor %}
  workflow_dispatch:

env:
  # Needed for conda builds
  ALPINE_IMAGE: "308535385114.dkr.ecr.us-east-1.amazonaws.com/tool/alpine"
  ANACONDA_USER: pytorch
  AWS_DEFAULT_REGION: us-east-1
  BUILD_ENVIRONMENT: !{{ build_environment }}
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  PR_NUMBER: ${{ github.event.pull_request.number }}
  SHA1: ${{ github.event.pull_request.head.sha || github.sha }}
  SKIP_ALL_TESTS: 1
!{{ common.concurrency(build_environment) }}

jobs:
{%- for config in build_configs %}
  !{{ config["build_name"] }}-build:
    if: ${{ github.repository_owner == 'pytorch' }}
    runs-on: windows.4xlarge.nonephemeral
    timeout-minutes: !{{ common.timeout_minutes }}
    !{{ upload.binary_env(config, True) }}
    steps:
      !{{ common.setup_ec2_windows() }}
      !{{ set_runner_specific_vars() }}
      !{{ common.checkout(deep_clone=False, directory="pytorch") }}
      !{{ common.checkout(deep_clone=False, directory="builder", repository=common.builder_repo, branch=common.builder_branch) }}
      - name: Populate binary env
        shell: bash
        run: |
          "${PYTORCH_ROOT}/.circleci/scripts/binary_populate_env.sh"
      - name: Build PyTorch binary
        shell: bash
        run: |
          "${PYTORCH_ROOT}/.circleci/scripts/binary_windows_build.sh"
      - uses: !{{ common.upload_artifact_action }}
        if: always()
        with:
          name: !{{ config["build_name"] }}
          retention-days: 14
          if-no-files-found: error
          path: "${{ env.PYTORCH_FINAL_PACKAGE_DIR }}"
      !{{ common.wait_and_kill_ssh_windows('pytorch') }}
  !{{ config["build_name"] }}-test:  # Testing
    if: ${{ github.repository_owner == 'pytorch' }}
    needs: !{{ config["build_name"] }}-build
{%- if config["gpu_arch_type"] == "cuda" %}
{%- if branches == "nightly" %}
    runs-on: windows.8xlarge.nvidia.gpu
{%- else %}
    runs-on: windows.8xlarge.nvidia.gpu.nonephemeral
{%- endif %}
{%- else %}
    runs-on: windows.4xlarge.nonephemeral
{%- endif %}
    timeout-minutes: !{{ common.timeout_minutes }}
    !{{ upload.binary_env(config, True) }}
    steps:
      !{{ common.setup_ec2_windows() }}
      !{{ set_runner_specific_vars() }}
      - uses: !{{ common.download_artifact_action }}
        name: Download Build Artifacts
        with:
          name: !{{ config["build_name"] }}
          path: "${{ env.PYTORCH_FINAL_PACKAGE_DIR }}"
      !{{ common.checkout(deep_clone=False, directory="pytorch") }}
      !{{ common.checkout(deep_clone=False, directory="builder", repository=common.builder_repo, branch=common.builder_branch) }}
      - name: Populate binary env
        shell: bash
        run: |
          "${PYTORCH_ROOT}/.circleci/scripts/binary_populate_env.sh"
      - name: Test PyTorch binary
        shell: bash
        run: |
          "${PYTORCH_ROOT}/.circleci/scripts/binary_windows_test.sh"
      !{{ common.wait_and_kill_ssh_windows('pytorch') }}
  {%- if branches == "nightly" %}
  !{{ upload.upload_binaries(config, True) }}
  {%- endif %}
{%- endfor %}
