variables:
  DOCKER_REGION: "registry.cn-qingdao.aliyuncs.com"
  DOCKER_NAMESPACE: "lacus"
  DOCKER_USERNAME: "rainsun20170129"
  DOCKER_PASSWORD: "100200300Wdy*"
  APP_NAME: "canary_fe"
  IMAGE_NAME: "$DOCKER_REGION/$DOCKER_NAMESPACE/$APP_NAME:$CI_COMMIT_REF_NAME-$CI_COMMIT_SHORT_SHA"
  DOCKER_FILE_PATH: "./Dockerfile"

stages:
  - build
  - clear
  - deploy
  - purge

build:
  stage: build
  image: docker:latest
  services:
    - name: docker:dind
  tags:
    - dockerbase
  script:
    - ls -a
    - docker login --username=${DOCKER_USERNAME} --password=${DOCKER_PASSWORD} ${DOCKER_REGION}
    - docker build -t ${IMAGE_NAME} -f ${DOCKER_FILE_PATH} .
    - docker push ${IMAGE_NAME}
    - docker rmi ${IMAGE_NAME}

clear_master:
  stage: clear
  tags:
    - ali2
  only:
    - master
  script:
    - docker stop pan_${APP_NAME}
    - docker rm pan_${APP_NAME}
  allow_failure: true

deploy_master:
  stage: deploy
  tags:
    - ali2
  only:
    - master
  script:
    - ls -a
    - docker login --username=${DOCKER_USERNAME} --password=${DOCKER_PASSWORD} ${DOCKER_REGION}
    - docker pull ${IMAGE_NAME}
    - docker run -d --name pan_${APP_NAME} -p 127.0.0.1:5005:80 ${IMAGE_NAME}
  cache:
    policy: pull

clear_outside:
  stage: clear
  tags:
    - ali2
  only:
    - outside
  script:
    - docker stop ${APP_NAME}
    - docker rm ${APP_NAME}
  allow_failure: true

deploy_outside:
  stage: deploy
  tags:
    - ali2
  only:
    - outside
  script:
    - ls -a
    - docker login --username=${DOCKER_USERNAME} --password=${DOCKER_PASSWORD} ${DOCKER_REGION}
    - docker pull ${IMAGE_NAME}
    - docker run -d --name ${APP_NAME} -p 127.0.0.1:5004:80 ${IMAGE_NAME}
  cache:
    policy: pull

purge:
  stage: purge
  tags:
    - dockerbase
  only:
    - master
  script:
    - docker run --rm dns:codebook-master
