持续集成交付CICD:GitLabCI上传Nexus制品

目录

一、实验

1.GitLabCI上传Nexus制品

2.优化GitLabCI(引用系统变量)

3.添加if条件判断项目类型

4.优化GitLabCI(模板类)

二、问题

1.GitLabCI获取jar文件失败

[2. GitLabCI获取流水线项目命名空间失败](#2. GitLabCI获取流水线项目命名空间失败)

[3.GItLab Package如何实现上传至本地仓库](#3.GItLab Package如何实现上传至本地仓库)


一、实验

1.GitLabCI上传Nexus制品

(1)进入共享库

(2)修改流水线,提交到master

bash 复制代码
include:
 - project: 'devops03/devops03-gitlabci-lib'
   ref: master
   file: "/jobs/CI.yaml"




workflow:
  rules:
    #新建分支永远不执行
    - if: $CI_PIPELINE_SOURCE == "web"      #允许手动触发
      when: always
    - if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
      when: never
    # 其他情况永远执行
    - when: always

#取消每个阶段自动下载代码,即全局关闭作业代码下载
variables:
  GIT_CHECKOUT: "false"
  BUILD_SHELL: "mvn clean package -DskipTests -s settings.xml"   ## 构建命令
  TEST_SHELL: "mvn test -s settings.xml"                         ## 测试命令
  ARTIFACT_PATH: "target/*.jar"                                  ## 制品lujing
  TEST_REPORTS: "target/surefire-reports/TEST-*.xml"             ## 测试报告

stages:
  - build
  - test
  - sonarscan
  - pushartifact


pipelineInit:
  extends: 
    - .pipelineInit


cibuild:
 extends:
 - .cibuild


citest:
 extends:
 - .citest

sonarscan:
 extends:
 - .sonarscan

pushartifact:
  tags:
    - build
  stage: pushartifact
  script:
    |-
      pkgName=`ls target/ | grep -e "^maven.*jar$"`
      curl -X POST "http://192.168.204.13:8081/service/rest/v1/components?repository=mylocalrepo" \
      -H "accept: application/json" \
      -H "Content-Type: multipart/form-data" \
      -F "raw.directory=/tmp" \
      -F "raw.asset1=@target/${pkgName};type=application/java-archive" \
      -F "raw.asset1.filename=${pkgName}" -u admin:Admin123

(2) maven项目开始运行流水线

(3)运行

(4)成功

(5)查看日志

(6)Nexus查看

2.优化GitLabCI(引用系统变量)

(1) 优化流水线,提交到master

bash 复制代码
include:
 - project: 'devops03/devops03-gitlabci-lib'
   ref: master
   file: "/jobs/CI.yaml"




workflow:
  rules:
    #新建分支永远不执行
    - if: $CI_PIPELINE_SOURCE == "web"      #允许手动触发
      when: always
    - if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
      when: never
    # 其他情况永远执行
    - when: always

#取消每个阶段自动下载代码,即全局关闭作业代码下载
variables:
  GIT_CHECKOUT: "false"
  BUILD_SHELL: "mvn clean package -DskipTests -s settings.xml"   ## 构建命令
  TEST_SHELL: "mvn test -s settings.xml"                         ## 测试命令
  ARTIFACT_PATH: "target/*.jar"                                  ## 制品lujing
  TEST_REPORTS: "target/surefire-reports/TEST-*.xml"             ## 测试报告

stages:
  - build
  - test
  - sonarscan
  - pushartifact


pipelineInit:
  extends: 
    - .pipelineInit


cibuild:
 extends:
 - .cibuild


citest:
 extends:
 - .citest

sonarscan:
 extends:
 - .sonarscan

pushartifact:
  tags:
    - build
  stage: pushartifact
  script:
    |-
      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=mylocalrepo" \
      -H "accept: application/json" \
      -H "Content-Type: multipart/form-data" \
      -F "raw.directory=/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}/${CI_COMMIT_SHA}/" \
      -F "raw.asset1=@${CI_PROJECT_NAME}-${CI_COMMIT_SHA}.jar;type=application/java-archive" \
      -F "raw.asset1.filename=${CI_PROJECT_NAME}-${CI_COMMIT_SHA}.jar" -u admin:Admin123

(2) maven项目开始运行流水线

(3)运行

(4) 成功

(5)查看日志

(6)Nexus查看

3.添加if条件判断项目类型

(1)修改流水线,提交到master

bash 复制代码
include:
 - project: 'devops03/devops03-gitlabci-lib'
   ref: master
   file: "/jobs/CI.yaml"




workflow:
  rules:
    #新建分支永远不执行
    - if: $CI_PIPELINE_SOURCE == "web"      #允许手动触发
      when: always
    - if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
      when: never
    # 其他情况永远执行
    - when: always

#取消每个阶段自动下载代码,即全局关闭作业代码下载
variables:
  GIT_CHECKOUT: "false"  ## 全局关闭作业代码下载
  PROJECT_TYPE: "java"   ## 定义项目类型
  BUILD_SHELL: "mvn clean package -DskipTests -s settings.xml"   ## 构建命令
  TEST_SHELL: "mvn test -s settings.xml"                         ## 测试命令
  ARTIFACT_PATH: "target/*.jar"                                  ## 制品lujing
  TEST_REPORTS: "target/surefire-reports/TEST-*.xml"             ## 测试报告

stages:
  - build
  - test
  - sonarscan
  - pushartifact


pipelineInit:
  extends: 
    - .pipelineInit


cibuild:
 extends:
 - .cibuild


citest:
 extends:
 - .citest

sonarscan:
 extends:
 - .sonarscan

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=mylocalrepo" \
          -H "accept: application/json" \
          -H "Content-Type: multipart/form-data" \
          -F "raw.directory=/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}/${CI_COMMIT_SHA}/" \
          -F "raw.asset1=@${CI_PROJECT_NAME}-${CI_COMMIT_SHA}.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

(2) maven项目开始运行流水线

(3)运行

(4) 成功

(5)查看日志

(6)Nexus查看

4.优化GitLabCI(模板类)

(1)修改流水线与新增模板类,提交到master

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=mylocalrepo" \
          -H "accept: application/json" \
          -H "Content-Type: multipart/form-data" \
          -F "raw.directory=/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}/${CI_COMMIT_SHA}/" \
          -F "raw.asset1=@${CI_PROJECT_NAME}-${CI_COMMIT_SHA}.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

gitlab-ci.yaml

bash 复制代码
​
include:
 - project: 'devops03/devops03-gitlabci-lib'
   ref: master
   file: "/jobs/CI.yaml"




workflow:
  rules:
    #新建分支永远不执行
    - if: $CI_PIPELINE_SOURCE == "web"      #允许手动触发
      when: always
    - if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
      when: never
    # 其他情况永远执行
    - when: always

#取消每个阶段自动下载代码,即全局关闭作业代码下载
variables:
  GIT_CHECKOUT: "false"  ## 全局关闭作业代码下载
  PROJECT_TYPE: "java"   ## 定义项目类型
  BUILD_SHELL: "mvn clean package -DskipTests -s settings.xml"   ## 构建命令
  TEST_SHELL: "mvn test -s settings.xml"                         ## 测试命令
  ARTIFACT_PATH: "target/*.jar"                                  ## 制品lujing
  TEST_REPORTS: "target/surefire-reports/TEST-*.xml"             ## 测试报告

stages:
  - build
  - test
  - sonarscan
  - pushartifact


pipelineInit:
  extends: 
    - .pipelineInit


cibuild:
 extends:
 - .cibuild


citest:
 extends:
 - .citest

sonarscan:
 extends:
 - .sonarscan

pushartifact:
 extends:
 - .pushartifact

​

(2) maven项目开始运行流水线

(3)运行

(4) 成功

(5)查看日志

(6)Nexus查看

二、问题

1.GitLabCI获取jar文件失败

(1)报错

(2)原因分析

正则表达式输入有误

(3)解决方法

修改正则表达式。

修改前:

bash 复制代码
pkgName=`ls target/ | grep -e "jar$"`

修改后:

bash 复制代码
pkgName=`ls target/ | grep -e "^maven.*jar$"`

成功:

2. GitLabCI获取流水线项目命名空间失败

(1)报错

(2)原因分析

变量错误

(3)解决方法

修改引用流水线变量。

修改前:

bash 复制代码
-F "raw.directory=/${CI_PROJECT_ROOT_NAMESPACE}/${CI_PROJECT_NAME}/${CI_COMMIT_SHA}/" \

修改后:

bash 复制代码
-F "raw.directory=/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}/${CI_COMMIT_SHA}/" \

成功:

3.GItLab Package如何实现上传至本地仓库

(1)版本

GItLab Package 需要GitLab 13.5及以上版本实现

(2)curl命令上传到GItLab Package

bash 复制代码
curl --header "PRIVATE-TOKEN: 4b54764fd071175387e94385a0d6c5" \
     --upload-file sonar-gitlab-plugin-4.1.0-SNAPSHOT.jar \
     "http://192.168.204.8/api/v4/projects/14/packages/generic/devops03-maven5-service/0.0.1/sonar-gitlab-plugin-4.1.0-SNAPSHOT.jar?status=default"

(3)流水线上传到GItLab Package

CI.yaml

bash 复制代码
.pushartforgitlab:
  tags:
    - build
  stage: pushartifact  
  script:
    |- 
      if [[ ${PROJECT_TYPE} == "java" ]];then
          newPkgName=${CI_PROJECT_NAME}-${CI_COMMIT_SHA}.jar
          pkgName=`ls target/ | grep -e "jar$"`
          cd target/
          mv ${pkgName} ${newPkgName}
          curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
          --upload-file ${newPkgName} \
          "http://192.168.204.8/api/v4/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}/${CI_COMMIT_SHA}/${newPkgName}?status=default"
      else
          echo "PROJECT_TYPE ERROR [java]"
      fi

gitlab-ci.yaml

bash 复制代码
pushartifact:
 extends:
 - .pushartforgitlab
相关推荐
舞动CPU4 小时前
linux c/c++最高效的计时方法
linux·运维·服务器
皮锤打乌龟5 小时前
(干货)Jenkins使用kubernetes插件连接k8s的认证方式
运维·kubernetes·jenkins
钰@5 小时前
小程序开发者工具的network选项卡中有某域名的接口请求,但是在charles中抓不到该接口
运维·服务器·小程序
wanhengwangluo5 小时前
云服务器和物理服务器的区别有哪些?
运维·服务器
秦jh_6 小时前
【Linux】多线程(概念,控制)
linux·运维·前端
yaosheng_VALVE7 小时前
稀硫酸介质中 V 型球阀的材质选择与选型要点-耀圣
运维·spring cloud·自动化·intellij-idea·材质·1024程序员节
看山还是山,看水还是。7 小时前
Redis 配置
运维·数据库·redis·安全·缓存·测试覆盖率
扣得君7 小时前
C++20 Coroutine Echo Server
运维·服务器·c++20
keep__go8 小时前
Linux 批量配置互信
linux·运维·服务器·数据库·shell
矛取矛求8 小时前
Linux中给普通账户一次性提权
linux·运维·服务器