on: 
  workflow_call:
    inputs:
      arch:
        type: string
        description: 'Architecture to build (amd64, arm64, or arm)'
        default: 'amd64'
      os:
        type: string
        description: 'Target OS (linux or windows)'
        default: 'linux'
      upload-image:
        type: boolean
        description: 'Build and upload k3s image (only works on arm64 or amd64)'
        required: false
        default: false
      upload-build:
        type: boolean
        description: 'Upload contents of build/out, used to build the k3s image externally'
        required: false
        default: false

permissions:
  contents: read

jobs:
  build:
    name: Build # DO NOT CHANGE THIS NAME, we rely on it for INSTALL_K3S_PR functionality
    runs-on: ${{ contains(inputs.arch, 'arm') && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }}
    timeout-minutes: 20
    env:
      BIN_EXT: ${{ inputs.os == 'windows' && '.exe' || '' }}
      ARCH_EXT: ${{ inputs.os == 'windows' && '-windows' || format('-{0}', inputs.arch) }}
      GOOS: ${{ inputs.os }}
    steps:
    - name: Checkout K3s
      uses: actions/checkout@v6
    
    - name: Set up QEMU
      if: inputs.arch == 'arm'
      uses: docker/setup-qemu-action@v4
      with:
        cache-image: false

    - name: Set up Docker
      uses: docker/setup-docker-action@v5
      with:
        version: type=image,tag=28
        daemon-config: '{"features":{"containerd-snapshotter":true}}'
        set-host: true

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3

    - name: Determine Git Version Info
      id: git_vars
      run: |
        source ./scripts/git_version.sh
        {
          echo "git_tag=${GIT_TAG}"
          echo "tree_state=${TREE_STATE}"
          echo "commit=${COMMIT}"
          echo "dirty=${DIRTY}"
        } >> "$GITHUB_OUTPUT"

    - name: Build K3s Binary Native
      if: inputs.arch == 'arm64' || inputs.arch == 'amd64'
      env:
          DOCKER_BUILD_SUMMARY: false
      uses: docker/build-push-action@v7
      with:
        context: .
        file: ./Dockerfile.local
        target: result
        # Defined actions like this don't ingest GITHUB_ENV, so use outputs
        # and manual set the build arguments
        build-args: |
          GIT_TAG=${{ steps.git_vars.outputs.git_tag }}
          TREE_STATE=${{ steps.git_vars.outputs.tree_state }}
          COMMIT=${{ steps.git_vars.outputs.commit }}
          DIRTY=${{ steps.git_vars.outputs.dirty }}
          GOOS=${{ inputs.os }}
        push: false
        provenance: mode=min
        outputs: type=local,dest=.

    - name: Build K3s Binary Emulated
      if: inputs.arch != 'arm64' && inputs.arch != 'amd64'
      env:
        PLATFORM: ${{ inputs.arch == 'arm' && 'linux/arm/v7' || format('linux/{0}', inputs.arch) }}
        DOCKER_BUILD_SUMMARY: false
      uses: docker/build-push-action@v7
      with:
        context: .
        file: ./Dockerfile.local
        target: result
        build-args: |
          GIT_TAG=${{ steps.git_vars.outputs.git_tag }}
          TREE_STATE=${{ steps.git_vars.outputs.tree_state }}
          COMMIT=${{ steps.git_vars.outputs.commit }}
          DIRTY=${{ steps.git_vars.outputs.dirty }}
        push: false
        provenance: mode=min
        platforms: ${{ env.PLATFORM }}
        outputs: type=local,dest=.

    - name: Caculate binary checksum
      run: |
        if [ ${{ inputs.arch }} == 'amd64' ]; then
          sha256sum dist/artifacts/k3s${{ env.BIN_EXT }} | sed 's|dist/artifacts/||' > dist/artifacts/k3s${{ env.BIN_EXT }}.sha256sum
        elif [ ${{ inputs.arch }} == "arm" ]; then
          sha256sum dist/artifacts/k3s-armhf | sed 's|dist/artifacts/||' > dist/artifacts/k3s${{ env.ARCH_EXT }}.sha256sum
        else
          sha256sum dist/artifacts/k3s${{ env.ARCH_EXT }}${{ env.BIN_EXT }} | sed 's|dist/artifacts/||' > dist/artifacts/k3s${{ env.ARCH_EXT }}${{ env.BIN_EXT }}.sha256sum
        fi
    
    - name: Build K3s image
      if: inputs.upload-image == true && inputs.os == 'linux' && (inputs.arch == 'amd64' || inputs.arch == 'arm64')
      run: ./scripts/package-image
      
    - name: "Save K3s image"
      if: inputs.upload-image == true && inputs.os == 'linux'
      run: docker image save rancher/k3s -o ./dist/artifacts/k3s-image.tar

    - name: "Save K3s build"
      if: inputs.upload-build == true  && inputs.os == 'linux'
      run: |
        mv ./build/out/data-linux.tar.zst ./dist/artifacts/data-linux${{ env.ARCH_EXT }}.tar.zst

    - name: "Upload K3s Artifacts"
      uses: actions/upload-artifact@v6
      with:
        name: k3s${{ env.ARCH_EXT }}
        path: dist/artifacts/
