目录
一、实验
1.GitLabCI上传Nexus制品
2.优化GitLabCI(引用系统变量)
3.添加if条件判断项目类型
4.优化GitLabCI(模板类)
二、问题
1.GitLabCI获取jar文件失败
2. GitLabCI获取流水线项目命名空间失败
3.GItLab Package如何实现上传至本地仓库
(1)进入共享库
(2)修改流水线,提交到master
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" ## 制品路径
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查看
(1) 优化流水线,提交到master
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" ## 制品路径
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查看
(1)修改流水线,提交到master
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" ## 制品路径
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查看
(1)修改流水线与新增模板类,提交到master
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=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
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" ## 制品路径
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)报错
(2)原因分析
正则表达式输入有误
(3)解决方法
修改正则表达式。
修改前:
pkgName=`ls target/ | grep -e "jar$"`
修改后:
pkgName=`ls target/ | grep -e "^maven.*jar$"`
成功:
(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}/" \
成功:
(1)版本
GItLab Package 需要GitLab 13.5及以上版本实现
(2)curl命令上传到GItLab Package
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
.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
pushartifact:
extends:
- .pushartforgitlab