stages:
  - build
  - deploy

build_master:
  stage: build
  
  image: docker:latest
  services:
    - docker:dind
  
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY

  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE" ./
    - docker push "$CI_REGISTRY_IMAGE"
  
  only:
    - master

deploy:
  stage: deploy

  image: jaromirpufler/docker-openssh-client

  variables:
    SSH_DEPLOY_KEY: $DEPLOY_PRIVATE_KEY

  script:
    - ssh -4 $DEPLOY_HOST "docker service update --image $CI_REGISTRY_IMAGE --force $DEPLOY_SERVICE;"

  only:
    - master
