sudo: required
language: minimal
dist: trusty
services:
  - docker
git:
    submodules: false
env:
  global:
    - PROD_IMAGE_NAME=atst-prod
    - TESTER_IMAGE1_NAME=atst-tester-nocrls
    - TESTER_IMAGE2_NAME=atst-tester
cache:
  directories:
    - crl

before_install:
  # Use sed to replace the SSH URL with the public URL
  - sed -i 's/git@github.com:/https:\/\/github.com\//' .gitmodules
  # Manually initialize submodules
  - git submodule update --init --recursive
install:
  - curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.10.4/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/

before_script:
  - rm -rf ./crl/*
  - docker run -d --name postgres96 postgres:9.6-alpine
  - docker run -d --name redis redis:4.0.10-alpine
  - docker run --link postgres96:postgres96 --link redis:redis waisbrot/wait
  - export postgres_ip="$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" postgres96)"
  - export redis_ip="$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" redis)"
  - docker login -u $ATAT_DOCKER_REGISTRY_USERNAME -p $ATAT_DOCKER_REGISTRY_PASSWORD $ATAT_DOCKER_REGISTRY_URL
  - docker build --tag "${TESTER_IMAGE1_NAME}" --add-host "postgreshost:${postgres_ip}" --add-host "redishost:${redis_ip}" . -f deploy/docker/tester/Dockerfile

script:
  - docker run -d --entrypoint='/bin/sh' -t --name current-atst-tester "${TESTER_IMAGE1_NAME}"
  - docker container exec -t current-atst-tester script/sync-crls
  - docker commit current-atst-tester "${TESTER_IMAGE2_NAME}"
  - docker cp current-atst-tester:/opt/atat/atst/crl/. ./crl/
  - docker container stop current-atst-tester
  - docker run --add-host "postgreshost:${postgres_ip}" --add-host "redishost:${redis_ip}" "${TESTER_IMAGE2_NAME}"

before_deploy:
  - docker build --tag "${PROD_IMAGE_NAME}" . -f deploy/docker/prod/Dockerfile
  - git_sha="$(git rev-parse --short HEAD)"
  - remote_image_name="${ATAT_DOCKER_REGISTRY_URL}/${PROD_IMAGE_NAME}:${git_sha}"
  - docker tag "${PROD_IMAGE_NAME}" "${remote_image_name}"
  - docker images
  - docker push "${remote_image_name}"

deploy:
  provider: script
  script: deploy/kubernetes/atst-update-deploy.sh
  on:
    branch: continuous-deployment
