name: Build containerization template

permissions:
  contents: read

on: 
  workflow_call:
    inputs:
      release:
        type: boolean
        description: "Create a release"
        default: false
      version: 
        type: string
        description: Version of containerization 
        default: test

jobs: 
  buildAndTest: 
    name: Build and Test repo
    if: github.repository == 'apple/containerization'
    timeout-minutes: 60
    runs-on: [self-hosted, macos, tahoe, ARM64]
    permissions:
      contents: read
      packages: write
    env:
      DEVELOPER_DIR: "/Applications/Xcode_swift_6.3.app/Contents/Developer"

    steps:
      - name: Checkout repository
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8  # v6
        with:
          fetch-depth: 0

      - name: Activate Swiftly
        run: |
          source ~/.swiftly/env.sh
          cat ~/.swiftly/env.sh

      - name: Check formatting
        run: | 
          ./scripts/install-hawkeye.sh
          make fmt
          git diff
          if ! git diff --quiet ; then echo the following files require formatting or license headers: ; git diff --name-only ; false ; fi

      - name: Check protobufs
        run: | 
          make protos
          if ! git diff --quiet ; then echo the following files require formatting or license headers: ; git diff --name-only ; false ; fi

      - name: Make containerization, examples, and docs
        run: |
          make clean containerization examples docs
          tar cfz _site.tgz _site
        env:
          BUILD_CONFIGURATION: ${{ inputs.release && 'release' || 'debug' }}

      - name: Make vminitd image
        run: |
          source ~/.swiftly/env.sh
          make -C vminitd swift linux-sdk
          make init
        env:
          BUILD_CONFIGURATION: ${{ inputs.release && 'release' || 'debug' }}

      - name: Test containerization
        run: | 
          make fetch-default-kernel
          make test integration
        env:
          REGISTRY_TOKEN: ${{ github.token }}
          REGISTRY_USERNAME: ${{ github.actor }}

      - name: Push vminitd image
        if: ${{ inputs.release }}
        env:
          REGISTRY_TOKEN: ${{ github.token }}
          REGISTRY_USERNAME: ${{ github.actor }}
          REGISTRY_HOST: ghcr.io
          VERSION: ${{ inputs.version }}
        run: |
          bin/cctl images tag vminit:latest "ghcr.io/apple/containerization/vminit:${VERSION}"
          bin/cctl images push "ghcr.io/apple/containerization/vminit:${VERSION}"

      - name: Create image tar 
        if: ${{ !inputs.release }}
        run: |
          bin/cctl images save vminit:latest -o vminit.tar

      - name: Save vminit artifact
        if: ${{ !inputs.release }}
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f  # v6
        with:
          name: vminit
          path: vminit.tar

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

  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@983d7736d9b0ae728b81ab479565c72886d7745b  # v5

      - name: Download a single artifact
        uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131  # v7
        with:
          name: api-docs

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

      - name: Upload Artifact
        uses: actions/upload-pages-artifact@7b1f4a764d45c48632c6b24a0339c27f5614fb0b  # v4
        with:
          path: "./_site"
