on:
  workflow_call:
    inputs:
      ref:
        required: true
        type: string
      repo:
        required: true
        type: string
env:
  FORCE_COLOR: 1
  CLICOLOR_FORCE: 1
  RUST_LOG_STYLE: always

jobs:
  arm64-prebuild:
    timeout-minutes: 20
    runs-on: ubuntu-24.04-arm

    steps:
      - uses: earthly/actions-setup@43211c7a0eae5344d6d79fb4aaf209c8f8866203 # v1.0.13

      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
          fetch-depth: 0
          ref: ${{ inputs.ref }}
          repository: ${{ inputs.repo }}


      - name: Login to GitHub Container Registry
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}

      - name: Run build
        id: build
        run: |
          earthly --ci --push -P +prebuild

  amd64-prebuild:
    timeout-minutes: 20
    runs-on: ubuntu-latest

    steps:
      - uses: earthly/actions-setup@43211c7a0eae5344d6d79fb4aaf209c8f8866203 # v1.0.13

      # Setup repo and add caching
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
          fetch-depth: 0
          ref: ${{ inputs.ref }}
          repository: ${{ inputs.repo }}


      - name: Login to GitHub Container Registry
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}


      - name: Run build
        id: build
        run: |
          earthly --ci --push -P +prebuild

  build-scripts:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    steps:
      - uses: earthly/actions-setup@43211c7a0eae5344d6d79fb4aaf209c8f8866203 # v1.0.13

      - name: Set up QEMU
        uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0

      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with: 
          persist-credentials: false
          fetch-depth: 0
          ref: ${{ inputs.ref }}
          repository: ${{ inputs.repo }}


      - name: Login to GitHub Container Registry
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}


      - name: Run build
        id: build
        run: |
          earthly --ci --push -P +build-scripts-all

  build-images:
    permissions:
      packages: write
    timeout-minutes: 60
    runs-on: ubuntu-latest
    needs:
      - arm64-prebuild
      - amd64-prebuild

    steps:
      - name: Maximize build space
        uses: ublue-os/remove-unwanted-software@e3843c85f5f9b73626845de0f5d44fb78ce22e12 # v6

      - uses: earthly/actions-setup@43211c7a0eae5344d6d79fb4aaf209c8f8866203 # v1.0.13

      - name: Set up QEMU
        uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0

      # Setup repo and add caching
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with: 
          persist-credentials: false
          fetch-depth: 0
          ref: ${{ inputs.ref }}
          repository: ${{ inputs.repo }}


      - name: Login to GitHub Container Registry
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}


      - name: Run build
        run: earthly --push --ci -P +build-images-all
