# automatically generated by auxilium
.template-build:
    stage: build
    interruptible: false
    {%- if aux.ci.mechanism == "gitlab" %}
    variables:
        BUILD_DEPS: ""
    {%- endif %}
    script:
    {% if aux.ci.mechanism == "gitlab" -%}
    -   cd $ROOT
    -   IMG=$(echo "$PROJECT_REGISTRY/$SERVICE_NAME:$IMG_TAG" | awk '{print tolower($0)}')
    -   DEF_IMG=$(echo "$PROJECT_REGISTRY/$SERVICE_NAME:$CI_DEFAULT_BRANCH" | awk
        '{print tolower($0)}')
    -   |
        for DEP in $BUILD_DEPS
        do
            case "$DEP" in
                *:* ) DEP_IMG="$CI_REGISTRY/$DEP";;
                * ) DEP_IMG="$PROJECT_REGISTRY/$DEP:$IMG_TAG";;
            esac
            DEP_IMG=$(echo $DEP_IMG | awk '{print tolower($0)}')
            TMP="${DEP_IMG%:*}" # remove all after of :
            DEP_SERVICE_NAME="${TMP##*/}" # remove all infront of / (repeated ##)
            docker pull -q $DEP_IMG
            docker tag $DEP_IMG $DEP_SERVICE_NAME
        done
    -   |
        if [ $DOCKER_LAYER_CACHE = "yes" ]
        then
            (docker pull -q $IMG && docker tag $IMG $SERVICE_NAME) || (docker pull -q $DEF_IMG && docker tag $DEF_IMG $SERVICE_NAME) || true
        fi
    -   docker compose --file=$DOCKER_DIR/compose.yml build $SERVICE_NAME
    -   docker tag $SERVICE_NAME $IMG
    -   docker push $IMG
    {%- elif aux.ci.mechanism == "mixed" -%}
    -   python3 devops/CI/python_ci.py $CI_JOB_STAGE:$SERVICE_NAME
    {%- endif %}
{% include 'tags.jinja2' %}
.template-run:
    stage: run
    interruptible: true
    variables:
        OVERWRITE_COMPOSE: ""
{% if aux.ci.mechanism == "gitlab" %}
        BUILD_DEPS: ""
{% endif %}
    script:
{% if aux.ci.mechanism == "gitlab" %}
    -   cd $ROOT
    -   PULL_FROM_REG="$SERVICES $BUILD_DEPS"
    -   |
        for SERVICE_NAME in $PULL_FROM_REG
        do
            if [ -f "$DOCKER_DIR/$SERVICE_NAME.dockerfile" ]; then
                IMG="$PROJECT_REGISTRY/$SERVICE_NAME:$IMG_TAG"
                IMG=$(echo $IMG | awk '{print tolower($0)}')
                docker pull -q $IMG
                docker tag $IMG $SERVICE_NAME
            fi
        done
    -   ALL_OVERWRITE_WITH_F=""
    -   |
        if [ "$OVERWRITE_COMPOSE" != "" ]
        then
            ALL_OVERWRITE_WITH_F="--file=$(echo "$OVERWRITE_COMPOSE" | sed 's/ / --file=/g')"
        fi
    -   docker compose --file=$DOCKER_DIR/compose.yml $ALL_OVERWRITE_WITH_F up --no-build
        --abort-on-container-exit $SERVICES
{% elif aux.ci.mechanism == "mixed" %}
    -   python3 devops/CI/python_ci.py $CI_JOB_STAGE:$SERVICE_NAME
{% endif %}
{% include 'tags.jinja2' -%}
