variables:
  SONAR_URL: http://sonar2.cloud.prolog.org/
  SONAR_LOGIN: c6b851ca3eb86f571a6900eb17dfb64e5283e918
  GIT_CLONE_PATH: $CI_BUILDS_DIR/repo/$CI_CONCURRENT_ID/$CI_PROJECT_NAME/$CI_COMMIT_REF_NAME
  PROJECT_NAME: ${CI_PROJECT_NAME}
  K8S_FILE: "deploy.yml"
  GIT_STRATEGY: none
  #MAVEN_OPTS: "-Dmaven.repo.local=/builds/repo/m2"
  MAVEN_OPTS: "-Dorg.slf4j.simpleLogger.defaultLogLevel=WARN"
  #  CI_DEBUG_TRACE: "true"

stages:
  - compile
  - sonar-check
  - publish-sonar-result
  - set-code-check-baseline
  - deploy-jar
  - build-img
  - k8s-deploy
image: harbor.cloud.prolog.org/base-solution/maven:3-openjdk-8

mvn-compile:
  stage: compile
  variables:
    GIT_STRATEGY: clone
  script:
    - mvn -B clean  install package


sonar-check:
  stage: sonar-check
  variables:
    GIT_STRATEGY: none
  script:
    - >
      mvn -B verify  sonar:sonar
      -Dsonar.host.url=$SONAR_URL
      -Dsonar.login=$SONAR_LOGIN
      -Dsonar.analysis.mode=preview
      -Dsonar.gitlab.max_major_issues_gate=0
      -Dsonar.gitlab.max_minor_issues_gate=0
      -Dsonar.gitlab.max_info_issues_gate=0
      -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA
      -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
      -Dsonar.gitlab.project_id=$CI_PROJECT_ID


#
publish-sonar-result:
  stage: publish-sonar-result
  script:
    - >
      mvn -B verify sonar:sonar
      -Dsonar.host.url=$SONAR_URL
      -Dsonar.projectVersion=$CI_COMMIT_SHA
      -Dsonar.login=$SONAR_LOGIN
  only:
    refs:
      - master

set-code-check-baseline:
  stage: set-code-check-baseline
  variables:
    GIT_STRATEGY: clone
  script:
    - >
      mvn -B clean package verify sonar:sonar
      -Dsonar.host.url=$SONAR_URL
      -Dsonar.projectVersion=$CI_COMMIT_SHA
      -Dsonar.login=$SONAR_LOGIN
  when: manual

deploy-jar:
  stage: deploy-jar
  variables:
    GIT_STRATEGY: clone
  script:
    -  pwd
    - ls
    - mvn -B clean deploy
  when: manual

#开始构建镜像并上传至harbor
build-img:
  stage: build-img
  image: harbor.cloud.prolog.org/base-solution/drone-kaniko:v2
  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: "true"
  script:
    - PLUGIN_TAGS="${CI_COMMIT_SHA:0:5}"
    - pwd
    - ls -l
    - /kaniko/plugin.sh
  only:
    refs:
      - dev   #对应分支代码分支
  when: manual  #手动执行，注释后会自动在每次提交变更后执行

#发布流程
k8s-deploy:
  stage: k8s-deploy
  variables:
    PROJECT_PORT: 9816         #配置文件里面的端口需要和deployment容器端口一致 次端口只能在k8s内部访问
    PROJECT_NODEPORT: 39816     #nodeport 为该服务在k8s集群外访问端口
    #    PROJECT_NAME: plg-cs-eureka    # 服务名称，不宜过长
    PROJECT_NAME: ${CI_PROJECT_NAME} #启用变量配。
    PROJECT_NAMESPACE: test           # k8s集群内部的命名空间namespaces
    PROJECT_ACTIVE: k8s               # 项目对应的环境配置文件
    #    PROJECT_IMAGE: harbor.cloud.prolog.org/test/plg-cs-eureka:latest
    PROJECT_IMAGE: harbor.cloud.prolog.org/test/${CI_PROJECT_NAME}:latest #变量配置镜像
  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#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  #手动执行，注释后会自动在每次提交变更后执行
