# @generated DO NOT EDIT MANUALLY
# Template is at:    .github/templates/macos_ci_workflow.yml.j2
# Generation script: .github/scripts/generate_ci_workflows.py
name: macos-11-py3-x86-64

on:
  push:
    branches:
      - master
      - release/*
    tags:
      - 'ciflow/all/*'
      - 'ciflow/macos/*'
      - 'ciflow/trunk/*'
  workflow_dispatch:

# For setup-miniconda, see https://github.com/conda-incubator/setup-miniconda/issues/179
defaults:
  run:
    shell: bash -e -l {0}
env:
  BUILD_ENVIRONMENT: macos-11-py3-x86-64
  COMPACT_JOB_NAME: macos-11-py3-x86-64
  IN_CI: 1
  IS_GHA: 1
  PYTORCH_RETRY_TEST_CASES: 1

  # Set xcode xcode version to 12.4
  DEVELOPER_DIR: /Applications/Xcode_12.4.app/Contents/Developer

jobs:

  build:
    runs-on: macos-11
    env:
      JOB_BASE_NAME: macos-11-py3-x86-64
      # For sccache access (only on non-forked PRs)
      AWS_ACCESS_KEY_ID: ${{ secrets.MACOS_SCCACHE_S3_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.MACOS_SCCACHE_S3_SECRET_ACCESS_KEY }}
      PR_LABELS: ${{ toJson(github.event.pull_request.labels.*.name) }}
    steps:
      - name: print labels
        run: echo "${PR_LABELS}"
      - name: Checkout PyTorch
        uses: zhouzhuojie/checkout@05b13c9a0d21f08f6d5e64a1d5042246d13619d9
        with:
          # deep clone, to allow use of git merge-base
          fetch-depth: 0
          submodules: recursive
      - name: Clean PyTorch checkout
        run: |
          # Remove any artifacts from the previous checkouts
          git clean -fxd
      - name: Setup miniconda
        uses: conda-incubator/setup-miniconda@v2
        with:
          auto-update-conda: true
          python-version: 3.8
          activate-environment: build
      - name: Install macOS homebrew dependencies
        run: |
          # Install dependencies
          brew install libomp
      - name: Install sccache (only for non-forked PRs, and pushes to trunk)
        if: ${{ github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository }}
        run: |
          sudo curl --retry 3 https://s3.amazonaws.com/ossci-macos/sccache_v2.15 --output /usr/local/bin/sccache
          sudo chmod +x /usr/local/bin/sccache
          echo "SCCACHE_BUCKET=ossci-compiler-cache-circleci-v2" >> "${GITHUB_ENV}"
      - name: Build
        run: |
          echo "CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname "$(which conda)")/../"}" >> "${GITHUB_ENV}"
          .jenkins/pytorch/macos-build.sh
      - name: Archive artifacts into zip
        run: |
          zip -1 -r artifacts.zip dist/
      - uses: actions/upload-artifact@v2
        name: Store PyTorch Build Artifacts on GHA
        with:
          name: ${{ env.BUILD_ENVIRONMENT }}
          retention-days: 14
          if-no-files-found: error
          path:
            artifacts.zip


  generate-test-matrix:
    needs: build
    runs-on: ubuntu-18.04
    timeout-minutes: 240
    env:
      TEST_RUNNER_TYPE: macos-11
      ENABLE_DISTRIBUTED_TEST: ''
      NUM_TEST_SHARDS: 2
      PR_BODY: ${{ github.event.pull_request.body }}
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
      render-matrix: ${{ steps.set-matrix.outputs.render-matrix }}
      ignore-disabled-issues: ${{ steps.set-matrix.outputs.ignore-disabled-issues }}
    container:
      image: python:3.9
    steps:
      - name: Install dependencies
        run: pip install typing-extensions==3.10
      - name: Clone pytorch/pytorch
        uses: zhouzhuojie/checkout@05b13c9a0d21f08f6d5e64a1d5042246d13619d9
      - name: Generating test matrix
        id: set-matrix
        run: .github/scripts/generate_pytorch_test_matrix.py

  test:
    needs: [build, generate-test-matrix]
    strategy:
      matrix: ${{ fromJson(needs.generate-test-matrix.outputs.matrix) }}
      fail-fast: false
    runs-on: ${{ matrix.runner }}
    timeout-minutes: 240
    env:
      JOB_BASE_NAME: macos-11-py3-x86-64-test
      TEST_CONFIG: ${{ matrix.config }}
      SHARD_NUMBER: ${{ matrix.shard }}
      NUM_TEST_SHARDS: ${{ matrix.num_shards }}
      PYTORCH_IGNORE_DISABLED_ISSUES: ${{ needs.generate-test-matrix.outputs.ignore-disabled-issues }}
    steps:
      - name: Checkout PyTorch
        uses: zhouzhuojie/checkout@05b13c9a0d21f08f6d5e64a1d5042246d13619d9
        with:
          # deep clone, to allow use of git merge-base
          fetch-depth: 0
          submodules: false
      - name: Clean PyTorch checkout
        run: |
          # Remove any artifacts from the previous checkouts
          git clean -fxd
      - uses: actions/download-artifact@v2
        name: Download PyTorch Build Artifacts from GHA
        with:
          name: ${{ env.BUILD_ENVIRONMENT }}
          path: .
      - name: Unzip artifacts
        run: |
          unzip -o artifacts.zip
      - name: Setup miniconda
        uses: conda-incubator/setup-miniconda@v2
        with:
          auto-update-conda: true
          python-version: 3.8
          activate-environment: build
      - name: Install macOS homebrew dependencies
        run: |
          # Install dependencies
          brew install libomp
      - name: Parse ref
        id: parse-ref
        run: .github/scripts/parse_ref.py
      - name: Test
        run: |
          python3 -mpip install dist/*.whl
          .jenkins/pytorch/macos-test.sh
      - name: Install render_test_results dependencies
        if: always()
        shell: bash
        run: |
          python3 -m pip install junitparser==2.1.1 rich==10.9.0
      - name: "[[ Click me for rendered test results (useful for finding failing tests) ]]"
        if: always()
        shell: bash
        # Encoding is weird on windows, just try to default to utf-8 if possible
        env:
          PYTHONIOENCODING: "utf-8"
        run: |
          python3 tools/render_junit.py test/
      - name: Zip JSONs for upload
        if: always()
        env:
          FILE_SUFFIX: '${{ github.job }}-${{ matrix.config }}-${{ matrix.shard }}-${{ matrix.num_shards }}-${{ matrix.runner }}'
        run: |
          # Remove any previous test jsons if they exist
          rm -f test-jsons-*.zip
          zip -r "test-jsons-${FILE_SUFFIX}.zip" test -i '*.json'
      - uses: actions/upload-artifact@v2
        name: Store Test Downloaded JSONs on Github
        if: always()
        with:
          name: test-jsons
          retention-days: 14
          if-no-files-found: warn
          path:
            test-jsons-*.zip
      - name: Zip test reports for upload
        if: always()
        env:
          FILE_SUFFIX: '${{ github.job }}-${{ matrix.config }}-${{ matrix.shard }}-${{ matrix.num_shards }}-${{ matrix.runner }}'
        run: |
          # Remove any previous test reports if they exist
          rm -f test-reports-*.zip
          zip -r "test-reports-${FILE_SUFFIX}.zip" test -i '*.xml'
      - uses: actions/upload-artifact@v2
        name: Store Test Reports on Github
        if: always()
        with:
          name: test-reports
          retention-days: 14
          if-no-files-found: error
          path:
            test-reports-*.zip
      - name: Display and upload test statistics (Click Me)
        if: always()
        # temporary hack: set CIRCLE_* vars, until we update
        # tools/stats/print_test_stats.py to natively support GitHub Actions
        env:
          AWS_DEFAULT_REGION: us-east-1
          BRANCH: ${{ steps.parse-ref.outputs.branch }}
          JOB_BASE_NAME: macos-11-py3-x86-64-test
          PR_NUMBER: ${{ github.event.pull_request.number }}
          SHA1: ${{ github.event.pull_request.head.sha || github.sha }}
          TAG: ${{ steps.parse-ref.outputs.tag }}
          WORKFLOW_ID: '${{ github.run_id }}'
        shell: bash
        run: |
          python3 -m pip install -r requirements.txt
          python3 -m pip install boto3==1.19.12
          python3 -m tools.stats.print_test_stats --upload-to-s3 --compare-with-s3 test


concurrency:
  group: macos-11-py3-x86-64-${{ github.event.pull_request.number || github.sha }}-${{ github.event_name == 'workflow_dispatch' }}
  cancel-in-progress: true
