本文主要是介绍持续集成交付CICD:GitLabCI上传Nexus制品,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
一、实验
1.GitLabCI上传Nexus制品
2.优化GitLabCI(引用系统变量)
3.添加if条件判断项目类型
4.优化GitLabCI(模板类)
二、问题
1.GitLabCI获取jar文件失败
2. GitLabCI获取流水线项目命名空间失败
一、实验
1.GitLabCI上传Nexus制品
(1)进入共享库
(2)修改流水线,提交到master
include:- project: 'devops03/devops03-gitlabci-lib'ref: masterfile: "/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" ## 制品lujingTEST_REPORTS: "target/surefire-reports/TEST-*.xml" ## 测试报告stages:- build- test- sonarscan- pushartifactpipelineInit:extends: - .pipelineInitcibuild:extends:- .cibuildcitest:extends:- .citestsonarscan:extends:- .sonarscanpushartifact:tags:- buildstage: pushartifactscript:|-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
include:- project: 'devops03/devops03-gitlabci-lib'ref: masterfile: "/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" ## 制品lujingTEST_REPORTS: "target/surefire-reports/TEST-*.xml" ## 测试报告stages:- build- test- sonarscan- pushartifactpipelineInit:extends: - .pipelineInitcibuild:extends:- .cibuildcitest:extends:- .citestsonarscan:extends:- .sonarscanpushartifact:tags:- buildstage: pushartifactscript:|-pkgName=`ls target/ | grep -e "^maven.*jar$"`cd target/mv ${pkgName} ${CI_PROJECT_NAME}-${CI_COMMIT_SHA}.jarcurl -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
include:- project: 'devops03/devops03-gitlabci-lib'ref: masterfile: "/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" ## 制品lujingTEST_REPORTS: "target/surefire-reports/TEST-*.xml" ## 测试报告stages:- build- test- sonarscan- pushartifactpipelineInit:extends: - .pipelineInitcibuild:extends:- .cibuildcitest:extends:- .citestsonarscan:extends:- .sonarscanpushartifact:tags:- buildstage: pushartifactscript:|-if [[ ${PROJECT_TYPE} == "java" ]];thenpkgName=`ls target/ | grep -e "^maven.*jar$"`cd target/mv ${pkgName} ${CI_PROJECT_NAME}-${CI_COMMIT_SHA}.jarcurl -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:Admin123elseecho "PROJECT_TYPE ERROR [java]"fi
(2) maven项目开始运行流水线
(3)运行
(4) 成功
(5)查看日志
(6)Nexus查看
4.优化GitLabCI(模板类)
(1)修改流水线与新增模板类,提交到master
CI.yaml
.pipelineInit:tags:- buildstage: .prevariables:GIT_CHECKOUT: "true"script:- ls -l.cibuild:tags:- buildstage: buildscript:- echo "${BUILD_SHELL}"- ${BUILD_SHELL}artifacts:paths:- ${ARTIFACT_PATH}.citest:tags:- buildstage: testscript:- echo "${TEST_SHELL}"- ${TEST_SHELL}# artifacts:# reports:# - junit: ${TEST_REPORTS}.sonarscan:tags: - buildstage: sonarscanscript: |-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:- buildstage: pushartifactscript:|-if [[ ${PROJECT_TYPE} == "java" ]];thenpkgName=`ls target/ | grep -e "^maven.*jar$"`cd target/mv ${pkgName} ${CI_PROJECT_NAME}-${CI_COMMIT_SHA}.jarcurl -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:Admin123elseecho "PROJECT_TYPE ERROR [java]"fi
gitlab-ci.yaml
include:- project: 'devops03/devops03-gitlabci-lib'ref: masterfile: "/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" ## 制品lujingTEST_REPORTS: "target/surefire-reports/TEST-*.xml" ## 测试报告stages:- build- test- sonarscan- pushartifactpipelineInit:extends: - .pipelineInitcibuild:extends:- .cibuildcitest:extends:- .citestsonarscan:extends:- .sonarscanpushartifact:extends:- .pushartifact
(2) maven项目开始运行流水线
(3)运行
(4) 成功
(5)查看日志
(6)Nexus查看
二、问题
1.GitLabCI获取jar文件失败
(1)报错
(2)原因分析
正则表达式输入有误
(3)解决方法
修改正则表达式。
修改前:
pkgName=`ls target/ | grep -e "jar$"`
修改后:
pkgName=`ls target/ | grep -e "^maven.*jar$"`
成功:
2. GitLabCI获取流水线项目命名空间失败
(1)报错
(2)原因分析
变量错误
(3)解决方法
修改引用流水线变量。
修改前:
-F "raw.directory=/${CI_PROJECT_ROOT_NAMESPACE}/${CI_PROJECT_NAME}/${CI_COMMIT_SHA}/" \
修改后:
-F "raw.directory=/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}/${CI_COMMIT_SHA}/" \
成功:
这篇关于持续集成交付CICD:GitLabCI上传Nexus制品的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!