stages:
  - build
  - deploy

build:
  stage: build
  image: docker:stable
  services:
    - docker:dind
  variables:
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
  script:
    - echo $DOCKERHUBUSER
    - echo $DOCKERHUBPASSWORD
    - docker login -u $DOCKERHUBUSER -p $DOCKERHUBPASSWORD docker.io
    - docker build --tag=l1ll1/strudel2_backend .
    - docker push l1ll1/strudel2_backend:latest
  tags:
    - build_strudel2_backend

deploy_test:
  stage: deploy
  only:
    - test@hpc-team/strudel2_backend
  tags: 
    - deploy_test
  variables:
    DOCKER_HOST: unix:///var/run/docker.sock
  script:
    - echo "deploying dev"
    - docker login -u $DOCKERHUBUSER -p $DOCKERHUBPASSWORD docker.io
    - docker stop strudel2_backend || echo "Failed to stop existing container"
    - docker ps -a -q | xargs -I{} docker rm  {} || echo "No containers to remove"
    - docker images -q | xargs -I{} docker rmi {} || echo "No images to remove"
    - docker pull l1ll1/strudel2_backend:latest
    - docker run -d -p 8080:8080 -p 8090:8090 --name strudel2_backend l1ll1/strudel2_backend:latest || echo "Unable to start image ... possibly the old wsgi process is still running?"
  dependencies:
    - build

deploy_dev:
  stage: deploy
  only:
    - dev@hpc-team/strudel2_backend
  tags: 
    - deploy_dev
    - deploy_dev_pawsey
  variables:
    DOCKER_HOST: unix:///var/run/docker.sock
  script:
    - echo "deploying dev"
    - docker login -u $DOCKERHUBUSER -p $DOCKERHUBPASSWORD docker.io
    - docker stop strudel2_backend || echo "Failed to stop existing container"
    - docker ps -a -q | xargs -I{} docker rm  {} || echo "No containers to remove"
    - docker images -q | xargs -I{} docker rmi {} || echo "No images to remove"
    - docker pull l1ll1/strudel2_backend:latest
    - docker run -d -p 8080:8080 -p 8090:8090 --name strudel2_backend l1ll1/strudel2_backend:latest || echo "Unable to start image ... possibly the old wsgi process is still running?"
  dependencies:
    - build
