stages:
  - dependencies
  - test
  - setup
  - build
  - staging
  - release
  - deploy

dependencies:production:
  image: node:$NODE_VERSION
  stage: dependencies
  only:
    refs:
      - master
  # before_script:
  #   - npm -g config set user root
  script:
    - npm ci
  cache:
    key:
      files:
        - package-lock.json
    paths:
      - node_modules
  artifacts:
    expire_in: 1 hour
    paths:
      - node_modules/

# test:
#   image: node:$NODE_VERSION
#   stage: setup
#   dependencies:
#     - dependencies:production
#   only:
#     - master
#   before_script:
#     - npm run lint
#   script:
#     - CI=true npm run test

setup:production:
  image: node:$NODE_VERSION
  stage: setup
  dependencies:
    - dependencies:production
  only:
    - master
  script:
    - npm run build:production
  artifacts:
    expire_in: 1 hour
    paths:
      - build

build:production:
  image: docker:20.10.21
  stage: build
  dependencies:
    - setup:production
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  services:
    - docker:20.10.21-dind
  only:
    - master
  before_script:
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
  script:
    - docker build -t ${CI_REGISTRY}/brandx/bxt-app/production:${CI_COMMIT_SHA} .
    - docker tag ${CI_REGISTRY}/brandx/bxt-app/production:${CI_COMMIT_SHA} ${CI_REGISTRY}/brandx/bxt-app/production:latest
    - docker push ${CI_REGISTRY}/brandx/bxt-app/production

deploy:production:
  image: docker:20.10.21
  stage: deploy
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_HOST: ssh://deploy@bxt-prod01.brandxtech.ca
    DEPLOY_IMAGE: ${CI_REGISTRY}/brandx/bxt-app/production:latest
    DEPLOY_TARGET: deploy@bxt-prod01.brandxtech.ca
  only:
    - master
  environment:
    name: Production
    url: https://dashboard.adaptiveagriculture.ca
  before_script: &deploy_tunnel
    - "apk --update add openssh-client"
    - "eval $(ssh-agent -s)"
    - "echo \"$DEPLOY_SSH_PRIV_KEY\" | tr -d '\r' | ssh-add - > /dev/null"
    # - "ssh -Mo StrictHostKeyChecking=no -S deploy-ctrl-socket -fnNTL /var/run/docker.sock:/var/run/docker.sock ${DEPLOY_TARGET}"
    # - "ssh -S deploy-ctrl-socket -O check ${DEPLOY_TARGET}"
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh -o StrictHostKeyChecking=no ${DEPLOY_TARGET}
  script:
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
    - docker stack deploy --with-registry-auth -c docker-compose.prod.yml webui
  when: manual
  allow_failure: false

dependencies:staging:
  image: node:$NODE_VERSION
  stage: dependencies
  only:
    refs:
      - staging_environment
  # before_script:
  #   - npm -g config set user root
  script:
    - npm ci
  cache:
    key:
      files:
        - package-lock.json
    paths:
      - node_modules
  artifacts:
    expire_in: 1 hour
    paths:
      - node_modules/

# test:staging:
#   image: node:$NODE_VERSION
#   stage: setup
#   dependencies:
#     - dependencies:staging
#   only:
#     - staging_environment
#   before_script:
#     - npm run lint
#   script:
#     - CI=true npm run test

setup:staging:
  image: node:$NODE_VERSION
  stage: setup
  dependencies:
    - dependencies:staging
  only:
    - staging_environment
  script:
    - npm run build:staging
  artifacts:
    expire_in: 1 hour
    paths:
      - build

build:base:
  image: docker:20.10.21
  stage: build
  dependencies:
    - dependencies:staging
  variables:
    DOCKER_DRIVER: overlay2
    PROJECT: brandx/bxt-app
    DOCKER_TLS_CERTDIR: ""
  services:
    - docker:20.10.21-dind
  only:
    - master
  before_script:
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
  script:
    - docker build -t ${CI_REGISTRY}/${PROJECT}/base:${CI_COMMIT_SHA} -f Dockerfile.base .
    - docker tag ${CI_REGISTRY}/${PROJECT}/base:${CI_COMMIT_SHA} ${CI_REGISTRY}/${PROJECT}/base:staging
    - docker push ${CI_REGISTRY}/${PROJECT}/base
  when: manual
  allow_failure: true

build:staging:
  image: docker:20.10.21
  stage: build
  dependencies:
    - setup:staging
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  services:
    - docker:20.10.21-dind
  only:
    - staging_environment
  before_script:
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
  script:
    - docker build -t ${CI_REGISTRY}/brandx/bxt-app/production:${CI_COMMIT_SHA} .
    - docker tag ${CI_REGISTRY}/brandx/bxt-app/production:${CI_COMMIT_SHA} ${CI_REGISTRY}/brandx/bxt-app/production:staging
    - docker push ${CI_REGISTRY}/brandx/bxt-app/production:staging

staging:deploy:
  image: docker:20.10.21
  stage: staging
  variables:
    DOCKER_DRIVER: overlay2
    DEPLOY_IMAGE: ${CI_REGISTRY}/brandx/bxt-app/production:staging
    DOCKER_HOST: ssh://deploy@staging.brandxtech.ca
    DEPLOY_TARGET: deploy@staging.brandxtech.ca
    VITE_MAPBOX_ACCESS_TOKEN: $MAPBOX_ACCESS_TOKEN
  only:
    - staging_environment
  environment:
    name: Staging
    url: https://staging.brandxtech.ca
  before_script: &staging_tunnel
    - "apk --update add openssh-client"
    - "eval $(ssh-agent -s)"
    - "echo \"$STAGING_SSH_PRIV_KEY\" | tr -d '\r' | ssh-add - "
    - "echo \"$MAPBOX_ACCESS_TOKEN\""
    # - "ssh -Mo StrictHostKeyChecking=no -S deploy-ctrl-socket -fnNTL /var/run/docker.sock:/var/run/docker.sock ${DEPLOY_TARGET}"
    # - "ssh -S deploy-ctrl-socket -O check ${DEPLOY_TARGET}" 
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh -o StrictHostKeyChecking=no ${DEPLOY_TARGET}
  script:
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
    - docker stack deploy --with-registry-auth -c docker-compose.staging.yml webui
  when: manual
  allow_failure: false
  
dependencies:development:
  image: node:$NODE_VERSION
  stage: dependencies
  only:
    refs:
      - dev_environment
  # before_script:
    # - npm -g config set user root
  script:
    - npm ci
  cache:
    key:
      files:
        - package-lock.json
    paths:
      - node_modules
  artifacts:
    expire_in: 1 hour
    paths:
      - node_modules/

# test:development:
#   image: node:$NODE_VERSION
#   stage: setup
#   dependencies:
#     - dependencies:development
#   only:
#     - dev_environment
#   before_script:
#     - npm run lint
#   script:
#     - CI=true npm run test

setup:development:
  image: node:$NODE_VERSION
  stage: setup
  dependencies:
    - dependencies:development
  only:
    - dev_environment
  script:
    - npm run build:development
  artifacts:
    expire_in: 1 hour
    paths:
      - build

build:development:
  image: docker:20.10.21
  stage: build
  dependencies:
    - setup:development
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  services:
    - docker:20.10.21-dind
  only:
    - dev_environment
  before_script:
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
  script:
    - docker build -t ${CI_REGISTRY}/brandx/bxt-app/production:${CI_COMMIT_SHA} .
    - docker tag ${CI_REGISTRY}/brandx/bxt-app/production:${CI_COMMIT_SHA} ${CI_REGISTRY}/brandx/bxt-app/production:development
    - docker push ${CI_REGISTRY}/brandx/bxt-app/production:development

deploy:development:
  image: docker:20.10.21
  stage: deploy
  variables:
    DOCKER_DRIVER: overlay2
    DEPLOY_IMAGE: ${CI_REGISTRY}/brandx/bxt-app/production:development
    DOCKER_HOST: ssh://deploy@bxt-dev.adaptiveagriculture.ca
    DEPLOY_TARGET: deploy@bxt-dev.adaptiveagriculture.ca
  only:
    - dev_environment
  environment:
    name: Development
    url: https://bxt-dev.adaptiveagriculture.ca
  before_script: &dev_tunnel
    - "apk --update add openssh-client"
    - "eval $(ssh-agent -s)"
    - ssh -V
    - cat /etc/os-release
    # - "echo DEPLOY_TARGET=${DEPLOY_TARGET}"
    # - "echo DOCKER_HOST=${DOCKER_HOST}"
    - "echo \"$DEVELOPMENT_SSH_PRIV_KEY\" | tr -d '\r' | ssh-add - "
    # - "ssh -Mo StrictHostKeyChecking=no -S deploy-ctrl-socket -fnNTL /var/run/docker.sock:/var/run/docker.sock ${DEPLOY_TARGET}"
    # - "ssh -S deploy-ctrl-socket -O check ${DEPLOY_TARGET}" 
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh -o StrictHostKeyChecking=no ${DEPLOY_TARGET}
  script:
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
    - docker stack deploy --with-registry-auth -c docker-compose.dev.yml webui
  when: manual
  allow_failure: false

dependencies:streamline:
  image: node:$NODE_VERSION
  stage: dependencies
  only:
    refs:
      - streamline
  # before_script:
  #   - npm -g config set user root
  script:
    - npm ci
  cache:
    key:
      files:
        - package-lock.json
    paths:
      - node_modules
  artifacts:
    expire_in: 1 hour
    paths:
      - node_modules/

# test:
#   image: node:$NODE_VERSION
#   stage: setup
#   dependencies:
#     - dependencies:production
#   only:
#     - master
#   before_script:
#     - npm run lint
#   script:
#     - CI=true npm run test

setup:streamline:
  image: node:$NODE_VERSION
  stage: setup
  dependencies:
    - dependencies:streamline
  only:
    - streamline
  script:
    - npm run build:streamline
  artifacts:
    expire_in: 1 hour
    paths:
      - build

build:streamline:
  image: docker:20.10.21
  stage: build
  dependencies:
    - setup:streamline
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  services:
    - docker:20.10.21-dind
  only:
    - streamline
  before_script:
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
  script:
    - docker build -t ${CI_REGISTRY}/brandx/bxt-app/streamline:${CI_COMMIT_SHA} .
    - docker tag ${CI_REGISTRY}/brandx/bxt-app/streamline:${CI_COMMIT_SHA} ${CI_REGISTRY}/brandx/bxt-app/streamline:latest
    - docker push ${CI_REGISTRY}/brandx/bxt-app/streamline:${CI_COMMIT_SHA}
    - docker push ${CI_REGISTRY}/brandx/bxt-app/streamline:latest

deploy:streamline:
  image: docker:20.10.21
  stage: deploy
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_HOST: ssh://deploy@streamline.adaptiveagriculture.ca
    DEPLOY_IMAGE: ${CI_REGISTRY}/brandx/bxt-app/streamline:${CI_COMMIT_SHA}
    DEPLOY_TARGET: deploy@streamline.adaptiveagriculture.ca
  only:
    - streamline
  environment:
    name: Streamline
    url: https://streamline.adaptiveagriculture.ca
  before_script: &deploy_tunnel
    - "apk --update add openssh-client"
    - "eval $(ssh-agent -s)"
    - "echo \"$STREAMLINE_SSH_PRIV_KEY\" | tr -d '\r' | ssh-add - > /dev/null"
    # - "ssh -Mo StrictHostKeyChecking=no -S deploy-ctrl-socket -fnNTL /var/run/docker.sock:/var/run/docker.sock ${DEPLOY_TARGET}"
    # - "ssh -S deploy-ctrl-socket -O check ${DEPLOY_TARGET}"
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh -o StrictHostKeyChecking=no ${DEPLOY_TARGET}
  script:
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
    - docker stack deploy --with-registry-auth -c docker-compose.streamline.yml webui
  when: manual
  allow_failure: false