# CI Build Template
.build:
    script:
        - echo "Building branch or tag -- ${IMAGE_TAG}"
        - NAME="${REGISTRY_URL}/workspaces/${SERVICE_NAME}"
        - docker build -t ${NAME}:${CI_COMMIT_SHORT_SHA} -f ${PATH_PREFIX}${SERVICE_NAME}/Dockerfile . --build-arg DEPLOY_ENV=${DEPLOY_ENV} --build-arg WS_VERSION=${VERSION}
             --build-arg CAPO_PROFILE=dsoc-${DEPLOY_ENV} --build-arg API_TOKEN=${API_TOKEN} --target prod
        - docker tag ${NAME}:${CI_COMMIT_SHORT_SHA} ${NAME}:${IMAGE_TAG}
        - echo "TAG=${IMAGE_TAG}" >> build.env
    artifacts:
        reports:
            dotenv: build.env
    rules:
        - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH =~ /(^[0-9]\.[0-9]|^[0-9]\.[0-9]\.[0-9])-DEVELOPMENT/'
          variables:
            IMAGE_TAG: $CI_COMMIT_BRANCH
            VERSION: 0.0.1+$CI_COMMIT_BRANCH
            DEPLOY_ENV: "dev"
          changes:
            - ${PATH_PREFIX}${SERVICE_NAME}/**/*
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
          variables:
            IMAGE_TAG: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
            VERSION: 0.0.1+$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
            DEPLOY_ENV: "dev"
          changes:
            - ${PATH_PREFIX}${SERVICE_NAME}/**/*
        - if: '$CI_COMMIT_TAG =~ /^end-of-sprint-[0-9]+/'
          variables:
            IMAGE_TAG: $CI_COMMIT_TAG
            VERSION: 0.0.2+$CI_COMMIT_TAG
            # override DEPLOY_ENV
            DEPLOY_ENV: "test"
          changes:
            - ${PATH_PREFIX}${SERVICE_NAME}/**/*
        - if: '$CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+-rc[0-9]+/ || $CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0.9]+-rc[0-9]+/'
          variables:
            IMAGE_TAG: $CI_COMMIT_TAG
            VERSION: $CI_COMMIT_TAG
            # override DEPLOY_ENV
            DEPLOY_ENV: "test"
          changes:
            - ${PATH_PREFIX}${SERVICE_NAME}/**/*
        - if: '$CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+$/ || $CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/'
          variables:
            IMAGE_TAG: $CI_COMMIT_TAG
            VERSION: $CI_COMMIT_TAG
            # override DEPLOY_ENV
            DEPLOY_ENV: "prod"
          changes:
            - ${PATH_PREFIX}${SERVICE_NAME}/**/*
