实践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

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

查看流水线:

确认推送成功:

相关推荐
想你依然心痛3 小时前
AtomCode 在 DevOps 场景中的实战:CI/CD 流水线脚本自动生成
运维·ci/cd·docker·devops·自动化部署·github actions
IT二叔14 小时前
Java项目部署-03-teamcity-cicd-docker镜像流水线方式部署
java·ci/cd·持续部署
想你依然心痛1 天前
持续集成在嵌入式开发中的实践:GitLab CI与交叉编译——自动化构建、固件生成
ci/cd·自动化·gitlab
潘正翔1 天前
docker基础_镜像使用
linux·运维·服务器·docker·容器·centos·devops
love530love1 天前
AI Agent + 本地 ComfyUI 无头模式实战:关闭 IDE 后 AI 独立重启并完成图文生成
ide·人工智能·windows·python·音视频·agent·devops
智脑API平台1 天前
告警太多等于没告警:DevOps 用 Claude Code 做日志归因和发布复盘
运维·人工智能·devops·codex
hbugs0012 天前
【案例分享】全网首个华三数据中心流量可视化实验,基于EVE-NG V7平台
网络·网络协议·安全·devops·eve-ng
Sean‘2 天前
GitLab 升级后 502:Puma 反复重启问题处理记录
docker·gateway·gitlab
NexTunnel2 天前
告别传统组网,也能安全访问公司 GitLab / SVN
安全·svn·gitlab
love530love2 天前
WorkBuddy + 本地 ComfyUI Wan2.1 文生视频实战:从连续报错到成功出片的完整踩坑记录
人工智能·windows·python·音视频·devops·comfyui·mcp