name: ios
on:
  push:
    branches:
      - main
  pull_request:
# Cancel in-progress CI jobs when a new commit is pushed to a PR.
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true
jobs:
  pre_check:
    name: pre_check
    runs-on: ubuntu-latest
    outputs:
      should_run: ${{ steps.check_changes.outputs.run_tests }}
    steps:
      # Checkout repo to Github Actions runner
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: check for relevant changes
        id: check_changes
        run: |
          ./ci/check_bazel.sh //examples/swift:ios_app //platform/swift //test/platform/swift || ./ci/files_changed.sh .github/workflows/ios.yaml && ./ci/run_tests.sh
          true
  swift_hello_world:
    name: swift_hello_world
    timeout-minutes: 40
    needs: "pre_check"
    if: needs.pre_check.outputs.should_run == 'true'
    runs-on: macos-14
    steps:
      # Checkout repo to Github Actions runner
      - name: Checkout
        uses: actions/checkout@v4

      - name: 'Install dependencies'
        run: ./ci/mac_ci_setup.sh
      - run: ./bazelw build --config ci //examples/swift/hello_world:ios_app
        name: 'Build app'
      # TODO(snowp): Add some kind of assertion that the app does that it's supposed to
      - run: ./bazelw run --config ci //examples/swift/hello_world:ios_app &> /tmp/envoy.log &
        name: 'Run app'
  macos_asan:
    runs-on: macos-14
    needs: "pre_check"
    if: needs.pre_check.outputs.should_run == 'true'
    steps:
      # Checkout repo to Github Actions runner
      - name: Checkout
        uses: actions/checkout@v4

      - name: 'Install dependencies'
        run: ./ci/mac_ci_setup.sh
      - name: Run tests (asan)
        run: env -u ANDROID_NDK_HOME ./bazelw test //core/... //platform/... //test/platform/swift/unit_integration/... --test_tag_filters=macos_only --test_output=errors --build_tests_only --config ci --config asan
  macos_tsan:
    runs-on: macos-14
    needs: "pre_check"
    if: needs.pre_check.outputs.should_run == 'true'
    steps:
      # Checkout repo to Github Actions runner
      - name: Checkout
        uses: actions/checkout@v4

      - name: 'Install dependencies'
        run: ./ci/mac_ci_setup.sh
      - name: Run iOS tests (tsan)
        run: env -u ANDROID_NDK_HOME ./bazelw test $(./bazelw query 'kind(ios_unit_test, //test/platform/swift/unit_integration:test_tsan)') --test_tag_filters=macos_only --test_output=errors --config ci --config ios-tsan
  verify_ios:
    runs-on: ubuntu-latest
    needs: ["macos_tsan", "macos_asan", "swift_hello_world"]
    if: always()
    steps:
      # Checkout repo to Github Actions runner
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      - run: |
          ./ci/check_result.sh ${{ needs.macos_tsan.result }} \
          && ./ci/check_result.sh ${{ needs.macos_asan.result }} \
          && ./ci/check_result.sh ${{ needs.swift_hello_world.result }}