# Package application needed to run tests & build the image on next stage
build policy:
  variables:
    IMAGE_NAME: 'policy' # name of the microservice
    IMAGE_NAME_TEST: 'policy-test' # name of the microservice
    IMAGE_TAG: '0.0.1' # tag of the container image (production, development, etc)
  stage: build
  script:
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/src/main/docker/Dockerfile.multistage.jvm ./src/$IMAGE_NAME/ --target builder
  rules:
    - changes:
        - src/$IMAGE_NAME/**
        - .gitlab-ci.yml

# Run tests, build & push the image
unit_test policy:
  variables:
    IMAGE_NAME: 'policy' # name of the microservice
    IMAGE_NAME_TEST: 'policy-test' # name of the microservice
    IMAGE_TAG: '0.0.1' # tag of the container image (production, development, etc)
  stage: unit_test
  needs:
    - build policy
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/src/main/docker/Dockerfile.multistage.jvm ./src/$IMAGE_NAME/ --target unit-test
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/src/main/docker/Dockerfile.multistage.jvm ./src/$IMAGE_NAME/ --target release
    - docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
  rules:
    - changes:
        - src/$IMAGE_NAME/**
        - .gitlab-ci.yml

# Deployment of automation service in Kubernetes Cluster
deploy policy:
  stage: deploy
  needs:
    - build policy
    - unit_test policy
  script:
    - kubectl version
    - kubectl get all
    - kubectl apply -f "manifests/policyservice.yaml"
    - kubectl delete pods --selector app=policyservice
    - kubectl get all