variables:
  RELEASE_NAME: "SERVICE_NAME"
  SERVICE_PORT: 1099
  REGISTRY_URL: 751503455312.dkr.ecr.us-west-2.amazonaws.com/pgnonprod
  
stages:
  - Build
  - Test
  - ReleaseCleanup
  - ReleaseDeploy
  - Validation
  
Build:
  stage: Build
  script:
  - mvn  clean install
  - $(aws ecr get-login --no-include-email --region us-west-2)
  - docker build -t 751503455312.dkr.ecr.us-west-2.amazonaws.com/pgnonprod:$CI_PIPELINE_ID .
  - docker push 751503455312.dkr.ecr.us-west-2.amazonaws.com/pgnonprod:$CI_PIPELINE_ID
  
ReleaseCleanup:
   stage: ReleaseCleanup
   script: 
   -  echo `pwd`
   -  cd ./helm && /sbin/helm delete --purge $RELEASE_NAME && exit 0
   allow_failure: true

ReleaseDeploy:
   stage: ReleaseDeploy
   script:
   - echo `pwd`
   - sed -i s/#BUILD_ID#/$CI_PIPELINE_ID/g ./helm/service/values.yaml 
   - sed -i s/#SERVICE_PORT#/$SERVICE_PORT/g ./helm/service/values.yaml
   - cd ./helm && /sbin/helm install service --name $RELEASE_NAME
   
Validation:
    stage: Validation
    script:
    - sleep 45
    - if [ `curl -s -o /dev/null -I -w "%{http_code}" https://pgtest.altimetrik.com/$RELEASE_NAME` = "200" ]; then exit 0; else exit 1; fi
    allow_failure: false