variables:
  WEBSITE_SRC: ${CI_PROJECT_DIR}/website
  INFRA_SRC: ${CI_PROJECT_DIR}/infra
  TEAMSPEAK_SRC: ${CI_PROJECT_DIR}/teamspeak
  DEFIANT_SRC: ${CI_PROJECT_DIR}/defiant
  CHARTMUSEUM_SRC: ${CI_PROJECT_DIR}/chartmuseum
  ABITIBI_SRC: ${CI_PROJECT_DIR}/abitibi
  ONETUN_SRC: ${CI_PROJECT_DIR}/onetun
  TWITTON_SRC: ${CI_PROJECT_DIR}/twitton
  TSACTIVITY_SRC: ${CI_PROJECT_DIR}/ts-activity

  IMAGE_PREFIX: registry.gitlab.com/momothereal/momoperes.ca
  WEBSITE_IMAGE: ${IMAGE_PREFIX}/website
  DEFIANT_IMAGE: ${IMAGE_PREFIX}/defiant
  ABITIBI_IMAGE: ${IMAGE_PREFIX}/abitibi

  KUBECONFIG_PARENT: /etc/deploy
  KUBECONFIG: ${KUBECONFIG_PARENT}/config
  DEPLOY_NAMESPACE: public

stages:
  - build
  - release
  - deploy

### WEBSITE ###

website:release:
  stage: release
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    - echo "Building to ${WEBSITE_IMAGE}:${CI_COMMIT_TAG}"
    - echo "{\"auths\":{\"${CI_REGISTRY}\":{\"username\":\"${CI_REGISTRY_USER}\",\"password\":\"${CI_REGISTRY_PASSWORD}\"}}}" > /kaniko/.docker/config.json
    - /kaniko/executor --context ${WEBSITE_SRC} --dockerfile ${WEBSITE_SRC}/docker/Dockerfile --destination ${WEBSITE_IMAGE}:${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHORT_SHA} --destination ${WEBSITE_IMAGE}:${CI_COMMIT_REF_SLUG}
  only:
    changes:
      - website/**/*

website:deploy:
  stage: deploy
  image: wavyfm/helm-kubectl-docker:v1.19.3-v3.4.2
  script:
    - mkdir -p ${KUBECONFIG_PARENT}
    - echo ${K8S_CLUSTER_CONFIG} | base64 -d > ${KUBECONFIG}

    # Deploy using helm chart
    - cd ${WEBSITE_SRC}
    - helm upgrade --install --namespace ${DEPLOY_NAMESPACE} --set image.name=${WEBSITE_IMAGE},image.tag=${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHORT_SHA} website ./chart/website
  only:
    refs:
      - master
    changes:
      - website/**/*

### ABITIBI ###

abitibi:release:
  stage: release
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    - echo "Building to ${ABITIBI_IMAGE}:${CI_COMMIT_TAG}"
    - echo "{\"auths\":{\"${CI_REGISTRY}\":{\"username\":\"${CI_REGISTRY_USER}\",\"password\":\"${CI_REGISTRY_PASSWORD}\"}}}" > /kaniko/.docker/config.json
    - /kaniko/executor --context ${ABITIBI_SRC} --dockerfile ${ABITIBI_SRC}/docker/Dockerfile --destination ${ABITIBI_IMAGE}:${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHORT_SHA} --destination ${ABITIBI_IMAGE}:${CI_COMMIT_REF_SLUG}
  only:
    changes:
      - abitibi/**/*

abitibi:deploy:
  stage: deploy
  image: wavyfm/helm-kubectl-docker:v1.19.3-v3.4.2
  script:
    - mkdir -p ${KUBECONFIG_PARENT}
    - echo ${K8S_CLUSTER_CONFIG} | base64 -d > ${KUBECONFIG}

    # Deploy using helm chart
    - cd ${ABITIBI_SRC}
    - helm upgrade --install --namespace ${DEPLOY_NAMESPACE} --set image.name=${ABITIBI_IMAGE},image.tag=${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHORT_SHA} abitibi ./chart/abitibi
  only:
    refs:
      - master
    changes:
      - abitibi/**/*

onetun-ntc-pdf-converter:deploy:
  stage: deploy
  image: wavyfm/helm-kubectl-docker:v1.19.3-v3.4.2
  script:
    - mkdir -p ${KUBECONFIG_PARENT}
    - echo ${K8S_CLUSTER_CONFIG} | base64 -d > ${KUBECONFIG}

    # Deploy using helm chart
    - cd ${ONETUN_SRC}
    - helm upgrade --install -n ${DEPLOY_NAMESPACE} -f ./ntc-pdf-converter.yml ntc-pdf-converter ./chart/onetun
  only:
    refs:
      - master
    changes:
      - onetun/**/*

twitton:deploy:
  stage: deploy
  image: wavyfm/helm-kubectl-docker:v1.19.3-v3.4.2
  script:
    - mkdir -p ${KUBECONFIG_PARENT}
    - echo ${K8S_CLUSTER_CONFIG} | base64 -d > ${KUBECONFIG}

    # Deploy using helm chart
    - cd ${TWITTON_SRC}
    - helm upgrade --install -n ${DEPLOY_NAMESPACE} -f ./values.yaml twitton ./chart/twitton
  only:
    refs:
      - master
    changes:
      - twitton/**/*

# INFRA #

infra:deploy:
  stage: deploy
  image: wavyfm/helm-kubectl-docker:v1.19.3-v3.4.2
  script:
    - mkdir -p ${KUBECONFIG_PARENT}
    - echo ${K8S_CLUSTER_CONFIG} | base64 -d > ${KUBECONFIG}

    # Deploy nginx ingress
    # Note: Temporarily disabled due to issue with nginx-ingress and helm3 upgrades.
    # - helm init --client-only && helm repo update
    # - helm upgrade nginx-ingress stable/nginx-ingress --install --namespace ${DEPLOY_NAMESPACE} --set controller.publishService.enabled=true

    # Deploy infra manifests directly
    - cd ${INFRA_SRC}/
    - kubectl apply --namespace ${DEPLOY_NAMESPACE} -R -f manifests/
  only:
    refs:
      - master
    changes:
      - infra/**/*

# TEAMSPEAK #

teamspeak:deploy:
  stage: deploy
  image: wavyfm/helm-kubectl-docker:v1.19.3-v3.4.2
  script:
    - mkdir -p ${KUBECONFIG_PARENT}
    - echo ${K8S_CLUSTER_CONFIG} | base64 -d > ${KUBECONFIG}

    # Deploy using helm chart
    - cd ${TEAMSPEAK_SRC}
    - helm upgrade --install --namespace ${DEPLOY_NAMESPACE} teamspeak ./chart/teamspeak
  only:
    refs:
      - master
    changes:
      - teamspeak/**/*

# TS-ACTIVITY #

ts-activity:deploy:
  stage: deploy
  image: wavyfm/helm-kubectl-docker:v1.19.3-v3.4.2
  script:
    - mkdir -p ${KUBECONFIG_PARENT}
    - echo ${K8S_CLUSTER_CONFIG} | base64 -d > ${KUBECONFIG}

    # Deploy using helm chart
    - cd ${TSACTIVITY_SRC}
    - helm repo add momoperes https://charts.momoperes.ca
    - helm repo update
    - helm upgrade --install --namespace ${DEPLOY_NAMESPACE} -f values.yaml ts-activity momoperes/ts-activity
  only:
    refs:
      - master
    changes:
      - ts-activity/**/*

# DEFIANT #

#defiant:build:
#  stage: build
#  image: debian
#  script:
#    - apt-get update && apt-get install -y git
#    - cd ${DEFIANT_SRC}/src
#    - git clone https://github.com/momothereal/code-jam-2.git defiant
#    - cp main_config.yaml defiant/config/main_config.yaml
#  artifacts:
#    paths:
#      - ${DEFIANT_SRC}/src
#  only:
#    changes:
#      - defiant/**/*

#defiant:release:
#  stage: release
#  image:
#    name: gcr.io/kaniko-project/executor:debug
#    entrypoint: [""]
#  script:
#    - echo "Building to ${DEFIANT_IMAGE}:${CI_COMMIT_TAG}"
#    - echo "{\"auths\":{\"${CI_REGISTRY}\":{\"username\":\"${CI_REGISTRY_USER}\",\"password\":\"${CI_REGISTRY_PASSWORD}\"}}}" > /kaniko/.docker/config.json
#    - /kaniko/executor --context ${DEFIANT_SRC}/src/defiant --dockerfile ${DEFIANT_SRC}/src/defiant/Dockerfile --destination ${DEFIANT_IMAGE}:${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHORT_SHA} --destination ${DEFIANT_IMAGE}:${CI_COMMIT_REF_SLUG}
#  only:
#    changes:
#      - defiant/**/*

#defiant:deploy:
#  stage: deploy
#  image: wavyfm/helm-kubectl-docker:v1.18.8-v3.4.2
#  script:
#    - mkdir -p ${KUBECONFIG_PARENT}
#    - echo ${K8S_CLUSTER_CONFIG} | base64 -d > ${KUBECONFIG}
#
#    # Deploy using helm chart
#    - cd ${DEFIANT_SRC}
#    - helm init --client-only && helm repo update
#    - helm upgrade --install --force --namespace ${DEPLOY_NAMESPACE} --set image.name=${DEFIANT_IMAGE},image.tag=${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHORT_SHA} defiant ./chart/defiant
#  only:
#    refs:
#      - master
#    changes:
#      - defiant/**/*

# CHART MUSEUM #

chartmuseum:deploy:
  stage: deploy
  image: wavyfm/helm-kubectl-docker:v1.19.3-v3.4.2
  script:
    - mkdir -p ${KUBECONFIG_PARENT}
    - echo ${K8S_CLUSTER_CONFIG} | base64 -d > ${KUBECONFIG}

    # Deploy using helm chart
    - cd ${CHARTMUSEUM_SRC}
    - helm repo add chartmuseum https://chartmuseum.github.io/charts && helm repo update
    - helm upgrade --install --namespace ${DEPLOY_NAMESPACE} -f values.yaml chartmuseum chartmuseum/chartmuseum
  only:
    refs:
      - master
    changes:
      - chartmuseum/**/*
