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') && (github.repository_owner == 'k3s-io' && 'cncf-ubuntu-16-64-arm' || 'ubuntu-24.04-arm') || (github.repository_owner == 'k3s-io' && inputs.arch == 'amd64' && 'cncf-ubuntu-16-64-x86' || '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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
    
    - name: Set up QEMU
      if: inputs.arch == 'arm'
      uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4
      with:
        cache-image: false

    - name: Set up Docker
      uses: docker/setup-docker-action@1a6edb0ba9ac496f6850236981f15d8f9a82254d # 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@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0

    - 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@bcafcacb16a39f128d818304e6c9c0c18556b85f # 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@bcafcacb16a39f128d818304e6c9c0c18556b85f # 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

    # We use this version of the k3s binary to symlink to kubectl (and any other smylinks we need)
    - name: "Save bin k3s binary"
      if: inputs.os == 'linux'
      run: install -m 0755 ./bin/k3s ./dist/artifacts/bin-k3s

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