name: Docker Build
description: Builds linkerd's docker images

inputs:
  docker-registry:
    description: The docker registry used to tag the images
    required: false
    default: cr.l5d.io/linkerd
  docker-target:
    description: The OS-arch the docker build will be targeted to
    required: false
    default: linux-amd64
  docker-push:
    description: Whether to push the built images to the registry
    required: false
    default: ''
  docker-ghcr-username:
    description: GH username under which to push images
    required: false
  docker-ghcr-pat:
    description: GH PAT for docker-ghcr-username
    required: false
  component:
    description: Component to build
    required: true
  tag:
    description: Tag to use for the built image
    required: true

runs:
  using: composite
  steps:
    # populate ACTIONS_CACHE_URL and ACTIONS_RUNTIME_TOKEN
    - uses: crazy-max/ghaction-github-runtime@3cb05d89e1f492524af3d41a1c98c83bc3025124
    - uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392
    - uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435
    - name: Run bin/docker-build-${{ inputs.component }}
      env:
        DOCKER_REGISTRY: ${{ inputs.docker-registry }}
        DOCKER_TARGET: ${{ inputs.docker-target }}
        DOCKER_PUSH: ${{ inputs.docker-push }}
        TAG: ${{ inputs.tag }}
      shell: bash
      run: |
        if [[ -n "$DOCKER_PUSH" ]];then
          echo "${{ inputs.docker-ghcr-pat }}" | docker login ghcr.io -u "${{ inputs.docker-ghcr-username }}" --password-stdin
        fi
        bin/docker-build-${{ inputs.component }}

    - name: Output Image Digest
      shell: bash
      id: image_digest
      run: |
        image_digest=$(cat target/metadata-${{ inputs.component }}.json | jq -r '."containerimage.digest"')
        echo "${image_digest}"
        echo "DIGEST=${{ inputs.docker-registry }}/${{ inputs.component }}@${image_digest}" >> "$GITHUB_OUTPUT"

outputs:
  image:
    description: The image that was built
    value: ${{ inputs.docker-registry }}/${{ inputs.component }}:${{ inputs.tag }}
  digest:
    description: The image digest
    value: ${{ steps.image_digest.outputs.DIGEST }}
