# Cleanup Images Template
.cleanup:
    script:
        - NAME="${REGISTRY_URL}/workspaces/${SERVICE_NAME}"
        - |
            printf "%s\n" "- Removing Images -" \
            "${NAME}:${IMAGE_TAG}"
        - docker image rm --force "${REGISTRY_URL}/workspaces/${SERVICE_NAME}:${IMAGE_TAG}"
    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
        changes:
          - ${PATH_PREFIX}${SERVICE_NAME}/**/*
      - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
        variables:
          IMAGE_TAG: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
        changes:
          - ${PATH_PREFIX}${SERVICE_NAME}/**/*
      - if: '$CI_COMMIT_TAG =~ /^end-of-sprint-[0-9]+/'
        variables:
          IMAGE_TAG: $CI_COMMIT_TAG
        changes:
          - ${PATH_PREFIX}${SERVICE_NAME}/**/*
      - if: '$CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+-rc[0-9]+/'
        variables:
          IMAGE_TAG: $CI_COMMIT_TAG
        changes:
          - ${PATH_PREFIX}${SERVICE_NAME}/**/*
      - if: '$CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+$/'
        variables:
          IMAGE_TAG: $CI_COMMIT_TAG
        changes:
          - ${PATH_PREFIX}${SERVICE_NAME}/**/*
