stages:
  - build

image: docker:20.10.8

variables:
  DOCKER_TLS_CERTDIR: "/certs"
  IMAGE_TAG_APP: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
  IMAGE_TAG_NGINX: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-nginx

services:
  - docker:20.10.8-dind

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

build_app:
  stage: build
  script:
    - docker pull $CI_REGISTRY_IMAGE:test || true
    - docker build --cache-from $CI_REGISTRY_IMAGE:test -t $IMAGE_TAG_APP .
    - docker push $IMAGE_TAG_APP

build_nginx:
  stage: build
  when: manual
  script:
    - docker pull $CI_REGISTRY_IMAGE:test-nginx || true
    - docker build --cache-from $CI_REGISTRY_IMAGE:test-nginx -t $IMAGE_TAG_NGINX . -f Dockerfile.nginx
    - docker push $IMAGE_TAG_NGINX
