stages:
    - build-base
    - push-base
    - build-dev
    - unit-test-dev
    - test-coverage
    - push-dev
    - clean-images
    # - deploy-dev
    # - deploy-test

variables:
    PROJECT_NAME: "workspaces"
    DEFAULT_DEPLOY_ENV: "dev"

image: docker:19.03.12

workflow:
  rules:
    - if: $CI_MERGE_REQUEST_IID
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

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

# Build Base Image
build base image:
    stage: build-base
    script:
        - docker build -t ${REGISTRY_URL}/ops/base:${PROJECT_NAME} -f Dockerfile.base .
        - docker tag ${REGISTRY_URL}/ops/base:${PROJECT_NAME} ${REGISTRY_URL}/ops/base:${CI_COMMIT_SHORT_SHA}
    rules:
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE =~ /\A(?i)-ops/'
          changes:
            - Dockerfile.base

# Push Base Image Stage
push base image:
    stage: push-base
    script:
        - echo "$HARBOR_PASSWORD" | docker login -u "$HARBOR_USER" --password-stdin $REGISTRY_URL
        - docker push ${REGISTRY_URL}/ops/base:${PROJECT_NAME}
        - docker push ${REGISTRY_URL}/ops/base:${CI_COMMIT_SHORT_SHA}
    rules:
        - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_MESSAGE =~ /\A(?i)-ops/'
          changes:
            - Dockerfile.base

# Build Stages
build dev workflow:
    stage: build-dev
    variables:
        SERVICE_NAME: "workflow"
    extends: .build

build dev capability:
    stage: build-dev
    variables:
        SERVICE_NAME: "capability"
    extends: .build

build dev notification:
    stage: build-dev
    variables:
        SERVICE_NAME: "notification"
    extends: .build

## Test Stages ##

# Unit Tests
unit test dev workflow:
    stage: unit-test-dev
    variables:
        SERVICE_NAME: "workflow"
    extends: .unit-test
    needs:
        - build dev workflow

unit test dev capability:
    stage: unit-test-dev
    variables:
        SERVICE_NAME: "capability"
    extends: .unit-test
    needs:
        - build dev capability

unit test dev notification:
    stage: unit-test-dev
    variables:
        SERVICE_NAME: "notification"
    extends: .unit-test
    needs:
        - build dev notification

# disabled stage
.unit test coverage:
    stage: test-coverage
    before_script:
        - 
    variables:
        SERVICE_NAME_1: "capability"
        SERVICE_NAME_2: "workflow"
        SERVICE_NAME_3: "notification"
        CONTAINER_NAME_1: ${SERVICE_NAME_1}_${CI_COMMIT_SHORT_SHA}
        CONTAINER_NAME_2: ${SERVICE_NAME_2}_${CI_COMMIT_SHORT_SHA}
        CONTAINER_NAME_2: ${SERVICE_NAME_3}_${CI_COMMIT_SHORT_SHA}
    script:
        - docker container run --name ${CONTAINER_NAME_1} ${REGISTRY_URL}/${PROJECT_NAME}/${SERVICE_NAME_1}:${CI_COMMIT_SHORT_SHA} ./bin/run-tests.sh -cr xml -o ${SERVICE_NAME_1}.xml
        - docker container run --name ${CONTAINER_NAME_2} ${REGISTRY_URL}/${PROJECT_NAME}/${SERVICE_NAME_2}:${CI_COMMIT_SHORT_SHA} ./bin/run-tests.sh -cr xml -o ${SERVICE_NAME_2}.xml
        - docker container run --name ${CONTAINER_NAME_3} ${REGISTRY_URL}/${PROJECT_NAME}/${SERVICE_NAME_3}:${CI_COMMIT_SHORT_SHA} ./bin/run-tests.sh -cr xml -o ${SERVICE_NAME_3}.xml
        - docker cp ${CONTAINER_NAME_1}:coverage.xml ./${CONTAINER_NAME_1}.xml
        - docker cp ${CONTAINER_NAME_2}:coverage.xml ./${CONTAINER_NAME_2}.xml
        - docker cp ${CONTAINER_NAME_3}:coverage.xml ./${CONTAINER_NAME_3}.xml

# Push Stages
push dev workflow:
    stage: push-dev
    variables:
        SERVICE_NAME: "workflow"
    extends: .push
    needs:
        - unit test dev workflow

push dev capability:
    stage: push-dev
    variables:
        SERVICE_NAME: "capability"
    extends: .push
    needs:
        - unit test dev capability

push dev notification:
    stage: push-dev
    variables:
        SERVICE_NAME: "notification"
    extends: .push
    needs:
        - unit test dev notification

# Cleanup
clean build workflow:
    stage: clean-images
    variables:
        SERVICE_NAME: "workflow"
    extends: .cleanup
    rules:
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE =~ /\A(?i)-debug/'
          when: never

clean build capability:
    stage: clean-images
    variables:
        SERVICE_NAME: "capability"
    extends: .cleanup
    rules:
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE =~ /\A(?i)-debug/'
          when: never

clean build notification:
    stage: clean-images
    variables:
        SERVICE_NAME: "notification"
    extends: .cleanup
    rules:
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE =~ /\A(?i)-debug/'
          when: never

# Deploy Stages

# Development 
# deploy dev:
#     stage: deploy-dev
#     script:
#         - |
#             if [ "$CI_COMMIT_BRANCH" != "" ]; then \
#                 if [ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]; then \
#                     echo "Deploying merged branch: ${CI_COMMIT_BRANCH} to dev environment"; \
#                     SWARM_NODE_ENV="dev" TAG_TO_DEPLOY="development" docker stack deploy --compose-file docker-compose.dev.yml workspaces-dev ; \
#                 else
#                     if [ "$CAPABILITY_BRANCH" != "" ]; then \
#                         echo "Updating Capability Service: ${CI_COMMIT_BRANCH} in dev environment"; \
#                         docker service update --image marconi.aoc.nrao.edu/workspaces/capability:"$CAPABILITY_BRANCH" workspaces-dev_capability
#                     fi
#                     if [ "$WORKFLOW_BRANCH" != "" ]; then \
#                         echo "Updating Workflow Service: ${CI_COMMIT_BRANCH} in dev environment"; \
#                         docker service update --image marconi.aoc.nrao.edu/workspaces/workflow:"$WORKFLOW_BRANCH" workspaces-dev_workflow
#                     fi
#                     if [ "$WORKFLOW_BRANCH" == "" && "$CAPABILITY_BRANCH" == "" ]; then \
#                         echo "Deploying branch: ${CI_COMMIT_BRANCH} to dev environment"; \
#                         SWARM_NODE_ENV="dev" TAG_TO_DEPLOY="${CI_COMMIT_BRANCH}" docker stack deploy --compose-file docker-compose.dev.yml workspaces-dev ; \
#                     fi
#                 fi
#             fi
#             if [ "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" != "" ]; then \
#                 if [ "$CAPABILITY_BRANCH" != "" ]; then \
#                         echo "Updating Capability Service with PR Branch: ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} in dev environment"; \
#                         docker service update --image marconi.aoc.nrao.edu/workspaces/capability:"$CAPABILITY_BRANCH" workspaces-dev_capability
#                     fi
#                     if [ "$WORKFLOW_BRANCH" != "" ]; then \
#                         echo "Updating Workflow Service with PR Branch: ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} in dev environment"; \
#                         docker service update --image marconi.aoc.nrao.edu/workspaces/workflow:"$WORKFLOW_BRANCH" workspaces-dev_workflow
#                     fi
#                     if [ "$WORKFLOW_BRANCH" == "" && "$CAPABILITY_BRANCH" == "" ]; then \
#                         echo "Deploying branch: ${CI_COMMIT_BRANCH} to dev environment"; \
#                         SWARM_NODE_ENV="dev" TAG_TO_DEPLOY="${CI_COMMIT_BRANCH}" docker stack deploy --compose-file docker-compose.dev.yml workspaces-dev ; \
#                     fi
#                 echo "Deploying PR branch: ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} to dev environment"; \
#                 SWARM_NODE_ENV="dev" TAG_TO_DEPLOY="dev-branch" docker stack deploy --compose-file docker-compose.dev.yml workspaces-dev ; \
#             fi
#     environment: development
#     rules:
#         - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
#           when: always
#         - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
#           when: manual

# Testing
# deploy test:
#     stage: deploy-test
#     script:
#         |
#             echo "branch: ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"
#             echo "tag: ${CI_COMMIT_TAG}"
#             SWARM_NODE_ENV="test" TAG_TO_DEPLOY="${CI_COMMIT_TAG}" docker stack deploy --compose-file docker-compose.dev.yml workspaces-dev
#     rules:
#         - if: $CI_COMMIT_TAG
#           when: manual