stages:
    - init
    - build-base
    - push-base
    - build-dev
    - push-dev
    - deploy-dev
    - deploy-test

image: docker:19.03.12

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

# Check if branch is associated with a MR
# This helps solves the issue of duplicate pipelines when a branch that's associated with a PR is changed
# It creates two pipelines. It checks if the branch has an open PR. If it does, it'll cancel the push-to-
# branch pipeline to allow the Merge-Request-event pipeline to run.
# Drawbacks: the cancelled push-to-PR pipeline is marked as a Failed pipeline
# based on issue: https://gitlab.com/gitlab-org/gitlab/-/issues/201845
# which should have a less hacky fix in a future Gitlab-CI release
check-merge-request:
    stage: init
    before_script:
        - apk add --no-cache curl
    script: 
        - 'export OPENED_MR_ON_BRANCH=$(curl --header "Private-Token: $GITLAB_TOKEN" "https://$CI_SERVER_HOST/api/v4/merge_requests?project_id=$CI_PROJECT_ID&source_branch=$CI_COMMIT_BRANCH&state=opened&scope=all")'
        - |
            if [ "${OPENED_MR_ON_BRANCH}" != "[]" ]; then \
                echo "There is at least one MR opened on branch $CI_COMMIT_BRANCH. Stopping the push pipeline in favor of the merge request pipeline"; \
                exit 1; \
            fi
    rules:
        - if: $GITLAB_TOKEN && $CI_PIPELINE_SOURCE == 'push'

# Build Base if changed
build base image:
    stage: build-base
    script:
        - docker build -t marconi.aoc.nrao.edu/ops/base:workspaces -f Condaless.dockerfile.v2 . ; \
    rules:
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE =~ /\A(?i)-ops/'
          changes:
            - Condaless.dockerfile.v2

# Build Stages
build dev workflow:
    stage: build-dev
    script:
        - |
            if [ "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" != "" ]; then \
                echo "Building PR branch: ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"; \
                docker build -t marconi.aoc.nrao.edu/workspaces/workflow:"$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" -f services/workflow/Dockerfile.dev . ; \
                docker tag marconi.aoc.nrao.edu/workspaces/workflow:"$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" marconi.aoc.nrao.edu/workspaces/workflow:"$CI_COMMIT_SHORT_SHA" ; \
                echo "WORKFLOW_BRANCH=$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" >> build.env; \
            else
                echo "Building branch: ${CI_COMMIT_BRANCH}"; \
                docker build -t marconi.aoc.nrao.edu/workspaces/workflow:"$CI_COMMIT_BRANCH" -f services/workflow/Dockerfile.dev . ; \
                docker tag marconi.aoc.nrao.edu/workspaces/workflow:"$CI_COMMIT_BRANCH" marconi.aoc.nrao.edu/workspaces/workflow:"$CI_COMMIT_SHORT_SHA" ; \
                if [ "$CI_COMMIT_BRANCH" != "main" ]; then \
                    echo "WORKFLOW_BRANCH=$CI_COMMIT_BRANCH" >> build.env; \
                fi
            fi
    artifacts:
        reports:
            dotenv: build.env
    rules:
        - if: $CI_COMMIT_MESSAGE =~ /\A(?i)Revert/
          when: always
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
          changes:
            - .gitlab-ci.yml
          when: always
        - if: $GITLAB_TOKEN && $CI_PIPELINE_SOURCE == 'push'
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
          changes:
            - services/workflow/**/*
            - apps/cli/**/*.{py,sh}
            - shared/**/*.{py,sh}
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE =~ /\A(?i)-ops/'
          changes:
            - Condaless.dockerfile.v2

build dev capability:
    stage: build-dev
    script:
        - |
            if [ "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" != "" ]; then \
                echo "Building PR branch: ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"; \
                docker build -t marconi.aoc.nrao.edu/workspaces/capability:"$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" -f services/capability/Dockerfile.dev . ; \
                docker tag marconi.aoc.nrao.edu/workspaces/capability:"$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" marconi.aoc.nrao.edu/workspaces/capability:"$CI_COMMIT_SHORT_SHA" ; \
                echo "CAPABILITY_BRANCH=$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" >> build.env; \
            else
                echo "Building branch: ${CI_COMMIT_BRANCH}"; \
                docker build -t marconi.aoc.nrao.edu/workspaces/capability:"$CI_COMMIT_BRANCH" -f services/capability/Dockerfile.dev . ; \
                docker tag marconi.aoc.nrao.edu/workspaces/capability:"$CI_COMMIT_BRANCH" marconi.aoc.nrao.edu/workspaces/capability:"$CI_COMMIT_SHORT_SHA" ; \
                if [ "$CI_COMMIT_BRANCH" != "main" ]; then \
                    echo "CAPABILITY_BRANCH=$CI_COMMIT_BRANCH" >> build.env; \
                fi
            fi
    artifacts:
        reports:
            dotenv: build.env
    rules:
        - if: $CI_COMMIT_MESSAGE =~ /\A(?i)Revert/
          when: always
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
          changes:
            - .gitlab-ci.yml
          when: always
        - if: $GITLAB_TOKEN && $CI_PIPELINE_SOURCE == 'push'
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
          changes:
            - services/capability/**/*
            - apps/cli/**/*.{py,sh}
            - shared/**/*.{py,sh}
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE =~ /\A(?i)-ops/'
          changes:
            - Condaless.dockerfile.v2

# Test Stages

# Push Base Image Stage
push base image:
    stage: push-base
    script:
        - echo "$HARBOR_PASSWORD" | docker login -u "$HARBOR_USER" --password-stdin $HARBOR_URL 
        - docker push marconi.aoc.nrao.edu/ops/base:workspaces
    rules:
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
          changes:
            - Condaless.dockerfile.v2
          when: always
# 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
            else
                echo "Pushing branch image to registry: ${CI_COMMIT_BRANCH}"; \
                docker push marconi.aoc.nrao.edu/workspaces/workflow:$CI_COMMIT_BRANCH ; \
            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" || $CI_PIPELINE_SOURCE == 'push')
          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
            else
                echo "Pushing branch image to registry: ${CI_COMMIT_BRANCH}"; \
                docker push marconi.aoc.nrao.edu/workspaces/capability:$CI_COMMIT_BRANCH ; \
            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" || $CI_PIPELINE_SOURCE == 'push'
          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 runner:
#     stage: .post
#     script:
#         - docker images -q --filter=reference="marconi.aoc.nrao.edu/workspaces/*" | xargs docker rmi -f


### 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
    rules:
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
          when: always
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "push"'
          when: manual

# Testing
deploy test:
    stage: deploy-test
    script:
        |
            echo "branch: ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"
            echo "tag: ${CI_COMMIT_TAG}"
    rules:
        - if: $CI_COMMIT_TAG
          when: manual