stages:
  - compile
  - build
  - deploy

variables:
  PROJECT_NAME: ${CI_PROJECT_NAME}
  K8S_FILE: "deploy.yml"
  PROJECT_IMAGE: harbor.cloud.prolog.org/test/${CI_PROJECT_NAME}
before_script:
  - PROJECT_IMAGE_TAG=${PROJECT_IMAGE}:${CI_COMMIT_SHA:0:5}
#开始构建jar包
compile:
  image: harbor.cloud.prolog.org/base-solution/maven:3-openjdk-8
  stage: compile
  only:
    refs:
     - dev   #对应分支代码分支
  script:
     - mvn -q clean package -Dmaven.test.skip=true
  artifacts:
    expire_in: 1 day  #工作目录保存时间
    paths:
      - ./upcloud-base-wh-operate-service/target/*.jar #获取构建的jar包，此路径随对应项目修改
#  when: manual  #手动执行，注释后会自动在每次提交变更后执行

#开始构建镜像并上传至harbor
build:
  stage: build
  image: harbor.cloud.prolog.org/base-solution/drone-kaniko:bate
  variables:
    PLUGIN_REPO: "test/${CI_PROJECT_NAME}"      #此处为上传harbor时候的容器名称
    PLUGIN_REGISTRY: "harbor.cloud.prolog.org"
    PLUGIN_USERNAME: "$DOCKER_USERNAME"       #项目设置里面cicd 配置变量
    PLUGIN_PASSWORD: "$DOCKER_PASSWORD"       #项目设置里面cicd 配置变量
    PLUGIN_SKIP_TLS_VERIFY: "true"
    PLUGIN_SKIP_TLS_VERIFY_PULL: "true"
    PLUGIN_CACHE: "false"
    PLUGIN_TAGS: "${CI_COMMIT_SHA:0:5}"
  script:
    - PLUGIN_TAGS="${CI_COMMIT_SHA:0:5}"
    - /kaniko/plugin.sh
  only:
    refs:
     - dev   #对应分支代码分支
  when: manual  #手动执行，注释后会自动在每次提交变更后执行

#发布流程
k8s-deploy:
  stage: deploy
  variables:
    PROJECT_PORT: 8080          #配置文件里面的端口需要和deployment容器端口一致 次端口只能在k8s内部访问
    PROJECT_NODEPORT: 30821     #nodeport 为该服务在k8s集群外访问端口
    PROJECT_NAME: ${CI_PROJECT_NAME} #启用变量配。
    PROJECT_NAMESPACE: test           # k8s集群内部的命名空间namespaces
    PROJECT_ACTIVE: k8s               # 项目对应的环境配置文件
  image: harbor.cloud.prolog.org/base-solution/kubectl:v1
  script:
    - kubectl get nodes
    - kubectl version
    - sed -i "s#{PROJECT_NAME}#$PROJECT_NAME#g;
      s#{PROJECT_NODEPORT}#$PROJECT_NODEPORT#g;
      s#{PROJECT_PORT}#$PROJECT_PORT#g;
      s#{PROJECT_NAMESPACE}#$PROJECT_NAMESPACE#g;
      s#{PROJECT_IMAGE}#$PROJECT_IMAGE_TAG#g;
      s#{PROJECT_ACTIVE}#$PROJECT_ACTIVE#g" ./deploy.yml
    - cat ./deploy.yml
    - kubectl delete -f ./deploy.yml
    - kubectl apply -f ./deploy.yml
  only:
    refs:
     - dev   #对应分支代码分支
  when: manual  #手动执行，注释后会自动在每次提交变更后执行
