stages:
  - Build
#  - Test
  - Docker build
  - Deploy

#services:
#  - mongo

variables:
  CONTAINER_IMAGE: 'registry.kszk.bme.hu/bodysch/backend:$CI_COMMIT_REF_NAME'
#  MONGODB_TEST_URI: 'mongodb://mongo/bodysch-test'

Build:
  stage: Build
  tags: [kszk]
  image: node:14
  script:
    - npm install
  cache:
    paths:
      - node_modules/
  artifacts:
    expire_in: 1 day
    paths:
      - node_modules/

#Test:
#  stage: Test
#  tags: [kszk]
#  image: node:14
#  script:
#    - npm test
#  artifacts:
#    expire_in: 1 week
#    paths:
#      - junit.xml
#    reports:
#      junit: junit.xml

Docker build:
  stage: Docker build
  tags: [kszk]
  only:
    - master
    - dev
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: ['']
  script:
    - echo "{\"auths\":{\"registry.kszk.bme.hu\":{\"username\":\"$REGISTRY_USER\",\"password\":\"$REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CONTAINER_IMAGE

Deploy dev:
  stage: Deploy
  tags: [kszk-deploy] # request the custom SSH executor
  only:
    - dev
  script: # your deploy logic
    - echo "$REGISTRY_PASSWORD" | docker login registry.kszk.bme.hu --username $REGISTRY_USER --password-stdin
    - cp docker-compose.dev.yml /home/kszk-gitlab-deployer/bodysch-dev/
    - cd /home/kszk-gitlab-deployer/bodysch-dev/
    - docker-compose -f docker-compose.dev.yml --no-ansi pull
    - docker-compose -f docker-compose.dev.yml up -d
    - docker system prune -f
  variables:
    SSH_HOST: donald.sch.bme.hu # required
    SSH_USER: kszk-gitlab-deployer # default: kszk-gitlab-deployer
    SSH_PORT: 10122 # default: 22

# Deploy prod:
#   stage: Deploy
#   tags: [kszk-deploy] # request the custom SSH executor
#   only:
#     - master
#   script: # your deploy logic
#     - echo "$REGISTRY_PASSWORD" | docker login registry.kszk.bme.hu --username $REGISTRY_USER --password-stdin
#     - cp docker-compose.yml /home/kszk-gitlab-deployer/kszkepzes-prod/
#     - cd /home/kszk-gitlab-deployer/kszkepzes-prod/
#     - docker-compose --no-ansi pull
#     - docker-compose up -d
#     - docker system prune -f
#   variables:
#     SSH_HOST: donald.sch.bme.hu # required
#     SSH_USER: kszk-gitlab-deployer # default: kszk-gitlab-deployer
#     SSH_PORT: 10122 # default: 22
