Gitlab CI/CD K8S JAVA

# https://docs.gitlab.com/ee/development/cicd/templates.html
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Maven.gitlab-ci.yml
# For general lifecycle information see https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

#include:
#  - local: 'cicd/other-ci.yml'    #引入本地项目文件
#  - project: demo/java-demo       #包含来自另一个项目的文件
#    ref: master
#    file: '.gitlab-ci.yml'
#  - template: Auto-DevOps.gitlab-ci.yml   #使用官方提供的模板 https://gitlab.com/gitlab-org/gitlab/tree/#master/lib/gitlab/ci/templates
#  - remote: 'https://g.vimll.com:9888/root/helloworld/-/raw/master/.gitlab-ci.yml'   #使用远程文件#

#workflow:
#  rules:
#    - if: '$USERNAME == "Administrator"'
#      when: always
#    - when: never
#if: '$CI_PIPELINE_SOURCE == "merge_request_event"'	控制合并请求流水线何时运行。
#if: '$CI_PIPELINE_SOURCE == "push"'	控制分支流水线和标签流水线何时运行。
#if: $CI_COMMIT_TAG	控制标签流水线何时运行。
#if: $CI_COMMIT_BRANCH	控制分支流水线何时运行。

#image: d.vimll.com:9888/root/plulic/docker:20.10.16      ## 定义全局镜像
#services:     ## 工作期间运行的另一个Docker映像,并link到image关键字定义的Docker映像。这样,您就可以在构建期间访问服务映像.
#  - name: d.vimll.com:9888/root/plulic/docker:20.10.16-dind
#    alias: docker
variables:
  # `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
  MAVEN_OPTS: >-
    -Dhttps.protocols=TLSv1.2
    -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository
    -Dorg.slf4j.simpleLogger.showDateTime=true
    -Djava.awt.headless=true

  # As of Maven 3.3.0 instead of this you MAY define these options in `.mvn/maven.config` so the same config is used
  # when running from the command line.
  # As of Maven 3.6.1, the use of `--no-tranfer-progress` (or `-ntp`) suppresses download and upload messages. The use
  # of the `Slf4jMavenTransferListener` is no longer necessary.
  # `installAtEnd` and `deployAtEnd` are only effective with recent version of the corresponding plugins.
  MAVEN_CLI_OPTS: >-
    --batch-mode
    --errors
    --fail-at-end
    --show-version
    --no-transfer-progress
    -DinstallAtEnd=true
    -DdeployAtEnd=true

# This template uses the latest Maven 3 release, e.g., 3.8.6, and OpenJDK 8 (LTS)
# for verifying and deploying images
# Maven 3.8.x REQUIRES HTTPS repositories.
# See https://maven.apache.org/docs/3.8.1/release-notes.html#how-to-fix-when-i-get-a-http-repository-blocked for more.

  # Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled
  DOCKER_HOST: tcp://docker:2376
  DOCKER_TLS_CERTDIR: "/certs"
  DOCKER_TLS_VERIFY: 1
  DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"

# Cache downloaded dependencies and plugins between builds.
# To keep cache across branches add 'key: "$CI_JOB_NAME"'
# Be aware that `mvn deploy` will install the built jar into this repository. If you notice your cache size
# increasing, consider adding `-Dmaven.install.skip=true` to `MAVEN_OPTS` or in `.mvn/maven.config`

stages:
  - build
  - test
  - package
  - deploy
  - upload-jar

##编译阶段
java-build:
  # select the most appropriate image for your project
  image: d.vimll.com:9888/root/plulic/maven:3-openjdk-8
  stage: build
  # Cache downloaded dependencies and plugins between builds.
  # The key here separates one cache per branch/tag ($CI_COMMIT_REF_SLUG)
  cache:
    #key: "maven-$CI_COMMIT_REF_SLUG"
    key: "maven-helloword"
    paths:
      - .m2/repository
    #policy: pull     ##不下载缓存
  script:
    - mvn $MAVEN_CLI_OPTS package -DskipTests=true -s ci_settings.xml
  artifacts:
    name: "Maven artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    paths:
      - "**/target"
    expire_in: '30 mins'
  only:
    - develop
    - /^feature\/.*/
    - /^hotfix\/.*/
    - /^release\/.*/
  except:
    - tags

## 测试
unittest:      ##需要开启 sudo gitlab-rails console ;输入  Feature.enable(:junit_pipeline_view)  回车
  stage: test
  #  stage: .pre      ##.pre表示此作业在所有作业执行之前执行
  #  stage: .post     ##.post表示此作业在所有作业执行完成之后再执行
  tags:
    - k8s
  environment:
    name: test
    url: https://www.baidu.com
  script:
    - echo hahaha
    #- mvn test    ##测试执行 target/test-classes/ 目录下的程序,并生成测试报告。
    #- mvn cobertura:cobertura   ##生成测试覆盖率报告
  #artifacts:
  #  name: "$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG"
  #  when: on_success
  ##  expose_as: 'artifact 1'    ##匹配单个文件 paths精确到某个文件
  #  paths:
  #    - target/*.jar    
  #  reports:
  #    junit:
  #      - target/surefire-reports/TEST-*.xml
  #      - target/failsafe-reports/TEST-*.xml
  #    cobertura: target/site/cobertura/coverage.xml
  #coverage: '/Code coverage: \d+\.\d+/'
  #retry: 2
  retry:
    max: 2
    when:
      - script_failure
  allow_failure: true    ##允许失败
  ## when   on_success前面成功  on_failure前面失败  always总是执行  manual手动执行  delayed延迟执行
  #when: always
  when: delayed
  start_in: '2'     #延迟两秒
  timeout: 1h 10m
  #parallel: 2     ##并行作业实例数
  #rules:
  #  - if: $DELOY == "pro"   ## 如果DELOY=pro 则手动执行
  #    when: manual
  #  - if: '$CI_COMMIT_BRANCH == "develop"'
  #  - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  #    when: manual
  #    allow_failure: true
  #  - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  #  - if: $CI_PIPELINE_SOURCE == "schedule"
  #  - changes:   ##文件修改后触发
  #    - Dockerfile
  #    when: manual     
  #  - when: on_success    ## 如果上面规则不成立则执行此
  only:
    - develop
    - /^feature\/.*/
    - /^hotfix\/.*/
    - /^release\/.*/
  except:
    - tags
  needs:     ##并行任务时,关联任务执行完成后执行些任务,无需等待其他并行任务全部完成。默认限制10个最大作业数。
    - java-build
    #- job: "java-build"    ##制品下载 默认为true
    #  artifacts: true


##打包阶段
docker-build:
  image: d.vimll.com:9888/root/plulic/docker:20.10.16
  services:
    - name: d.vimll.com:9888/root/plulic/docker:20.10.16-dind
      alias: docker
  stage: package
  dependencies:      ##获取java-build阶段制品
    - java-build
  before_script:
    - echo $CI_REGISTRY
    - echo $CI_REGISTRY_USER
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY    
  script:
    - docker version
    - docker pull $CI_REGISTRY_IMAGE:latest || true
    - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG  --tag $CI_REGISTRY_IMAGE:latest .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
    - docker push $CI_REGISTRY_IMAGE:latest    
    #- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    #- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG .
    #- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    #- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
  only:
    - develop
    - /^feature\/.*/
    - /^hotfix\/.*/
    - /^release\/.*/

## 正式编译   
java-tags-build:
  # select the most appropriate image for your project
  image: d.vimll.com:9888/root/plulic/maven:3-openjdk-8
  stage: build
  # Cache downloaded dependencies and plugins between builds.
  # The key here separates one cache per branch/tag ($CI_COMMIT_REF_SLUG)
  cache:
    key: "maven-helloword"
    paths:
      - .m2/repository
  script:
    - mvn $MAVEN_CLI_OPTS package -DskipTests=true -s ci_settings.xml
  artifacts:
    name: "Maven artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    paths:
      - "**/target"
    expire_in: 30 mins
  only:
    - tags

## 正式打包
docker-tags-build:
  image: d.vimll.com:9888/root/plulic/docker:20.10.16
  services:
    - name: d.vimll.com:9888/root/plulic/docker:20.10.16-dind
      alias: docker
  stage: package
  allow_failure: true
  dependencies:
    - java-tags-build
  before_script:
    - echo $CI_REGISTRY
    - echo $CI_REGISTRY_USER
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY    
  script:
    - docker version
    - docker pull $CI_REGISTRY_IMAGE:latest || true
    - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG  --tag $CI_REGISTRY_IMAGE:latest .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
    - docker push $CI_REGISTRY_IMAGE:latest    
    #- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    #- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG .
    #- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    #- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
  only:
    - tags


## 正式发布
deploy-tags-k8s:
  image: d.vimll.com:9888/k8s-ops/containers/kubectl:1.22.17
  stage: deploy
  dependencies:
    - docker-tags-build
  before_script:
    - kubectl version
  script:
    - sed -ie "s/BUILD_NUMBER/$CI_COMMIT_SHA/g" deployment.yaml
    - sed -ie "s/APP_NAME/helloworld/g" deployment.yaml
    - sed -ie "s+IMAGE_NAME+$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG+g" deployment.yaml
    - cat deployment.yaml
    - kubectl apply -f deployment.yaml -n default
  only:
    - tags


  after_script:
    - echo "OK"

##上传jar包到私服
#maven-deploy:
#  stage: upload-jar
#  script: mvn $MAVEN_CLI_OPTS -am -pl $PROJECTS source:jar deploy
#  only:
#    - develop
#    - /^feature\/.*/
#    - /^hotfix\/.*/
#    - /^release\/.*/#