stages:
  - build
  - deploy


build:
  stage: build
  script:
    - if [[ $CI_BUILD_REF_SLUG == 'master' ]]; then export NEW_RELIC_ENABLED='true'; else export NEW_RELIC_ENABLED='false'; fi
    - echo "ENV NEW_RELIC_ENABLED=$NEW_RELIC_ENABLED" >> Dockerfile
    - echo "ENV NEW_RELIC_LICENSE_KEY=$NEW_RELIC_LICENSE_KEY" >> Dockerfile
    - echo "ENV NEW_RELIC_LABELS=branch:$CI_COMMIT_REF_NAME" >> Dockerfile
    - echo "ENV NEW_RELIC_APP_NAME=$CI_REGISTRY_IMAGE" >> Dockerfile
    - echo "ENV NEW_RELIC_NO_CONFIG_FILE=true" >> Dockerfile
    - cat Dockerfile
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_BUILD_REF_SLUG-$CI_PIPELINE_ID" -t "$CI_REGISTRY_IMAGE:$CI_BUILD_REF_SLUG-latest" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_BUILD_REF_SLUG-$CI_PIPELINE_ID"
    - docker push "$CI_REGISTRY_IMAGE:$CI_BUILD_REF_SLUG-latest"

deploy_dev:
  stage: deploy
  script:
    - ssh $DEV_USER@$DEV_HOST kubectl set image deployment/company company="$CI_REGISTRY_IMAGE:$CI_BUILD_REF_SLUG-$CI_PIPELINE_ID"
  only:
    - dev

deploy_master:
  stage: deploy
  script:
    - ssh $PROD_USER@$PROD_HOST kubectl set image deployment/company company="$CI_REGISTRY_IMAGE:$CI_BUILD_REF_SLUG-$CI_PIPELINE_ID"
  only:
    - master
