image: docker:latest

variables:
  DOCKER_TLS_CERTDIR: "/certs"
  BUILD_VERSION: p1

services:
  - docker:dind

before_script:
  - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  - SRC_VERSION=`grep FROM Dockerfile | cut -d ':' -f 2`
  - VERSION="${SRC_VERSION}-${BUILD_VERSION}"

build:
  stage: build
  script:
    - docker pull $CI_REGISTRY_IMAGE:latest || true
    - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$VERSION --tag $CI_REGISTRY_IMAGE:latest .
    - docker push $CI_REGISTRY_IMAGE:$VERSION
    - docker push $CI_REGISTRY_IMAGE:latest
