variables:
  DOCKER_HOST: tcp://docker:2375
  DOCKER_TLS_CERTDIR: ""

.for_development:
  environment:
    name: development
  variables:
    DOCKER_IMAGE_TAG: latest
    LABEL_SERVICE: dki-bank-sampah
  only:
    - main

.for_release:
  environment:
    name: release

.build:
  stage: build
  image: docker:latest
  tags:
    - imp
  services:
    - name: docker:27-dind
      command: ["--tls=false", "--mtu=1400"]
      variables:
        HEALTHCHECK_TCP_PORT: "2375"
  script:
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
    - docker pull ${CI_REGISTRY_IMAGE}:latest || true

    - docker build --network host
      --cache-from ${CI_REGISTRY_IMAGE}:${DOCKER_IMAGE_TAG}
      --tag ${CI_REGISTRY_IMAGE}:${DOCKER_IMAGE_TAG}
      --label org.label-schema.schema-version="1.0"
      --label org.label-schema.vcs-url="${CI_PROJECT_URL}.git"
      --label org.label-schema.vcs-ref="${CI_COMMIT_SHA}"
      --label service="${LABEL_SERVICE}"
      . -f Dockerfile

    - docker push ${CI_REGISTRY_IMAGE}:${DOCKER_IMAGE_TAG}

.prepare_release:
  extends: .build
  stage: build
  before_script:
    - DOCKER_IMAGE_TAG=${CI_COMMIT_TAG}
    - LABEL_SERVICE=dki-bank-sampah

release:publish:
  extends:
    - .prepare_release
    - .for_release
  rules:
    - if: $BY_TRIGGER == 'true'

development:publish:
  extends:
    - .build
    - .for_development
