---
include:
  - project: 'ghsc/hazdev/pipeline-build-template'
    ref: 'master'
    file: 'templates/library.yml'

image: ${DEVOPS_REGISTRY}usgs/centos:latest

stages:
  - test
  - integration
  - deploy

variables:
  CI_REGISTRY: ${CODE_REGISTRY}
  CI_REGISTRY_IMAGE: ${CODE_REGISTRY_IMAGE}

## --------------------------------------------------
# Templates
## --------------------------------------------------

.adjust_image_names:
  before_script:
    - IMAGE_NAME=usgs/${APP_NAME}:${CI_COMMIT_REF_SLUG}
    - IMAGE_NAME=${IMAGE_NAME/:master/:latest}
    - INTERNAL_IMAGE_NAME=${CI_REGISTRY_IMAGE}/${IMAGE_NAME}
    - STACK_NAME=${APP_NAME}

.build_docker_image:
  cache: {}
  extends:
    - .adjust_image_names
    - .dind
  script:
    - docker build
      --pull
      --build-arg FROM_IMAGE=${FROM_IMAGE}
      --build-arg GIT_BRANCH_NAME=${CI_COMMIT_REF_NAME}
      --build-arg GIT_COMMIT_SHA=${CI_COMMIT_SHA}
      --tag local/${IMAGE_NAME} .

    - docker tag local/${IMAGE_NAME} ${IMAGE_NAME}
    - docker push ${IMAGE_NAME}
    - docker image rm ${IMAGE_NAME}

    - docker tag local/${IMAGE_NAME} ${INTERNAL_IMAGE_NAME}
    - docker push ${INTERNAL_IMAGE_NAME}
    - docker image rm ${INTERNAL_IMAGE_NAME}
  stage: integration
  tags:
    - build
  variables:
    FROM_IMAGE: ${CODE_REGISTRY}/devops/images/usgs/centos:latest

.check_code:
  cache: {}
  image: ${DEVOPS_REGISTRY}usgs/conda:latest
  script:
    # Add conda to path
    - source /etc/profile.d/conda.sh
    # Install Project Dependencies
    - conda config --add channels conda-forge
    - conda install python=${PYTHON_VERSION} obspy pycurl
    - pip install pipenv
    - pipenv --site-packages install --dev --pre --skip-lock
    # Run Code Checks
    - pipenv run black --check .
    - pipenv run pytest --cov-report xml:cov.xml --cov=geomagio
  artifacts:
    paths:
      - cov.xml
    reports:
      junit: cov.xml
  stage: test
  tags:
    - development
  variables:
    PYTHON_VERSION: 3.8

.deploy:
  cache: {}
  extends:
    - .adjust_image_names
  image: ${CODE_REGISTRY}/ghsc/hazdev/cloud-formation/hazdev-build-runner:latest
  script:
    - git clone ${GENERIC_SWARM_DEPLOY_REPO} generic-deploy
    - cp -v
      generic-deploy/default.config.sh
      generic-deploy/default.funcs.sh
      generic-deploy/deploy.sh
      scripts/.
    - export APP_NAME=${APP_NAME}
    - export IMAGE_NAME=${IMAGE_NAME}
    - export REGISTRY=${CI_REGISTRY_IMAGE}
    - export STACK_NAME=${APP_NAME}
    - ./scripts/deploy.sh
  stage: deploy
  tags:
    - deploy
    - swarm
  variables:
    APP_NAME: geomag-algorithms

.mage:
  variables:
    DATA_HOST: 'cwbpub.cr.usgs.gov'
    DATA_PORT: '2060'
    DATA_TYPE: 'edge'

.mage01:
  tags:
    - mage01

.mage02:
  tags:
    - mage02

## --------------------------------------------------
# Test Stage
## --------------------------------------------------

Check Python 3.6:
  extends:
    - .check_code
  variables:
    PYTHON_VERSION: '3.6'

Check Python 3.7:
  extends:
    - .check_code
  variables:
    PYTHON_VERSION: '3.7'

Check Python 3.8:
  extends:
    - .check_code
  variables:
    PYTHON_VERSION: '3.8'

## --------------------------------------------------
# Integration Stage
## --------------------------------------------------

Build Staging Docker Image:
  extends:
    - .build_docker_image
    - .staging
  variables:
    APP_NAME: geomag-algorithms

Build Production Docker Image:
  extends:
    - .build_docker_image
    - .production
  variables:
    APP_NAME: geomag-algorithms

## --------------------------------------------------
# Deploy Stage
## --------------------------------------------------

Deploy Mage01:
  extends:
    - .deploy
    - .staging
    - .mage
    - .mage01
  variables:
    APP_NAME: geomag-algorithms

Deploy Mage02:
  extends:
    - .deploy
    - .production
    - .mage
    - .mage02
  variables:
    APP_NAME: geomag-algorithms
