image: docker:20

stages:
  - build
  - tag

before_script:
  - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

build:
  stage: build
  only:
    - main
  services:
    - docker:dind
  script:
    - docker build --quiet --tag=${CI_REGISTRY_IMAGE}:latest
    - docker push ${CI_REGISTRY_IMAGE}:latest

tag:
  stage: tag
  only:
    - tags
  variables:
    # Don't clone the project source-- we're just tagging a docker image:
    GIT_STRATEGY: none
  services:
    - docker:dind
  script:
    - docker pull ${CI_REGISTRY_IMAGE}:latest
    - docker tag ${CI_REGISTRY_IMAGE}:latest ${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME}
    - docker push ${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME}
