variables:
  CONTAINER_IMAGE: "gitlab.inf.unibz.it:4567/commul/merlin-platform/www"
  DOCKER_BUILDER_IMAGE: "docker:19.03.1"
  DOCKER_TLS_CERTDIR: "" # See https://gitlab.com/gitlab-org/gitlab-runner/issues/4501
  KUBECTL: "https://storage.googleapis.com/kubernetes-release/release/v1.8.10/bin/linux/amd64/kubectl"

stages:
  - build
  - tag
  - deploy

build:
  stage: build
  tags:
    - commul
  image: ${DOCKER_BUILDER_IMAGE}
  services:
    - docker:dind
  before_script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN https://gitlab.inf.unibz.it:4567
  script:
    - docker build --rm --no-cache --tag=${CONTAINER_IMAGE}:${CI_COMMIT_SHA} .
    - docker push ${CONTAINER_IMAGE}:${CI_COMMIT_SHA}
    - docker tag ${CONTAINER_IMAGE}:${CI_COMMIT_SHA} ${CONTAINER_IMAGE}:latest
    - docker push ${CONTAINER_IMAGE}:latest
  only:
    - branches
    
deploy_staging:
  stage: deploy
  tags:
    - commul
  image: ${DOCKER_BUILDER_IMAGE}
  services:
    - docker:dind
  before_script:
    - wget ${KUBECTL}
    - mv kubectl /usr/bin/
    - chmod +x /usr/bin/kubectl
    - kubectl config set-cluster "k8sproduction" --server="$KUBE_URL" --certificate-authority="$KUBE_CA_PEM_FILE"
    - kubectl config set-credentials "gitlab-deploy" --token=$K8S_PRODUCTION_TOKEN
    - kubectl config set-context "$CI_PROJECT_ID" --cluster="k8sproduction" --user="gitlab-deploy" --namespace="kommul-dev"
    - kubectl config use-context "$CI_PROJECT_ID"
    # - kubectl config view
    # - env
  script:
    - kubectl set image deployment/merlin-platform-deployment webserver=${CONTAINER_IMAGE}:${CI_COMMIT_SHA}
  only:
    - branches
