stages:
  - build
  - deploy
variables:
  GIT_STRATEGY: none
  GIT_SSH_COMMAND: 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
  UPSTREAM_TAG: $CI_COMMIT_TAG
  PROJECT_NAME: 'huamu_party_building_3d'
  BUILD_COMMAND_PROD: 'npm run build'


before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

.build_artifact:
  # image: registry-vpc.cn-hangzhou.aliyuncs.com/mdt/ndm_lite:build
  image: registry.cn-hangzhou.aliyuncs.com/mdt/node12_docker:build
  stage: build
  tags:
    - docker
  script:
    # TODO(Deo): deploy prod docker runner
    # - git clone -b ${UPSTREAM_TAG} --depth 1 ssh://git@172.16.148.75:9522/haute-couture/huamu/collector_admin.git $PROJECT_NAME
    - git clone -b ${UPSTREAM_TAG} --depth 1 ssh://git@gitlab.idatatlas.com:9522/haute-couture/huamu/$PROJECT_NAME.git
      $PROJECT_NAME
    - cd ./$PROJECT_NAME
    - rm -rf .temp
    - npm install
    # NONTICE: generally all hashed files uploaded to oss for cd used
    - $COMMAND
    # NOTICE: all real time updated files will be uploaded to nginx
    - cd dist
    - tar -czvf ../${UPSTREAM_TAG}.tar.gz .
    - mv ../${UPSTREAM_TAG}.tar.gz ${CI_PROJECT_DIR}
  artifacts:
    paths:
      - ${UPSTREAM_TAG}.tar.gz
    expire_in: 2 days

.deploy_artifact:
  stage: deploy
  script:
    - mkdir -p /var/www/statics/$PROJECT_NAME
    - tar --touch -xvf ${UPSTREAM_TAG}.tar.gz -C /var/www/statics/$PROJECT_NAME
    - rm -f `find "/var/www/statics/${PROJECT_NAME}" -mtime +3 -type f`

build_prod:
  extends: .build_artifact
  only:
    - /^prod-.*$/
  variables:
    COMMAND: $BUILD_COMMAND_PROD

deploy_prod:
  extends: .deploy_artifact
  tags:
    - vpc_nginx
  only:
    - /^prod-.*$/
  dependencies:
    - build_prod

