stages:
    - init
    - build-dev
    - push-dev
    - deploy-dev
    # - .post

# workflow:
#     rules:
#         - if: $CI_COMMIT_MESSAGE =~ /\A(?i)-wip/
#           when: never
        # - if: '$CI_PIPELINE_SOURCE == "push"'
        # - if: $CI_MERGE_REQUEST_IID
        # - if: $CI_COMMIT_TAG
        # - if: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == $CI_DEFAULT_BRANCH


image: docker:19.03.12

# variables:
#     # Docker Variables
#     DOCKER_TLS_CERTDIR: "/certs"

# services:
#     - name: docker:19.03.12-dind

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

# Check if branch is associated with a MR
# This fixes duplicate pipelines for the build stage when building a standalone branch
# and when building a branch that's associated with a PR
# 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
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" ; \
            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" ; \
            fi
    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}

# build dev capability:
#     stage: build-dev
#     script:
#         - 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
#     rules:
#         - if: $CI_COMMIT_MESSAGE =~ /\A(?i)Revert/
#           when: always
#         - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
#           changes:
#             - .gitlab-ci.yml
#           when: always
#         - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
#           changes:
#             - services/capability/**/*
#             - apps/cli/**/*.{py,sh}
#             - shared/**/*.{py,sh}

# Push
push dev workflow:
    stage: push-dev
    script:
        - echo "$HARBOR_PASSWORD" | docker login -u "$HARBOR_USER" --password-stdin $HARBOR_URL
        # - docker tag marconi.aoc.nrao.edu/workspaces/workflow:$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME marconi.aoc.nrao.edu/workspaces/workflow:dev
        # - docker push marconi.aoc.nrao.edu/workspaces/workflow:$CI_COMMIT_SHORT_SHA
        # - docker push marconi.aoc.nrao.edu/workspaces/workflow:$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
        # - docker push marconi.aoc.nrao.edu/workspaces/workflow:dev
        - |
            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" != "development" ]; 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_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE !~ /\A(?i)Merge/) || $CI_PIPELINE_SOURCE == 'push'
          when: manual
        - if: ($CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_MESSAGE =~ /\A(?i)Merge/ && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "development")
          when: always

# push dev capability:
#     stage: push-dev
#     script:
#         - echo "$HARBOR_PASSWORD" | docker login -u "$HARBOR_USER" --password-stdin $HARBOR_URL
#         - docker tag marconi.aoc.nrao.edu/workspaces/capability:$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME marconi.aoc.nrao.edu/workspaces/capability:dev
#         - docker push marconi.aoc.nrao.edu/workspaces/capability:$CI_COMMIT_SHORT_SHA
#         - docker push marconi.aoc.nrao.edu/workspaces/capability:$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
#         - docker push marconi.aoc.nrao.edu/workspaces/capability:dev
#     rules:
#         - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "development"'
#           when: manual

# Cleanup
# clean runner:
#     stage: .post
#     script:
#         - docker images -q --filter=reference="marconi.aoc.nrao.edu/workspaces/*" | xargs docker rmi -f

# Tests


# # Deploy
# deploy dev:
#     stage: deploy-dev
#     script:
#         - docker-compose --version
#     when: manual

# Deploy
# deploy dev:
#     stage: deploy-dev
#     script:
#         - env SWARM_NODE_ENV="dev" docker stack deploy --compose-file docker-compose.dev.yml workspaces-dev
#     environment:
#         name: development
#         url: https://wirth.aoc.nrao.edu
#     when: manual