# 010-打通Springboot应用编译构建
# 控制台地址
http://kenaito-gitlab.odboy.local:20080/admin (opens new window)
# 配置默认初始分支 master
# 开启导入功能
# 导入测试应用kenaito[弃用]
https://gitea.odboy.cn/odboy/kenaito.git (opens new window)
# 导入测试应用kenaito-devops
https://gitea.odboy.cn/odboy/kenaito-devops.git (opens new window)
# 导入完毕后,从main新建master分支,编辑仓库规则
http://kenaito-gitlab.odboy.local:20080/root/kenaito-devops/-/settings/repository (opens new window)
# kenaito-devops项目根目录新增 .gitlab-ci.yml 文件
- mvn参数参考 (opens new window)
- -pl 参数:手动选择需要构建的项目,项目以逗号分隔
- -am 参数:构建指定模块时,同时构建指定模块快依赖的其他依赖
variables:
MAVEN_CLI_OPTS: "-Dfile.encoding=UTF-8 -s /usr/local/maven/conf/settings.xml"
stages:
- buildJar
buildJar:
stage: buildJar
variables:
projectdir: $CI_PROJECT_DIR
projectid: $CI_PROJECT_ID
projectname: $CI_PROJECT_NAME
appname: "kenaito-sso-service"
jobid: $CI_JOB_ID
pipelineid: $CI_PIPELINE_ID
commitid: $CI_COMMIT_SHA
branchname: $CI_COMMIT_REF_NAME
versioncode: "202411131900"
envLabel: "online"
script:
- mvn clean package -DskipTests -pl $appname -am
- rm -rf /home/gitlab-runner/springboot/$appname/$versioncode
- mkdir -p /home/gitlab-runner/springboot/$appname/$versioncode
- cp $appname/target/$appname.jar /home/gitlab-runner/springboot/$appname/$versioncode/$appname.jar
- cp Dockerfile"_"$envLabel /home/gitlab-runner/springboot/$appname/$versioncode/
- cd /home/gitlab-runner/springboot/$appname/$versioncode/
- tar zcvf $appname.tar.gz $appname.jar
- rm -f $appname.jar
- ls -al
- echo imageName=kenaito-register.odboy.local:15000/springboot/$appname:$envLabel"_"$versioncode
- buildah build-using-dockerfile -f Dockerfile_$envLabel -t kenaito-register.odboy.local:15000/springboot/$appname:$envLabel"_"$versioncode --build-arg APP_NAME=$appname --build-arg APP_ENV=$envLabel ./
- buildah push --tls-verify=false kenaito-register.odboy.local:15000/springboot/$appname:$envLabel"_"$versioncode
- echo projectdir=$projectdir
- echo projectid=$projectid
- echo projectname=$projectname
- echo jobid=$jobid
- echo pipelineid=$pipelineid
- echo commitid=$commitid
- echo branchname=$branchname
- echo versioncode=$versioncode
- echo env=$envLabel
- echo pwd=${PWD}
tags:
- springboot
only:
- master
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# kenaito-devops项目根目录新增 Dockerfile_[envLabel] 文件
# Dockerfile_daily
# 基础镜像
FROM registry.cn-shanghai.aliyuncs.com/odboy/ops:runtime-jdk11
# 这里替换成你自己的应用名
ARG APP_NAME=kenaito-sso-service
# 多环境情况下,是哪个环境(比如有日常daily、预发stage、生产online)
ARG APP_ENV=daily
# 以下内容不要动.
ENV APP_NAME=$APP_NAME
ENV APP_ENV=$APP_ENV
VOLUME /home/admin/$APP_NAME/logs/status
VOLUME /home/admin/$APP_NAME/logs/biz
VOLUME /home/admin/$APP_NAME/dumpfile
ADD ${APP_NAME}.tar.gz /home/admin/${APP_NAME}/
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# Dockerfile_stage
# 基础镜像
FROM registry.cn-shanghai.aliyuncs.com/odboy/ops:runtime-jdk11
# 这里替换成你自己的应用名
ARG APP_NAME=kenaito-sso-service
# 多环境情况下,是哪个环境(比如有日常daily、预发stage、生产online)
ARG APP_ENV=stage
# 以下内容不要动.
ENV APP_NAME=$APP_NAME
ENV APP_ENV=$APP_ENV
VOLUME /home/admin/$APP_NAME/logs/status
VOLUME /home/admin/$APP_NAME/logs/biz
VOLUME /home/admin/$APP_NAME/dumpfile
ADD ${APP_NAME}.tar.gz /home/admin/${APP_NAME}/
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# Dockerfile_online
# 基础镜像
FROM registry.cn-shanghai.aliyuncs.com/odboy/ops:runtime-jdk11
# 这里替换成你自己的应用名
ARG APP_NAME=kenaito-sso-service
# 多环境情况下,是哪个环境(比如有日常daily、预发stage、生产online)
ARG APP_ENV=online
# 以下内容不要动.
ENV APP_NAME=$APP_NAME
ENV APP_ENV=$APP_ENV
VOLUME /home/admin/$APP_NAME/logs/status
VOLUME /home/admin/$APP_NAME/logs/biz
VOLUME /home/admin/$APP_NAME/dumpfile
ADD ${APP_NAME}.tar.gz /home/admin/${APP_NAME}/
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 效果如下所示
# 要使用当前 .gitlab-ci.yml 文件,还需配合修改对应发布模块 pom.xml 文件,添加 标签。例如:测试用 .gitlab-ci.yml 部署的是 kenaito-sso-service 模块
<!-- 打包 -->
<build>
<finalName>kenaito-sso-service</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 跳过单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# CI Pipeline Test
- 流水线 http://kenaito-gitlab.odboy.local:20080/root/kenaito-devops/-/pipelines (opens new window)
- Job http://kenaito-gitlab.odboy.local:20080/root/kenaito-devops/-/jobs (opens new window)
# 正常运行中的任务
# 验证推送成功的镜像
- repository list http://kenaito-register.odboy.local:15000/v2/_catalog (opens new window)
- springboot/kenaito-sso-service tag list http://kenaito-register.odboy.local:15000/v2/springboot/kenaito-sso-service/tags/list (opens new window)
# 实践过程中出现的异常
# 1、gitlab-runner 首次clone项目成功,二次pull项目失败
Running with gitlab-runner 17.5.2 (c6eae8d7)
on jdk21maven3 t1_zsftUx,system ID: s_d2b0ae649551
Preparing the "shell" executor
00:00
Using Shell (bash) executor...
Preparing environment
00:00
Running on localhost.localdomain...
Getting source from Git repository
00:00
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /home/gitlab-runner/builds/t1_zsftUx/0/root/kenaito/.git/
fatal: git fetch-pack: expected shallow list
fatal: The remote end hung up unexpectedly
ERROR: Job failed: exit status 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 1、解决方案
好家伙,我直接一个尼玛好家伙,原来是新版的不支持旧git,也就是2.0以下的。
官方Issues https://gitlab.com/gitlab-org/gitlab-foss/-/issues/60466
手动将git 版本从 1.8 升级到 2.43
1
2
3
4
5
2
3
4
5
# 升级命令
yum remove git -y
#wget https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
#tar -xzvf git-2.9.5.tar.gz
#cd git-2.9.5
# 安装依赖
sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel -y
#sudo yum install gcc perl-ExtUtils-MakeMaker -y
# 编译安装
#sudo make prefix=/usr/local all
#sudo make prefix=/usr/local install
[root@localhost git-2.9.5]# git version
git version 2.43.0
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12