stages:
  - build

variables:
  REGISTRY: hub.alterway.fr

.build_push_template: &build_push_definition
  stage: build
  only:
    - preprod
    - master
  script:
    - docker build --pull -t $REGISTRY/php:$CI_JOB_NAME $CI_JOB_NAME/ >> /dev/null
    - docker push $REGISTRY/php:$CI_JOB_NAME > /dev/null
    - docker tag $REGISTRY/php:$CI_JOB_NAME $NAMESPACE/php:$CI_JOB_NAME > /dev/null
    - docker push $NAMESPACE/php:$CI_JOB_NAME > /dev/null
    - |
         if [[ "$CI_BUILD_REF_NAME" = "master" ]]; then
           docker tag $REGISTRY/php:$CI_JOB_NAME $CI_REGISTRY_IMAGE:$CI_JOB_NAME > /dev/null
           docker push $CI_REGISTRY_IMAGE:$CI_JOB_NAME > /dev/null
         fi

before_script:
  - if [[ "$CI_BUILD_REF_NAME" != master ]]; then REGISTRY="$REGISTRY/$CI_BUILD_REF_NAME"; fi
  - if [[ "$CI_BUILD_REF_NAME"  = "master" ]]; then docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY; fi
  - NAMESPACE=$REGISTRY/library

5.7:
  <<: *build_push_definition

5.6:
  <<: *build_push_definition

5.5:
  <<: *build_push_definition

stages:
  - build

variables:
  REGISTRY: hub.alterway.fr

.build_push_template: &build_push_definition
  stage: build
  only:
    - preprod
    - master
  script:
    - docker build --pull -t $REGISTRY/php:${CI_JOB_NAME:1} ${CI_JOB_NAME:1}/ >> /dev/null
    - docker push $REGISTRY/php:${CI_JOB_NAME:1} > /dev/null
    - docker tag $REGISTRY/php:${CI_JOB_NAME:1} $NAMESPACE/php:${CI_JOB_NAME:1} > /dev/null
    - docker push $NAMESPACE/php:${CI_JOB_NAME:1} > /dev/null
    - |
         if [[ "$CI_BUILD_REF_NAME" = "master" ]]; then
           docker tag $REGISTRY/php:${CI_JOB_NAME:1} $CI_REGISTRY_IMAGE:${CI_JOB_NAME:1} > /dev/null
           docker push $CI_REGISTRY_IMAGE:${CI_JOB_NAME:1} > /dev/null
         fi

before_script:
  - if [[ "$CI_BUILD_REF_NAME" != master ]]; then REGISTRY="$REGISTRY/$CI_BUILD_REF_NAME"; fi
  - if [[ "$CI_BUILD_REF_NAME"  = "master" ]]; then docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY; fi
  - NAMESPACE=$REGISTRY/library

v5.7:
  <<: *build_push_definition

v5.6:
  <<: *build_push_definition

v5.5:
  <<: *build_push_definition

Remove first char of $CI_JOB_NAME (cannot start with a number)
Remove first char of $CI_JOB_NAME (cannot start with a number)