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

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

查看流水线:

确认推送成功:

相关推荐
2501_9289962214 小时前
GitLab CVE-2026-19478漏洞解析:Gitaly ref攻击绕过审计与中科热备下的代码资产保护重构
重构·gitlab
秦渝兴16 小时前
GitLab CI/CD 流水线实战
ci/cd·gitlab
gs8014017 小时前
告别 CI/CD 误伤与红条:Docker 镜像智能清理与优雅防冲突实战
ci/cd·docker·容器
leeyi20 小时前
DDD 六条铁律:让 CI 替你骂人——go-arch-lint 门禁实战(第104篇)
ci/cd·agent·领域驱动设计
算法大模型备案干货咪21 小时前
《把内容安全做成CI卡点:AIGC合规的工程化落地》
安全·ci/cd·aigc
Lyy1 天前
DevOps平台 — 第九篇:配置中心的设计与实现
后端·devops
code 小楊1 天前
生产级 Agent 评测体系实战:从 12 指标框架到 CI/CD 质量门禁全链路落地
大数据·人工智能·ci/cd
Ningcode_cloud2 天前
什么是CICD? GitLab + Jenkins 持续集成实战部署手册
运维·ci/cd·云原生·容器·gitlab·jenkins
姚永强2 天前
gitlab安装
运维·gitlab
成茂峰2 天前
实战:内外网隔离环境下基于 Jenkins + PowerShell 的自动化 CI 构建与邮件通知方案
ci/cd·自动化·jenkins