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

目录

一、实验

[1. GitLabCI环境设置](#1. GitLabCI环境设置)

2.优化GitLabCI共享库代码

[3.JenkinsCD 发布后端项目](#3.JenkinsCD 发布后端项目)

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

[5.JenkinsCD 再次发布后端项目](#5.JenkinsCD 再次发布后端项目)


一、实验

1. GitLabCI环境设置

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

(2)项目添加runnner

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

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

(5)修改共享库代码

修改CI.yaml

bash 复制代码
.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

bash 复制代码
.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

bash 复制代码
.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 查看

相关推荐
青靴2 天前
轻量级日志监控与告警系统(二,下):CI/CD 具体部署实战,一行推送实现秒级更新
ci/cd
要站在顶端2 天前
基于 curl 实现 Jenkins 上传制品到 JFrog Artifactory
运维·ci/cd·jenkins
4***99743 天前
DevOps在云原生中的CI/CD流水线
ci/cd·云原生·devops
西部森林牧歌3 天前
Arbess从基础到高阶(14) - 使用Arbess+GitLab+SonarQube实现代码扫描完成后自动化部署
ci/cd·gitlab·sonarqube·arbess·tiklab devops
青靴3 天前
虚拟机上实现最简 CI/CD
ci/cd·devops
青靴4 天前
轻量级 CI/CD:Git Hooks 自动部署 Node.js 应用(CICD-demo)
git·ci/cd·node.js
青靴4 天前
轻量级 CI/CD 实战(四):本地开发钉钉告警 → 自动部署云服务器 Kafka 消费者容器
ci/cd·docker·钉钉
奔跑吧邓邓子4 天前
Jenkins自动化持续集成:从入门到实战
ci/cd·自动化·实战·jenkins·自动化持续集成
青靴5 天前
Git Hooks 实现 CI/CD 进阶实践 -- 根据实际需求添加功能
git·elasticsearch·ci/cd
DreamLife☼5 天前
Docker-CI/CD:集成 GitHub Actions 实现自动化构建与部署
ci/cd·docker·自动化部署·github actions·镜像构建·trivy 扫描·compose 部署