---
include:
  - remote: https://jobs.just-ci.dev/v6.14.2/container/image.yml

variables:
  DOCKER_EXTRA_ARGS: ""

.docker:
  stage: build
  extends: [".image:rules", ".image:name"]
  image:
    name: docker.io/docker:latest
    entrypoint: [""]
  variables:
    DOCKER_HOST: tcp://docker:2375/
  services:
    - name: docker.io/docker:dind
      alias: docker
  script:
    - !reference [".image:name", script]
    - docker login ${CI_REGISTRY} -u ${CI_REGISTRY_USER} -p
      ${CI_REGISTRY_PASSWORD}
    - docker buildx create --use
    - |
      if [ "${IMAGE_ARCHITECTURES}" != "" ]; then
        DOCKER_EXTRA_ARGS="${DOCKER_EXTRA_ARGS} --platform ${IMAGE_ARCHITECTURES}"
      fi
    - |
      if [ "${DOCKER_EXTRA_ARGS}" != "" ]; then
        echo "[*] Applying the extra arguments '${DOCKER_EXTRA_ARGS}'."
      fi
    - |
      docker buildx build --push \
      --tag ${IMAGE_NAME}:${IMAGE_TAG} \
      --tag ${IMAGE_NAME}:${IMAGE_DEV_TAG} \
      -f ${IMAGE_CONTAINERFILE} ${DOCKER_EXTRA_ARGS} .
