# CI Build Template
.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_PIPELINE_SOURCE == "merge_request_event"'
          when: always