image: python:3.14

stages:
  - build
  - deploy
  - promote

build-images:
  stage: build
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    - executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE/$OS_ARCH:$CI_COMMIT_TAG-alpine
    - executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE/$OS_ARCH:$CI_COMMIT_TAG-slim-bullseye --build-arg PYTHON_FLAVOR=slim-bullseye
  rules:
    - if: $CI_COMMIT_TAG
  tags:
    - $RUNNER_TAG
  parallel:
    matrix:
      # See tags in https://docs.gitlab.com/ee/ci/runners/hosted_runners/linux.html
      - RUNNER_TAG: saas-linux-medium-amd64
        OS_ARCH: linux/amd64
      - RUNNER_TAG: saas-linux-medium-arm64
        OS_ARCH: linux/arm64

deploy-images:
  stage: deploy
  image: 
    name: mplatform/manifest-tool:alpine-v2.0.4@sha256:38b399ff66f9df247af59facceb7b60e2cd01c2d649aae318da7587efb4bbf87
    entrypoint: [""]
  script:
    - manifest-tool --username $CI_REGISTRY_USER --password $CI_REGISTRY_PASSWORD push from-args
      --platforms linux/amd64,linux/arm64
      --template $CI_REGISTRY_IMAGE/OS/ARCH:$CI_COMMIT_TAG-alpine
      --target $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG-alpine
    - manifest-tool --username $CI_REGISTRY_USER --password $CI_REGISTRY_PASSWORD push from-args
      --platforms linux/amd64,linux/arm64
      --template $CI_REGISTRY_IMAGE/OS/ARCH:$CI_COMMIT_TAG-slim-bullseye
      --target $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG-slim-bullseye
  rules:
    - if: $CI_COMMIT_TAG

tag-latest:
  stage: promote
  image:
    name: gcr.io/go-containerregistry/crane:debug
    entrypoint: [""]
  script:
    - crane auth login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - crane tag $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG-alpine ${CI_COMMIT_TAG}       # /python-gitlab:v1.2.3
    - crane tag $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG-alpine latest                 # /python-gitlab:latest
    - crane tag $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG-alpine alpine                 # /python-gitlab:alpine
    - crane tag $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG-slim-bullseye slim-bullseye   # /python-gitlab:slim-bullseye
  rules:
    - if: $CI_COMMIT_TAG
