image: nexus-registry.xlab.si:5001/docker:dind

variables:
  REGISTRY: registry-gitlab.xlab.si
  MEDINA_REGISTRY: optima-medina-docker-dev.artifact.tecnalia.com
  MEDINA_REG_PATH: wp3/t32

before_script:
  - export SERVICE=$(grep SERVICE MANIFEST | cut -d '=' -f2)
  - export VERSION=$(grep VERSION MANIFEST | cut -d '=' -f2)

stages:
  - build
  - test
  - push

build:
  stage: build
  script: 
    - docker build --no-cache -t $REGISTRY/medina/$SERVICE:$VERSION .

test:
  stage: test
  script:
    - apk add bash
    - docker stop $SERVICE || true && docker rm $SERVICE || true
    - docker run --name $SERVICE -d $REGISTRY/medina/$SERVICE:$VERSION
    - sleep 5
    - bash test/test.sh
    - docker stop $SERVICE && docker container rm $SERVICE

push:
  stage: push
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $REGISTRY
    - docker tag $REGISTRY/medina/$SERVICE:$VERSION $REGISTRY/medina/$SERVICE:latest
    - docker push $REGISTRY/medina/$SERVICE:$VERSION
    - docker push $REGISTRY/medina/$SERVICE:latest
    - docker logout $REGISTRY
    - docker login $MEDINA_REGISTRY -u medina.fordevelopers@gmail.com -p AKCp8kqMZkcPRPGZhHBw7uKFsyifF1iHb2ZvbBy5PK88wD8EdeSHZqFsc4h1wp3M2oVYGazhv
    - docker tag $REGISTRY/medina/$SERVICE:$VERSION $MEDINA_REGISTRY/$MEDINA_REG_PATH/$SERVICE:$VERSION
    - docker tag $REGISTRY/medina/$SERVICE:$VERSION $MEDINA_REGISTRY/$MEDINA_REG_PATH/$SERVICE:latest
    - docker push $MEDINA_REGISTRY/$MEDINA_REG_PATH/$SERVICE:$VERSION
    - docker push $MEDINA_REGISTRY/$MEDINA_REG_PATH/$SERVICE:latest
    - docker logout $MEDINA_REGISTRY
  only:
    - master
