name: container project - common jobs

on:
  workflow_call:
    inputs:
      release:
        type: boolean
        description: "Publish this build for release"
        default: false

jobs:
  buildAndTest:
    name: Build and test the project
    if: github.repository == 'apple/container'
    timeout-minutes: 60
    runs-on: [self-hosted, macos, sequoia, ARM64]
    permissions:
      contents: read
      packages: read
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Check formatting
        run: |
          ./scripts/install-hawkeye.sh
          make fmt

          if ! git diff --quiet -- . ; then
            echo "❌ The following files require formatting or license header updates:"
            git diff --name-only -- .
            false
          fi

      - name: Check protobuf
        run: |
          make protos
          if ! git diff --quiet -- . ; then
            echo "❌ The following files require formatting or license header updates:"
            git diff --name-only -- .
            false
          fi
        env:
          DEVELOPER_DIR: "/Applications/Xcode-latest.app/Contents/Developer"

      - name: Set build configuration
        run: |
          echo "BUILD_CONFIGURATION=debug" >> $GITHUB_ENV
          if [[ "${{ inputs.release }}" == "true" ]]; then
            echo "BUILD_CONFIGURATION=release" >> $GITHUB_ENV
          fi

      - name: Make the container project and docs
        run: |
          make container dsym docs
          tar cfz _site.tgz _site
        env:
          DEVELOPER_DIR: "/Applications/Xcode-latest.app/Contents/Developer"

      - name: Create package
        run: |
          mkdir -p outputs
          mv bin/${{ env.BUILD_CONFIGURATION }}/container-installer-unsigned.pkg outputs
          mv bin/${{ env.BUILD_CONFIGURATION }}/bundle/container-dSYM.zip outputs

      - name: Test the container project
        run: |
          launchctl setenv HTTP_PROXY $HTTP_PROXY
          APP_ROOT=$(mktemp -d -p "${RUNNER_TEMP}")
          trap 'rm -rf "${APP_ROOT}"; echo Removing data directory ${APP_ROOT}' EXIT
          echo "Created data directory ${APP_ROOT}"
          make APP_ROOT="${APP_ROOT}" test install-kernel integration
        env:
          DEVELOPER_DIR: "/Applications/Xcode-latest.app/Contents/Developer"

      - name: Save documentation artifact
        uses: actions/upload-artifact@v4
        with:
          name: api-docs
          path: "./_site.tgz"
          retention-days: 14

      - name: Save package artifacts
        uses: actions/upload-artifact@v4
        with:
          name: container-package
          path: ${{ github.workspace }}/outputs

  uploadPages:
    # Separate upload step required because upload-pages-artifact needs
    # gtar which is not on the macOS runner.
    name: Upload artifact for GitHub Pages
    needs: buildAndTest
    timeout-minutes: 5
    runs-on: ubuntu-latest
    steps:
      - name: Setup Pages
        uses: actions/configure-pages@v5

      - name: Download a single artifact
        uses: actions/download-artifact@v4
        with:
          name: api-docs

      - name: Add API docs to documentation
        run: |
          tar xfz _site.tgz

      - name: Upload Artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: "./_site"
