持续集成交付CICD:基于 GitLabCI 与 JenkinsCD 实现后端项目发布

发布时间:2023年12月18日

目录

一、实验

1. GitLabCI环境设置

2.优化GitLabCI共享库代码

3.JenkinsCD 发布后端项目

4.再次优化GitLabCI共享库代码

5.JenkinsCD 再次发布后端项目


一、实验

1. GitLabCI环境设置

(1)GitLab给后端项目添加CI配置路径

(2)项目添加runnner

(3)修改项目本地CI文件,避免运行远程流水线报错

(4)分支RELEASE-1.1.1 添加到受保护分支

(5)修改共享库代码

修改CI.yaml

.pipelineInit:
  tags:
    - build
  stage: .pre
  variables:
    GIT_CHECKOUT: "true"
  script:
    - ls -l

.cibuild:
    tags:
      - build
    stage: build
    script:
      - echo "${BUILD_SHELL}"
      - ${BUILD_SHELL}
    artifacts:
      paths:
        - ${ARTIFACT_PATH}

.citest:
  tags:
    - build
  stage: test
  script:
    - echo "${TEST_SHELL}"
    - ${TEST_SHELL}
    # artifacts:
    # reports:
    #   - junit: ${TEST_REPORTS}

.sonarscan:
  tags: 
    - build
  stage: sonarscan
  script: 
    |-
      sonar-scanner -Dsonar.host.url=http://192.168.204.8:9000 \
        -Dsonar.projectKey=${CI_PROJECT_NAME}\
        -Dsonar.projectName=${CI_PROJECT_NAME} \
        -Dsonar.projectVersion=${CI_COMMIT_SHA} \
        -Dsonar.login=${SONAR_AUTH_TOKEN} \
        -Dsonar.ws.timeout=30 \
        -Dsonar.projectDescription=${CI_PROJECT_TITLE} \
        -Dsonar.links.homepage=${CI_PROJECT_URL} \
        -Dsonar.links.ci=${CI_PIPELINE_URL} \
        -Dsonar.sources=src \
        -Dsonar.sourceEncoding=UTF-8 \
        -Dsonar.java.binaries=target/classes \
        -Dsonar.java.test.binaries=target/test-classes \
        -Dsonar.java.surefire.report=target/surefire-reports \
        -Dsonar.core.codeCoveragePlugin=jacoco \
        -Dsonar.jacoco.reportPaths=target/jacoco.exec

.pushartifact:
  tags:
    - build
  stage: pushartifact
  script:
    |-
      if [[ ${PROJECT_TYPE} == "java" ]];then
          pkgName=`ls target/ | grep -e "^maven.*jar$"`
          cd target/
          mv ${pkgName} ${CI_PROJECT_NAME}-${CI_COMMIT_SHA}.jar
          curl -X POST "http://192.168.204.13:8081/service/rest/v1/components?repository=${CI_PROJECT_NAMESPACE}" \
          -H "accept: application/json" \
          -H "Content-Type: multipart/form-data" \
          -F "raw.directory=/${CI_PROJECT_NAMPESPACE}/${CI_PROJECT_NAME}/${{CI_COMMIT_SHA}}/" \
          -F "raw.asset1=@${CI_PROJECT_NAME}-${VERSION}.jar;type=application/java-archive" \
          -F "raw.asset1.filename=${CI_PROJECT_NAME}-${{CI_COMMIT_SHA}}.jar" -u admin:Admin123
      else
          echo "PROJECT_TYPE ERROR [java]"
      fi

修改Nexus仓库地址。

修改前:

修改后:

(6)手动运行Pipeline

(7) 成功

(8)Nexus查看制品(存在目录不统一问题)

2.优化GitLabCI共享库代码

(1)?修改CI.yaml

.pipelineInit:
  tags:
    - build
  stage: .pre
  variables:
    GIT_CHECKOUT: "true"
  script:
    - ls -l

.cibuild:
    tags:
      - build
    stage: build
    script:
      - echo "${BUILD_SHELL}"
      - ${BUILD_SHELL}
    artifacts:
      paths:
        - ${ARTIFACT_PATH}

.citest:
  tags:
    - build
  stage: test
  script:
    - echo "${TEST_SHELL}"
    - ${TEST_SHELL}
    # artifacts:
    # reports:
    #   - junit: ${TEST_REPORTS}

.sonarscan:
  tags: 
    - build
  stage: sonarscan
  script: 
    |-
      sonar-scanner -Dsonar.host.url=http://192.168.204.8:9000 \
        -Dsonar.projectKey=${CI_PROJECT_NAME}\
        -Dsonar.projectName=${CI_PROJECT_NAME} \
        -Dsonar.projectVersion=${CI_COMMIT_SHA} \
        -Dsonar.login=${SONAR_AUTH_TOKEN} \
        -Dsonar.ws.timeout=30 \
        -Dsonar.projectDescription=${CI_PROJECT_TITLE} \
        -Dsonar.links.homepage=${CI_PROJECT_URL} \
        -Dsonar.links.ci=${CI_PIPELINE_URL} \
        -Dsonar.sources=src \
        -Dsonar.sourceEncoding=UTF-8 \
        -Dsonar.java.binaries=target/classes \
        -Dsonar.java.test.binaries=target/test-classes \
        -Dsonar.java.surefire.report=target/surefire-reports \
        -Dsonar.core.codeCoveragePlugin=jacoco \
        -Dsonar.jacoco.reportPaths=target/jacoco.exec

.pushartifact:
  tags:
    - build
  stage: pushartifact
  script:
    |-
      if [[ ${PROJECT_TYPE} == "java" ]];then
          pkgName=`ls target/ | grep -e "^maven.*jar$"`
          cd target/
          mv ${pkgName} ${CI_PROJECT_NAME}-${CI_COMMIT_SHA}.jar
          curl -X POST "http://192.168.204.13:8081/service/rest/v1/components?repository=${CI_PROJECT_NAMESPACE}" \
          -H "accept: application/json" \
          -H "Content-Type: multipart/form-data" \
          -F "raw.directory=/${CI_PROJECT_NAME}/${{CI_COMMIT_SHA}}/" \
          -F "raw.asset1=@${CI_PROJECT_NAME}-${VERSION}.jar;type=application/java-archive" \
          -F "raw.asset1.filename=${CI_PROJECT_NAME}-${{CI_COMMIT_SHA}}.jar" -u admin:Admin123
      else
          echo "PROJECT_TYPE ERROR [java]"
      fi

修改Nexus上传目录。

修改前:

修改后:

(2)手动运行Pipeline

(3)成功

(4)Nexus查看制品(存在版本号不统一问题)

3.JenkinsCD 发布后端项目

(1)查看ansible被控端状态

(2)拿到上一次GitLabCI上传到Nexus的版本号,在Jenkins上手动构建流水线

(3)Blue Ocean查看

4.再次优化GitLabCI共享库代码

(1?)修改CI.yaml

.pipelineInit:
  tags:
    - build
  stage: .pre
  variables:
    GIT_CHECKOUT: "true"
  script:
    - ls -l

.cibuild:
    tags:
      - build
    stage: build
    script:
      - echo "${BUILD_SHELL}"
      - ${BUILD_SHELL}
    artifacts:
      paths:
        - ${ARTIFACT_PATH}

.citest:
  tags:
    - build
  stage: test
  script:
    - echo "${TEST_SHELL}"
    - ${TEST_SHELL}
    # artifacts:
    # reports:
    #   - junit: ${TEST_REPORTS}

.sonarscan:
  tags: 
    - build
  stage: sonarscan
  script: 
    |-
      sonar-scanner -Dsonar.host.url=http://192.168.204.8:9000 \
        -Dsonar.projectKey=${CI_PROJECT_NAME}\
        -Dsonar.projectName=${CI_PROJECT_NAME} \
        -Dsonar.projectVersion=${CI_COMMIT_SHA} \
        -Dsonar.login=${SONAR_AUTH_TOKEN} \
        -Dsonar.ws.timeout=30 \
        -Dsonar.projectDescription=${CI_PROJECT_TITLE} \
        -Dsonar.links.homepage=${CI_PROJECT_URL} \
        -Dsonar.links.ci=${CI_PIPELINE_URL} \
        -Dsonar.sources=src \
        -Dsonar.sourceEncoding=UTF-8 \
        -Dsonar.java.binaries=target/classes \
        -Dsonar.java.test.binaries=target/test-classes \
        -Dsonar.java.surefire.report=target/surefire-reports \
        -Dsonar.core.codeCoveragePlugin=jacoco \
        -Dsonar.jacoco.reportPaths=target/jacoco.exec

.pushartifact:
  tags:
    - build
  stage: pushartifact
  script:
    |-
      if [[ ${PROJECT_TYPE} == "java" ]];then
          VERSION=`echo ${CI_COMMIT_BRANCH} | sed 's/RELEASE-//g'`
          pkgName=`ls target/ | grep -e "^maven.*jar$"`
          cd target/
          mv ${pkgName} ${CI_PROJECT_NAME}-${VERSION}.jar
          curl -X POST "http://192.168.204.13:8081/service/rest/v1/components?repository=${CI_PROJECT_NAMESPACE}" \
          -H "accept: application/json" \
          -H "Content-Type: multipart/form-data" \
          -F "raw.directory=/${CI_PROJECT_NAME}/${VERSION}/" \
          -F "raw.asset1=@${CI_PROJECT_NAME}-${VERSION}.jar;type=application/java-archive" \
          -F "raw.asset1.filename=${CI_PROJECT_NAME}-${VERSION}.jar" -u admin:Admin123
      else
          echo "PROJECT_TYPE ERROR [java]"
      fi

                  

修改分支变量。

修改前:

修改后:

(2)Nexus删除后端项目1.1.1分支的制品

已删除

(3)GitLab手动运行Pipeline

(4) 成功

(5)Nexus查看制品

5.JenkinsCD 再次发布后端项目

(1)Jenkins上手动构建流水线

(2)Blue Ocean 查看

文章来源:https://blog.csdn.net/cronaldo91/article/details/135041994
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。