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

variables:
    PROJECT_NAME: "workspaces"

image: docker:19.03.12

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

before_script:
    - echo "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
    - echo "$CI_COMMIT_REF_NAME"

.build:
    script:
        - '[ -z "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" ] && BRANCH=$CI_COMMIT_BRANCH || BRANCH=$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME'
        - echo "Building branch -- ${BRANCH}"
        - |
            if [ $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH ] ; then \
                docker build -t ${REGISTRY_URL}/${PROJECT_NAME}/${SERVICE_NAME}:${CI_COMMIT_BRANCH} -f services/${SERVICE_NAME}/Dockerfile.dev . ; \
            else
                docker build -t ${REGISTRY_URL}/${PROJECT_NAME}/${SERVICE_NAME}:${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} -f services/${SERVICE_NAME}/Dockerfile.dev . ; \
            fi
        - docker tag ${REGISTRY_URL}/${PROJECT_NAME}/${SERVICE_NAME}:${BRANCH} ${REGISTRY_URL}/${PROJECT_NAME}/${SERVICE_NAME}:${CI_COMMIT_SHORT_SHA}
        - echo "BRANCH_TAG=$BRANCH" >> build.env
    artifacts:
        reports:
            dotenv: build.env
    rules:
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
          when: always
        - if: $CI_COMMIT_MESSAGE =~ /\A(?i)Revert/
          when: always
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
          changes:
            - .gitlab-ci.yml
            - services/${SERVICE_NAME}/**/*
            - apps/cli/**/*.{py,sh}
            - shared/**/*.{py,sh}
          when: always
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE =~ /\A(?i)-ops/'
          changes:
            - Dockerfile.base

.cleanup: 
    script:
        - |
            printf "%s\n" "- Removing Images -" \
            "${REGISTRY_URL}/${PROJECT_NAME}/${SERVICE_NAME}:${BRANCH_TAG}" \
            "${REGISTRY_URL}/${PROJECT_NAME}/${SERVICE_NAME}:${CI_COMMIT_SHORT_SHA}"
        - docker image rm "${REGISTRY_URL}/${PROJECT_NAME}/${SERVICE_NAME}:${BRANCH_TAG}" "${REGISTRY_URL}/${PROJECT_NAME}/${SERVICE_NAME}:${CI_COMMIT_SHORT_SHA}"

# Build Base Image
build base image:
    stage: build-base
    script:
        - docker build -t ${REGISTRY_URL}/ops/base:${PROJECT_NAME} -f Dockerfile.base . ; \
    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}
    rules:
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE =~ /\A(?i)-ops/'
          changes:
            - Dockerfile.base
          when: always

# 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


# Test Stages

# Push Stages
# push dev workflow:
#     stage: push-dev
#     script:
#         - echo "$HARBOR_PASSWORD" | docker login -u "$HARBOR_USER" --password-stdin $HARBOR_URL
#         - |
#             if [ "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" != "" ]; then \
#                 echo "Pushing PR branch image to registry: ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"; \
#                 docker push marconi.aoc.nrao.edu/workspaces/workflow:$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME ; \
#                 if [ "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" != "main" ]; then \
#                     docker tag marconi.aoc.nrao.edu/workspaces/workflow:$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME marconi.aoc.nrao.edu/workspaces/workflow:dev-branch ; \
#                     docker push marconi.aoc.nrao.edu/workspaces/workflow:dev-branch ; \
#                 fi
#             elif [ "$CI_COMMIT_TAG" != "" ]; then \
#                 echo "Pushing Tagged image to registry: ${CI_COMMIT_TAG}"; \
#                 docker push marconi.aoc.nrao.edu/workspaces/workflow:$CI_COMMIT_TAG ; \
#             fi
#         - docker push marconi.aoc.nrao.edu/workspaces/workflow:$CI_COMMIT_SHORT_SHA
#     rules:
#         - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
#           when: always
#         - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
#           changes:
#             - .gitlab-ci.yml
#             - services/workflow/**/*
#             - apps/cli/**/*.{py,sh}
#             - shared/**/*.{py,sh}
#           when: manual
#         - if: $CI_COMMIT_MESSAGE =~ /\A(?i)Revert/
#           when: manual

# push dev capability:
#     stage: push-dev
#     script:
#         - echo "$HARBOR_PASSWORD" | docker login -u "$HARBOR_USER" --password-stdin $HARBOR_URL
#         - |
#             if [ "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" != "" ]; then \
#                 echo "Pushing PR branch image to registry: ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"; \
#                 docker push marconi.aoc.nrao.edu/workspaces/capability:$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME ; \
#                 if [ "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" != "main" ]; then \
#                     docker tag marconi.aoc.nrao.edu/workspaces/capability:$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME marconi.aoc.nrao.edu/workspaces/capability:dev-branch ; \
#                     docker push marconi.aoc.nrao.edu/workspaces/capability:dev-branch ; \
#                 fi
#             elif [ "$CI_COMMIT_TAG" != "" ]; then \
#                 echo "Pushing Tagged image to registry: ${CI_COMMIT_TAG}"; \
#                 docker push marconi.aoc.nrao.edu/workspaces/capability:$CI_COMMIT_TAG ; \
#             fi
#         - docker push marconi.aoc.nrao.edu/workspaces/capability:$CI_COMMIT_SHORT_SHA
#     rules:
#         - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
#           when: always
#         - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
#           changes:
#             - .gitlab-ci.yml
#             - services/capability/**/*
#             - apps/cli/**/*.{py,sh}
#             - shared/**/*.{py,sh}
#           when: manual
#         - if: $CI_COMMIT_MESSAGE =~ /\A(?i)Revert/
#           when: manual

# Cleanup
clean build workflow:
    stage: clean-images
    variables:
        SERVICE_NAME: "workflow"
    extends: .cleanup
    dependencies:
        - build dev workflow

clean build capability:
    stage: clean-images
    variables:
        SERVICE_NAME: "capability"
    extends: .cleanup
    dependencies:
        - build dev capability

### 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