实践003-Gitlab CICD编译构建

文章目录

后端Java编译

后端Java项目编译jar包

直接使用流水线进行快速编译。

shell 复制代码
[root@gitclient apiserver]# vim .gitlab-ci.yml
stages:
  - compile

compile:
  stage: compile
  image: maven:3.8.5-openjdk-17
  script:
    - mvn clean
    - mvn compile
    - mvn package -Dmaven.test.skip=true
    - ls target
  only:
    - main
  tags:
    - study-runner

提交流水线:

shell 复制代码
[root@gitclient apiserver]# git add .
[root@gitclient apiserver]# git commit -m "Compile Java to jar"
[root@gitclient apiserver]# git push origin main

查看编译结果,可知以成功编译出jar包。

此编译 jar 包后续需要封装为容器镜像,因此需要将编译的结果作为产物进行共享。

修改流水线如下后,重新提交流水线:

shell 复制代码
[root@gitclient apiserver]# vim .gitlab-ci.yml
stages:
  - compile

compile:
  stage: compile
  image: uhub.service.ucloud.cn/imxhy/maven:3.8.5-openjdk-17
  artifacts:
    paths:
      - target/apiservice-0.0.1-SNAPSHOT.jar
  script:
    - mvn clean
    - mvn compile
    - mvn package -Dmaven.test.skip=true
    - ls target
  only:
    - main
  tags:
    - study-runner

提交流水线:

shell 复制代码
[root@gitclient apiserver]# git add .
[root@gitclient apiserver]# git commit -m "Compile and Share Java to jar"
[root@gitclient apiserver]# git push origin main

查看流水线:

后端Java构建为镜像

编写相应的Dockerfile文件,将jar封装为对应的容器镜像。

shell 复制代码
[root@gitclient apiserver]# vim Dockerfile
FROM uhub.service.ucloud.cn/imxhy/maven:3.8.5-openjdk-17

MAINTAINER xhy@itzgr.cn

RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone

EXPOSE 8080

WORKDIR /opt/apiservice/

COPY target/apiservice-0.0.1-SNAPSHOT.jar ./

ENTRYPOINT ["java","-jar","/opt/apiservice/apiservice-0.0.1-SNAPSHOT.jar"]

使用Dockerfile构建镜像的步骤可以直接合入到流水线中,即增加build阶段。

提前在gitlab中创建ALIYUN_USER和ALIYUN_PASSWORD变量,配置阿里云镜像推送的账号和密码。

shell 复制代码
[root@gitclient apiserver]# vim .gitlab-ci.yml
stages:
  - compile
  - build

compile:
  stage: compile
  image: uhub.service.ucloud.cn/imxhy/maven:3.8.5-openjdk-17
  artifacts:
    paths:
      - target/apiservice-0.0.1-SNAPSHOT.jar
  script:
    - mvn clean
    - mvn compile
    - mvn package -Dmaven.test.skip=true
    - ls target
  only:
    - main
  tags:
    - study-runner

build:
  stage: build
  image: uhub.service.ucloud.cn/imxhy/executor:v1.9.0-debug
  needs:
    - compile
  script:
    - ls target
    - IMAGE_TAG=$(echo "${CI_COMMIT_TIMESTAMP}" | sed 's/T/_/g; s/-//g; s/://g' | cut -c1-15)
    - IMAGE_TAG_TO_INSTALL=${CI_COMMIT_TAG:-$IMAGE_TAG}
    - mkdir -p /kaniko/.docker
    - echo "{\"auths\":{\"registry.cn-hangzhou.aliyuncs.com\":{\"auth\":\"$(echo -n ${ALIYUN_USER}:${ALIYUN_PASSWORD} | base64)\"}}}" > /kaniko/.docker/config.json
    - cat /kaniko/.docker/config.json
    - >
      /kaniko/executor
      --context "${CI_PROJECT_DIR}"
      --dockerfile "${CI_PROJECT_DIR}/Dockerfile"
      --destination "registry.cn-hangzhou.aliyuncs.com/xhyimages/apiservice:${IMAGE_TAG_TO_INSTALL}"
      --registry-mirror "https://dbzucv6w.mirror.aliyuncs.com"
  only:
    - main
    - tags
  tags:
    - study-runner

提交流水线:

shell 复制代码
[root@gitclient apiserver]# git add .
[root@gitclient apiserver]# git commit -m "Compile and Share and Build Java to jar"
[root@gitclient apiserver]# git push origin main

提示:如上流水线直接将构建的镜像推送到阿里云镜像仓库,便于后期直接使用。

查看流水线:

确认推送成功:

前端VUE项目构建

前端项目构建镜像

前端VUE项目可以直接构建为容器镜像,不需要编译。

编写如下 Dockerfile ,构建对应的容器镜像。

shell 复制代码
[root@gitclient webui]# vim Dockerfile
FROM uhub.service.ucloud.cn/imxhy/node:23.11.0

MAINTAINER xhy@itzgr.cn

RUN npm install -g @vue/cli

WORKDIR /opt/webui/

COPY . ./

RUN npm install

ENTRYPOINT ["npm","run","serve"]

编写流水线:

shell 复制代码
[root@gitclient webui]# vim .gitlab-ci.yml
stages:
  - build

build:
  stage: build
  image: uhub.service.ucloud.cn/imxhy/executor:v1.9.0-debug
  script:
    - IMAGE_TAG=$(echo "${CI_COMMIT_TIMESTAMP}" | sed 's/T/_/g; s/-//g; s/://g' | cut -c1-15)
    - IMAGE_TAG_TO_INSTALL=${CI_COMMIT_TAG:-$IMAGE_TAG}
    - mkdir -p /kaniko/.docker
    - echo "{\"auths\":{\"registry.cn-hangzhou.aliyuncs.com\":{\"auth\":\"$(echo -n ${ALIYUN_USER}:${ALIYUN_PASSWORD} | base64)\"}}}" > /kaniko/.docker/config.json
    - cat /kaniko/.docker/config.json
    - >
      /kaniko/executor
      --context "${CI_PROJECT_DIR}"
      --dockerfile "${CI_PROJECT_DIR}/Dockerfile"
      --destination "registry.cn-hangzhou.aliyuncs.com/xhyimages/webui:${IMAGE_TAG_TO_INSTALL}"
      --registry-mirror "https://dbzucv6w.mirror.aliyuncs.com"
  only:
    - main
    - tags
  tags:
    - study-runner

提交流水线:

shell 复制代码
[root@gitclient webui]# git add .
[root@gitclient webui]# git commit -m "Build webui to docker image"
[root@gitclient webui]# git push origin main

提示:如上流水线直接将构建的镜像推送到阿里云镜像仓库,便于后期直接使用。

查看流水线:

确认推送成功:

相关推荐
FungLeo2 天前
FengCMS · PHP+MySQL 升级实录 · CI 落地:PHP 8.4/8.5 矩阵、隔离凭证、诊断门槛失败
mysql·ci/cd·php·持续集成·fengcms
莪_幻尘3 天前
我扫了 30 个 AGENTS.md:真正的问题不是没人写,是没接线
ci/cd·agent·ai编程
吴建旭 智宅焕4 天前
智能家居全国交付的确定性架构设计与实现:从不确定转化到交付基础设施
运维·智能家居·devops
JianZhen✓5 天前
后端进阶 13 问:CI/CD、Prisma、ORM、ER 图、SSE、索引事务、Nest 生命周期一次讲透(附面试答法)
ci/cd·面试·职场和发展
JonaHu7 天前
AceDataCloud Coding Plan 多模型编程:本地、CI 与团队仓库的 Token 隔离实战
ai编程·devops·codex·api安全·claude code
吴建旭 智宅焕8 天前
智能家居全国交付中执行端能力评估与分级调度架构设计:从注册数量到交付确定性的系统实现
运维·智能家居·devops
亚那迦基9 天前
24小时高危漏洞速报,互联网惊天危机来临!PHP、Java、Node.js、NGINX、GitLab无一幸免,看看你家是否中招
nginx·gitlab·php
考虑考虑9 天前
git刷新远程分支列表
git·后端·gitlab
工程小白菜9 天前
ai-code-review 全链路:让 AI 来审 AI 的代码
ci/cd·ai编程
Frankie_stand10 天前
工程工具链实战指南:从版本管理、构建打包到 CI 与日志分析的落地方法
ci/cd·无人机