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
  - deploy

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

test:
  stage: test
  script:
    - apk add bash
    - docker network create test-ec
    - docker run --rm --network=test-ec --env-file .env --name $SERVICE -d $REGISTRY/medina/$SERVICE:$VERSION
    - docker run --rm --network=test-ec toschneck/wait-for-it $SERVICE:7890 -t 240
    - bash test/test.sh
  after_script:
    - SERVICE=$(grep SERVICE MANIFEST | cut -d '=' -f2)
    - docker kill $SERVICE || docker network rm test-ec
    - docker network rm test-ec

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/wazuh-vat-$SERVICE:$VERSION
    - docker tag $REGISTRY/medina/$SERVICE:$VERSION $MEDINA_REGISTRY/$MEDINA_REG_PATH/wazuh-vat-$SERVICE:latest
    - docker push $MEDINA_REGISTRY/$MEDINA_REG_PATH/wazuh-vat-$SERVICE:$VERSION
    - docker push $MEDINA_REGISTRY/$MEDINA_REG_PATH/wazuh-vat-$SERVICE:latest
    - docker logout $MEDINA_REGISTRY
  only:
    - master

deploy:
  stage: deploy
  script:
    - docker run --rm curlimages/curl -I -X POST "https://xlab:110bb809200c797e6031787b51a049b819@cicd.medina.esilab.org/jenkins/job/medina/job/wp3/job/task_3.2/job/wazuh-vat-evidence-collector-deploy/buildWithParameters?PRJ_ENV=dev&PRJ_IMAGE_TAG=latest&YAMLS_OVERRIDE="
  only:
    - master

