stages:
  - test
  - docker-build
  - oc-tag
  - deploy

# Use the acc-py-devtools templates found at
# https://gitlab.cern.ch/-/ide/project/acc-co/devops/python/acc-py-devtools/blob/master/-/acc_py_devtools/templates/gitlab-ci/python.yml.
include:
 - project: acc-co/devops/python/acc-py-devtools
   file: acc_py_devtools/templates/gitlab-ci/python.yml

variables:
  project_name: caimira
  PY_VERSION: "3.9"


# ###################################################################################################
# Test code

# A full installation of CAiMIRA, tested with pytest.
test_install:
  extends: .acc_py_full_test


# A development installation of CAiMIRA tested with pytest.
test_dev:
  extends: .acc_py_dev_test


# A development installation of CAiMIRA tested with pytest.
test_dev-39:
  variables:
    PY_VERSION: "3.9"
  extends: .acc_py_dev_test


# ###################################################################################################
# Test OpenShift config

.test_openshift_config:
  stage: test
  rules:
    - if: '$OC_TOKEN && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == $BRANCH'
      allow_failure: true  # The branch must represent what is deployed.  FIXME: change to true because of a diff between ConfigMaps
    - if: '$OC_TOKEN && $CI_MERGE_REQUEST_EVENT_TYPE != "detached"'
      allow_failure: true   # Anything other than the branch may fail without blocking the pipeline.
  image: registry.cern.ch/docker.io/mambaorg/micromamba
  before_script:
    - micromamba create --yes -p $HOME/env python=3.9 ruamel.yaml wget -c conda-forge
    - export PATH=$HOME/env/bin/:$PATH
    - wget https://github.com/openshift/origin/releases/download/v3.11.0/openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit.tar.gz
    - tar xzf ./openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit.tar.gz
    - mv openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit/oc $HOME/env/bin/
  script:
    - cd ./app-config/openshift
    - oc login ${OC_SERVER} --token="${OC_TOKEN}"
    - python ./config-fetch.py ${CAIMIRA_INSTANCE} --output-directory ./${CAIMIRA_INSTANCE}/actual
    - python ./config-generate.py ${CAIMIRA_INSTANCE} --output-directory ./${CAIMIRA_INSTANCE}/expected
    - python ./config-normalise.py ./${CAIMIRA_INSTANCE}/actual ./${CAIMIRA_INSTANCE}/actual-normed
    - python ./config-normalise.py ./${CAIMIRA_INSTANCE}/expected ./${CAIMIRA_INSTANCE}/expected-normed
    - diff -u ./${CAIMIRA_INSTANCE}/actual-normed/ ./${CAIMIRA_INSTANCE}/expected-normed/
  artifacts:
    paths:
      - ./app-config/openshift/${CAIMIRA_INSTANCE}/actual
      - ./app-config/openshift/${CAIMIRA_INSTANCE}/expected


check_openshift_config_test:
  extends: .test_openshift_config
  variables:
    CAIMIRA_INSTANCE: 'caimira-test'
    BRANCH: 'live/caimira-test'
    OC_SERVER: https://api.paas.okd.cern.ch
    OC_TOKEN: "${OPENSHIFT_CAIMIRA_TEST_CONFIG_CHECKER_TOKEN}"


check_openshift_config_prod:
  extends: .test_openshift_config
  variables:
    CAIMIRA_INSTANCE: 'caimira-prod'
    BRANCH: 'master'
    OC_SERVER: https://api.paas.okd.cern.ch
    OC_TOKEN: "${OPENSHIFT_CAIMIRA_PROD_CONFIG_CHECKER_TOKEN}"

  
oci_calculator:
  extends: .image_builder
  variables:
    IMAGE_NAME: calculator
    DOCKERFILE_DIRECTORY: app-config/caimira-public-docker-image
    DOCKER_CONTEXT_DIRECTORY: ""


# ###################################################################################################
# Build docker images

.image_builder:
  # Build and push images to the openshift instance, which automatically triggers an application re-deployment.
  stage: docker-build
  rules:
    - if: '$CI_COMMIT_BRANCH == "live/caimira-test"'
      variables:
        IMAGE_TAG: caimira-test-latest
    - if: '$CI_COMMIT_BRANCH == "master"'
      variables:
        IMAGE_TAG: caimira-prod-latest
  image:
    # Based on guidance at https://gitlab.cern.ch/gitlabci-examples/build_docker_image.
    name: gitlab-registry.cern.ch/ci-tools/docker-image-builder
    entrypoint: [""]
  script:
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - echo "Building ${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:latest Docker image..."
    - /kaniko/executor --context ${CI_PROJECT_DIR}/${DOCKER_CONTEXT_DIRECTORY} --dockerfile ${CI_PROJECT_DIR}/${DOCKERFILE_DIRECTORY}/Dockerfile --destination ${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:${IMAGE_TAG}


auth-service-image_builder:
  extends:
    - .image_builder
  variables:
    IMAGE_NAME: auth-service
    DOCKERFILE_DIRECTORY: app-config/auth-service
    DOCKER_CONTEXT_DIRECTORY: app-config/auth-service


calculator-app-image_builder:
  extends:
    - .image_builder
  variables:
    IMAGE_NAME: calculator-app
    DOCKERFILE_DIRECTORY: app-config/calculator-app
    DOCKER_CONTEXT_DIRECTORY: ""


# ###################################################################################################
# Link build Docker images OpenShift <-> GitLab registry

.link_docker_images_with_gitlab_registry:
  stage: oc-tag
  image: gitlab-registry.cern.ch/paas-tools/openshift-client:latest
  rules:
    - if: '$CI_COMMIT_BRANCH == "live/caimira-test"'
      variables:
        OC_PROJECT: "caimira-test"
        OC_TOKEN: ${OPENSHIFT_CAIMIRA_TEST_DEPLOY_TOKEN}
        IMAGE_TAG: caimira-test-latest
    - if: '$CI_COMMIT_BRANCH == "master"'
      variables:
        OC_PROJECT: "caimira-prod"
        OC_TOKEN: ${OPENSHIFT_CAIMIRA_PROD_DEPLOY_TOKEN}
        IMAGE_TAG: caimira-prod-latest
  script:
     - oc tag --source=docker ${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:${IMAGE_TAG} ${IMAGE_NAME}:latest --token ${OC_TOKEN} --server=https://api.paas.okd.cern.ch -n ${OC_PROJECT}

link_auth-service_with_gitlab_registry:
  extends:
    - .link_docker_images_with_gitlab_registry
  variables:
    IMAGE_NAME: auth-service

link_calculator-app_with_gitlab_registry:
  extends:
    - .link_docker_images_with_gitlab_registry
  variables:
    IMAGE_NAME: calculator-app

link_calculator_with_gitlab_registry:
  extends:
    - .link_docker_images_with_gitlab_registry
  variables:
    IMAGE_NAME: calculator


# ###################################################################################################
# Trigger build of CAiMIRA router on OpenShift

trigger_caimira-router_build_on_openshift:
  stage: deploy
  rules:
    - if: '$CI_COMMIT_BRANCH == "live/caimira-test"'
      variables:
        OC_PROJECT: "caimira-test"
        BUILD_WEBHOOK_SECRET: ${OPENSHIFT_CAIMIRA_TEST_BUILD_WEBHOOK_SECRET}
    - if: '$CI_COMMIT_BRANCH == "master"'
      variables:
        OC_PROJECT: "caimira-prod"
        BUILD_WEBHOOK_SECRET: ${OPENSHIFT_CAIMIRA_PROD_BUILD_WEBHOOK_SECRET}
  script:
     - curl -X POST -k https://api.paas.okd.cern.ch/apis/build.openshift.io/v1/namespaces/${OC_PROJECT}/buildconfigs/caimira-router/webhooks/${BUILD_WEBHOOK_SECRET}/generic
