---

variables:
  GROUP_NAME: "balsh"
  PROJECT_NAME: "github-repos"
  REGISTRY: "registry.gitlab.com"
  IMAGE_FULL_NAME: "${REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}"


# Base scripts
# ============

.docker:
  # We use a custom dind image that is based on raw `docker`,
  # it has all the dependencies required.
  # By using it we reduce the build time significantly.
  # You can fallback to use raw `docker` image, see:
  # https://github.com/wemake-services/wemake-dind/
  image: wemakeservices/wemake-dind:latest
  interruptible: true
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
  before_script: &docker-before-script
    # Making sure we are in the right directory, does nothing by default:
    - pwd && echo "$CI_PROJECT_DIR" && cd "$CI_PROJECT_DIR"
    # Creating `.env` configuration file:
    - dump-env -t config/.env.template -p 'SECRET_' > config/.env
    # Login into Docker registry:
    - echo "$CI_JOB_TOKEN" | docker login "$REGISTRY"
      -u gitlab-ci-token --password-stdin
    # Debug information:
    - docker info && docker-compose --version && git --version


# Test scripts
# ============

test:
  stage: test
  extends: .docker
  before_script:
    - *docker-before-script
    # Pulling cache:
    - docker pull "${IMAGE_FULL_NAME}:dev" || true
    - docker tag "${IMAGE_FULL_NAME}:dev" "${PROJECT_NAME}:dev" || true
  script:
    # Checking config:
    - docker-compose -f docker-compose.yml
      -f docker/docker-compose.prod.yml config --quiet

    # The logic itself:
    - docker-compose build
    - docker-compose run --user=root --rm web sh ./docker/ci.sh
    - disl "${PROJECT_NAME}:dev" 950MiB

    # Pushing back the result for future runs:
    - docker tag "${PROJECT_NAME}:dev" "${IMAGE_FULL_NAME}:dev"
    - docker push "${IMAGE_FULL_NAME}:dev"
  only:
    - merge_requests


# Release scripts
# ===============

# Releasing image, when in `master` branch,
# can be replaced with `kira-release` bot:
# https://github.com/wemake-services/kira-release
release-image:
  extends: .docker
  stage: deploy
  allow_failure: false
  before_script:
    # Build local image to be released to gitlab registry,
    # modify it to suite your needs as you wish.
    # We only care about the name of the image:
    - *docker-before-script

    # Now we need the latest images for cache and improved build times:
    - docker pull "${IMAGE_FULL_NAME}:latest" || true
    - docker pull "${IMAGE_FULL_NAME}:dev" || true
    # Create correct tags:
    - docker tag "${IMAGE_FULL_NAME}:latest" "${PROJECT_NAME}:latest" || true
    - docker tag "${IMAGE_FULL_NAME}:dev" "${PROJECT_NAME}:dev" || true

    # Building the image itself:
    - docker-compose -f docker-compose.yml
      -f docker/docker-compose.prod.yml build
  script:
    - docker push "${IMAGE_FULL_NAME}:latest"
  only:
    - master
  environment:
    name: production  # used to track time with 'cycle analytics'
