stages:
    - pull-db
    - build-packages
    - build
    - unit-test
    - test-coverage
    - push
    - deploy-coverage-page
    - generate-go-yaml
    - go-trigger
    - deploy
    - .post

variables:
    # Gitlab optimization https://docs.gitlab.com/ee/ci/large_repositories/
    GIT_DEPTH: 10
    # Workspaces default variables
    PROJECT_NAME: "workspaces"
    DEPLOY_ENV: "dev"
    DL_HOST: https://dl-nrao.aoc.nrao.edu
    ENV_HOST: ws-dev.nrao.edu
    # Postgres Service Variables
    POSTGRES_DB: archive
    POSTGRES_USER: "archive"
    POSTGRES_PASSWORD: "docker"

image: docker:19.03.12

workflow:
  rules:
    - if: $CI_MERGE_REQUEST_TITLE =~ /^WIP:|^Draft:/
      when: never
    - if: $CI_MERGE_REQUEST_IID
    - if: $CI_COMMIT_TAG
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH =~ /(^[0-9]\.[0-9]|^[0-9]\.[0-9]\.[0-9])-DEVELOPMENT/'

# Import Templates
include:
    - '/ci/build.template.yml'
    - '/ci/push.template.yml'
    - '/ci/cleanup.template.yml'
    - '/ci/unit-test.template.yml'
    - '/ci/package-build.template.yml'


# Unit testing steps require a specific database image to be available; this step downloads it
pull db image:
    interruptible: true
    stage: pull-db
    script:
        - echo "$HARBOR_PASSWORD" | docker login --username "$HARBOR_USER" --password-stdin "$REGISTRY_URL"
        - docker pull ${REGISTRY_URL}/workspaces/db:workspaces

###############################################
# Build Python Packages
###############################################
build carta envoy:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/carta_envoy"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/carta_envoy/**/*"

build casa envoy:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/casa_envoy"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/casa_envoy/**/*"

build core sampler:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/utilities/core_sampler"
  extends: .build-package
  only:
    changes:
      - "apps/cli/utilities/core_sampler/**/*"

build conveyor:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/conveyor"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/conveyor/**/*"

build deliver:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/deliver"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/deliver/**/*"

build ingest envoy:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/ingest_envoy"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/ingest_envoy/**/*"

build mediator:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/mediator"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/mediator/**/*"

build null:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/null"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/null/**/*"

build productfetcher:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/productfetcher"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/productfetcher/**/*"

build update stage:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/update_stage"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/update_stage/**/*"

build vela:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/vela"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/vela/**/*"

build wf inspector:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/wf_inspector"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/wf_inspector/**/*"

build ws annihilator:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/ws_annihilator"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/ws_annihilator/**/*"

build ws metrics:
  interruptible: true
  stage: build-packages
  variables:
    PACKAGE_PATH: "apps/cli/executables/pexable/ws_metrics"
  extends: .build-package
  only:
    changes:
      - "apps/cli/executables/pexable/ws_metrics/**/*"



###############################################
# Build Service and Web Images
###############################################
build workflow:
    interruptible: true
    stage: build
    variables:
        SERVICE_NAME: "workflow"
        PATH_PREFIX: "services/"
    extends: .build

build capability:
    interruptible: true
    stage: build
    variables:
        SERVICE_NAME: "capability"
        PATH_PREFIX: "services/"
    extends: .build

build notification:
    interruptible: true
    stage: build
    variables:
        SERVICE_NAME: "notification"
        PATH_PREFIX: "services/"
    extends: .build

build web:
    interruptible: true
    stage: build
    variables:
        SERVICE_NAME: "web"
        PATH_PREFIX: "apps/"
    extends: .build


###############################################
# Test Stages for Services
###############################################
unit test workflow:
    interruptible: true
    stage: unit-test
    variables:
        SERVICE_NAME: "workflow"
        PATH_PREFIX: "services/"
    extends: .unit-test

unit test capability:
    interruptible: true
    stage: unit-test
    variables:
        SERVICE_NAME: "capability"
        PATH_PREFIX: "services/"
    extends: .unit-test

unit test notification:
    interruptible: true
    stage: unit-test
    variables:
        SERVICE_NAME: "notification"
        PATH_PREFIX: "services/"
    extends: .unit-test


###############################################
# Create Coverage Report
###############################################
.unit test coverage:
    interruptible: true
    stage: test-coverage
    image: python:3.10-slim
    before_script:
        - pip install pytest pytest-cov
    script:
        - coverage combine --append
        - coverage report -i --omit="**/test_*.py,**/_version.py,**/conftest.py,**/*interfaces.py" --skip-empty
        - coverage xml -i --omit="**/test_*.py,**/_version.py,**/conftest.py,**/*interfaces.py" --skip-empty
        - coverage html -i --omit="**/test_*.py,**/_version.py,**/conftest.py,**/*interfaces.py" --skip-empty
    artifacts:
        reports:
          coverage_report:
            coverage_format: cobertura
            path: coverage.xml
        paths:
          - htmlcov/
    dependencies:
      - unit test workflow
      - unit test capability
      - unit test notification
    rules:
        - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH =~ /(^[0-9]\.[0-9]|^[0-9]\.[0-9]\.[0-9])-DEVELOPMENT/'
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'


###############################################
# Push Service and Web Images to Registry
###############################################
push workflow:
    stage: push
    variables:
        SERVICE_NAME: "workflow"
        PATH_PREFIX: "services/"
    extends: .push

push capability:
    stage: push
    variables:
        SERVICE_NAME: "capability"
        PATH_PREFIX: "services/"
    extends: .push

push notification:
    stage: push
    variables:
        SERVICE_NAME: "notification"
        PATH_PREFIX: "services/"
    extends: .push

push web:
    stage: push
    variables:
        SERVICE_NAME: "web"
        PATH_PREFIX: "apps/"
    extends: .push
    # UI tests coming soon!
    # needs:
    #     - unit test dev ui

###############################################
# Clean Pipeline of Service and Web Images
###############################################
clean build workflow:
    stage: .post
    variables:
        SERVICE_NAME: "workflow"
        PATH_PREFIX: "services/"
    extends: .cleanup
    allow_failure: true

clean build capability:
    stage: .post
    variables:
        SERVICE_NAME: "capability"
        PATH_PREFIX: "services/"
    extends: .cleanup
    allow_failure: true

clean build notification:
    stage: .post
    variables:
        SERVICE_NAME: "notification"
        PATH_PREFIX: "services/"
    extends: .cleanup
    allow_failure: true

clean build web:
    stage: .post
    variables:
        SERVICE_NAME: "web"
        PATH_PREFIX: "apps/"
    extends: .cleanup
    allow_failure: true


###############################################
# Deployment Stages
###############################################
.pages:
    interruptible: true
    stage: deploy-coverage-page
    image: python:3.10-slim
    dependencies:
        - unit test coverage
    before_script:
      - pip install -r docs/requirements.txt
      - apt update
      - apt install make
    script:
        - mkdir public
        - mv htmlcov public/htmlcov
        - cd docs && make html && mv _build/html/* ../public/
    artifacts:
        paths:
        - public
        expire_in: 2 weeks
    rules:
      - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH =~ /(^[0-9]\.[0-9]|^[0-9]\.[0-9]\.[0-9])-DEVELOPMENT/'

 # Generate go builder yaml for child pipeline
go generate yaml:
   stage: generate-go-yaml
   image: python:3.10-slim
   before_script:
       - apt update && apt install -y git
       - pip install pyyaml
   script:
       - DEPLOY_ENV=${DEPLOY_ENV} ./ci/bin/generate-go-yaml.py
   rules:
       - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH =~ /(^[0-9]\.[0-9]|^[0-9]\.[0-9]\.[0-9])-DEVELOPMENT/'
         changes:
           - apps/cli/executables/go/**/*
       - if: '$CI_COMMIT_TAG =~ /^end-of-sprint-[0-9]+/ || $CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+-rc[0-9]+/'
         variables:
           # override DEPLOY_ENV
           DEPLOY_ENV: "test"
       - if: '$CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+$/'
         variables:
           DEPLOY_ENV: "prod"
   artifacts:
       paths:
           - generated-go-build-pipeline.yml

 # Trigger child pipeline based on generated go builder yaml
go child pipeline:
   stage: go-trigger
   trigger:
       include:
           - artifact: generated-go-build-pipeline.yml
             job: go generate yaml
       strategy: depend
   rules:
       - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH =~ /(^[0-9]\.[0-9]|^[0-9]\.[0-9]\.[0-9])-DEVELOPMENT/'
         changes:
           - apps/cli/executables/go/**/*
       - if: '$CI_COMMIT_TAG =~ /^end-of-sprint-[0-9]+/ || $CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+-rc[0-9]+/'
         variables:
           # override DEPLOY_ENV
           DEPLOY_ENV: "test"
       - if: '$CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+$/'
         variables:
           DEPLOY_ENV: "prod"

# Development
deploy:
    stage: deploy
    script:
        # Docker doesn't allow variable interpolation when declaring Docker Secret names
        # This sed command finds and replaces "dsoc_ENV_secrets:" with "dsoc_${DEPLOY_ENV}_secrets:"
        - sed -i "s/dsoc_ENV_secrets:/dsoc_${DEPLOY_ENV}_secrets:/g" docker-compose.yml
        - sed -i "s/naasc_ENV_secrets:/naasc_${DEPLOY_ENV}_secrets:/g" docker-compose.yml
        - ENV=$DEPLOY_ENV TAG=$IMAGE_TAG DL_HOST=$DL_HOST ENV_HOST=$ENV_HOST docker stack deploy --compose-file docker-compose.yml workspaces-${DEPLOY_ENV} -e TAG=${IMAGE_TAG}
    rules:
        - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH =~ /(^[0-9]\.[0-9]|^[0-9]\.[0-9]\.[0-9])-DEVELOPMENT/'
          variables:
            IMAGE_TAG: ${CI_COMMIT_BRANCH}
        - if: '$CI_COMMIT_TAG =~ /^end-of-sprint-[0-9]+/ || $CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+-rc[0-9]+/'
          variables:
            IMAGE_TAG: $CI_COMMIT_TAG
            # override DEPLOY_ENV
            DEPLOY_ENV: "test"
            # override DL_HOST
            DL_HOST: https://dl-dsoc-test.nrao.edu
            # override ENV_HOST
            ENV_HOST: ws-test.nrao.edu
        - if: '$CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+$/'
          variables:
            IMAGE_TAG: $CI_COMMIT_TAG
            # override DEPLOY_ENV
            DEPLOY_ENV: "prod"
            # override DL_HOST
            DL_HOST: https://dl-dsoc.nrao.edu
            # override ENV_HOST
            ENV_HOST: ws.nrao.edu

