stages:
  - build
  - alt_build
  - publish

fast-build:
  stage: build
  image: $CI_REGISTRY_IMAGE
  before_script:
    - mv /customCache/node_modules ./
    - cp -rf /customCache/www ./
    - cp -rf /customCache/hooks ./
  script:
    - node ./node_modules/gulp/bin/gulp webBuild
  after_script:
    - ls -la dist/web/
    - du -csh dist/web/
  artifacts:
    untracked: true
    paths:
      - dist/web
    expire_in: 60 minutes
  only:
    - master
    - tags
failsafe-build:
  stage: alt_build
  when: on_failure
  image: node:10
  script:
    - yarn
    - node ./node_modules/gulp/bin/gulp config --env default
    - node ./node_modules/gulp/bin/gulp webBuild
  artifacts:
    untracked: true
    paths:
      - dist/web
    expire_in: 60 minutes
  only:
    - master
    - tags
docker-for-fast-build:
  stage: alt_build
  when: on_failure
  allow_failure: true
  tags:
    - doppler-docker
  image: docker:latest
  services:
    - docker:dind
  script:
    - echo 'FROM node:10' > Dockerfile
    - echo 'WORKDIR /customCache' >> Dockerfile
    - echo 'COPY ./ ./' >> Dockerfile
    - echo 'RUN du -s `find . -maxdepth 1 | egrep -v "^\.$"`>/before.txt' >> Dockerfile
    - echo 'RUN yarn' >> Dockerfile
    - echo 'RUN du -s `find . -maxdepth 1 | egrep -v "^\.$"`>/after.txt' >> Dockerfile
    - echo 'WORKDIR /build' >> Dockerfile
    - echo "RUN diff /before.txt /after.txt || true" >> Dockerfile
    - docker login -u "gitlab-ci-token" -p "$CI_BUILD_TOKEN" $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE" .
    - docker push "$CI_REGISTRY_IMAGE"


pages:
  stage: publish
  image: node:10
  variables:
    GIT_STRATEGY: none
  script:
    - ls -la public || true
    - mv dist/web/www public
  artifacts:
    untracked: true
    paths:
      - public
  only:
    - master
mirror:
  stage: publish
  image: liaohuqiu/rsync
  before_script:
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - >-
      [[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >
      ~/.ssh/config
  script:
    - mv dist/web/www public
    - rsync -az -e ssh ./public/ gammanu@1000i100.fr:~/g1.money/dev-cesium
  after_script:
    - ssh gammanu@1000i100.fr "~/g1.money/update-cesium.sh"
  only:
    - master


release-web:
  stage: publish
  image: node:10
  variables:
    GIT_STRATEGY: none
  script:
    - ls -la www || true
    - mv dist/web/www www
  artifacts:
    untracked: true
    paths:
      - www
  only:
    - tags

