stages:
- build
- publish
- deploy
- docs
- mirror

variables:
  image_name: ubleipzig/deployer

docker_build:
  stage: build
  image: docker:latest
  services:
  - docker:dind
  script: |
    docker build --pull \
      --build-arg HTTP_PROXY=${HTTP_PROXY} \
      --build-arg HTTPS_PROXY=${HTTPS_PROXY} \
      --build-arg NO_PROXY=${NO_PROXY} \
      --build-arg http_proxy=${HTTP_PROXY} \
      --build-arg https_proxy=${HTTPS_PROXY} \
      --build-arg no_proxy=${NO_PROXY} \
      -t image \
      .
    docker save --output=image.tar.gz image
  artifacts:
    name: docker-image
    paths:
    - image.tar.gz
  tags:
    - docker

docker_publish_alpha:
  stage: publish
  image: docker:latest
  services:
  - docker:dind
  script: |
    test "${DOCKER_AUTH_CONFIG}" == "" && echo "docker-config does not exists, aborting!" && false
    mkdir -p ~/.docker && echo "$DOCKER_AUTH_CONFIG" >~/.docker/config.json
    docker load --input=image.tar.gz
    docker tag image ${image_name}:alpha-${CI_COMMIT_REF_NAME}
    docker push ${image_name}:alpha-${CI_COMMIT_REF_NAME}
  dependencies:
  - docker_build
  tags:
    - docker
  only:
  - /^[0-9]+-/

docker_publish_staging:
  stage: publish
  image: docker:latest
  services:
  - docker:dind
  variables:
    CI_DEBUG_TRACE: "true"
  script: |
    test "${DOCKER_AUTH_CONFIG}" == "" && echo "docker-config does not exists, aborting!" && false
    mkdir -p ~/.docker && echo "$DOCKER_AUTH_CONFIG" >~/.docker/config.json
    docker load --input=image.tar.gz
    docker tag image ${image_name}:staging
    docker push ${image_name}:staging
  dependencies:
  - docker_build
  tags:
    - docker
  only:
  - master

docker_publish_production:
  stage: publish
  image: docker:latest
  services:
  - docker:dind
  variables:
    CI_DEBUG_TRACE: "true"
  script: |
    test "${DOCKER_AUTH_CONFIG}" == "" && echo "docker-config does not exists, aborting!" && false
    mkdir -p ~/.docker && echo "$DOCKER_AUTH_CONFIG" >~/.docker/config.json
    version=`expr ${CI_COMMIT_TAG} ':' 'release/\(.\+\)'`
    major_version=`expr ${version} ':' '\([^.]\+\)'`
    minor_version=`expr ${version} ':' '[^.]\+\.\([^.]\+\)'`
    patch_version=`expr ${version} ':' '[^.]\+\.[^.]\+\.\(.\+\)'`
    docker load --input=image.tar.gz
    for tag in "latest" "${major_version}" "${major_version}.${minor_version}" "${version}"; do
      docker tag image ${image_name}:${tag}
      docker push ${image_name}:${tag}
    done
  dependencies:
  - docker_build
  tags:
    - docker
  except:
  - branches
  only:
  - /^release\/.*/