

stages:          # List of stages for jobs, and their order of execution
  - build
  - deploy-dev
#  - deploy-test
  - deploy-prod




variables:
  REGISTRY_GITLAB: registry.gitlab.com/seasoul/mes-antd-admin
  BUILD_NUMBER: ${CI_COMMIT_SHORT_SHA}-${CI_PIPELINE_ID}
  KUBE_CONFIG: /etc/deploy/config
cache:
  paths:
    - ./mod_cache/


build-rpc-job:       # This job runs in the build stage, which runs first.
    stage: build
    image: tico/docker:latest
    tags:
        - k8s-runner
    variables:
        NAME: antd-admin
        PORT: 8080
        APP_NAME: antd-admin
    script:
        - ls
        - mkdir -p ./.ssh
        - mv $SSH_PRIVATE_KEY ./.ssh/id_rsa
        - chmod 600 ./.ssh/id_rsa
        - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
        # 如果是登录私有仓库则需要更换如下代码：
        - docker build -f Dockerfile -t $APP_NAME:v${BUILD_NUMBER} . --network=host #编译成镜像
        - docker tag $APP_NAME:v${BUILD_NUMBER} $REGISTRY_GITLAB/$APP_NAME:v${BUILD_NUMBER} #将镜像加上tag
        - docker push $REGISTRY_GITLAB/$APP_NAME:v${BUILD_NUMBER} #提交到镜像仓库
        - docker tag $APP_NAME:v${BUILD_NUMBER} $REGISTRY_GITLAB/$APP_NAME:latest #将镜像加上tag
        - docker push $REGISTRY_GITLAB/$APP_NAME:latest #提交到镜像仓库
    after_script:
        - docker rmi $APP_NAME:v${BUILD_NUMBER} #删除镜像
        - docker rmi $REGISTRY_GITLAB/$APP_NAME:v${BUILD_NUMBER} #删除镜像
        - docker images|grep none|awk '{print $3}'|xargs docker rmi # 清理无tag的镜像
#        - docker rm $(docker ps --all -q -f status=exited) # 清理容器

deploy-rpc-dev-job:
    stage: deploy-dev
    image: registry.cn-hangzhou.aliyuncs.com/haoshuwei24/kubectl:1.16.6
    tags:
        - k8s-runner
    script:
        - mkdir -p /etc/deploy
        - echo $kube_config |base64 -d > $KUBE_CONFIG
        - kubectl get nodes
        - kubectl apply -f deploy/mes-antd-admin-dev.yaml # 部署测试环境

# deploy-rpc-test-job:
#     stage: deploy-dev
#     image: registry.cn-hangzhou.aliyuncs.com/haoshuwei24/kubectl:1.16.6
#     tags:
#         - k8s-runner
#     script:
#         - mkdir -p /etc/deploy
#         - mv $kube_config $KUBE_CONFIG
#         - kubectl get nodes
#         - kubectl apply -f deploy/mes-antd-admin-test.yaml # 部署灰度测试环境

deploy-rpc-prod-job:
    stage: deploy-prod
    image: registry.cn-hangzhou.aliyuncs.com/haoshuwei24/kubectl:1.16.6
    tags:
        - k8s-runner
    script:
        - mkdir -p /etc/deploy
        - echo $kube_config |base64 -d > $KUBE_CONFIG
        - kubectl get nodes
        - kubectl apply -f deploy/mes-antd-admin-prod.yaml # 部署正式环境



