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

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

查看流水线:

确认推送成功:

相关推荐
小飞程序员6 天前
Docker本地部署gitlab实践(windows,linux)
devops
牛奶咖啡137 天前
DevOps自动化运维实践_使用再生龙对Linux系统进行备份还原
运维·自动化·devops·linux系统的备份还原·linux系统克隆备份·再生龙
IAR Systems7 天前
松下电工借助IAR CI/CD解决方案,实现品质与效率双重飞跃
ci/cd
Cherry的跨界思维7 天前
【AI测试全栈:质量】47、Vue+Prometheus+Grafana实战:打造全方位AI监控面板开发指南
vue.js·人工智能·ci/cd·grafana·prometheus·ai测试·ai全栈
Aliex_git7 天前
Dockerfile 优化实践笔记
笔记·学习·gitlab
觅特科技-互站7 天前
告别手动微调Prompt:DevOps用陌讯Skills重构AI运维工作流
运维·prompt·线性回归·kmeans·devops
加农炮手Jinx7 天前
Flutter for OpenHarmony: Flutter 三方库 icon_font_generator 自动化将 SVG 图标集转化为字体文件(鸿蒙矢量资源全自动管理)
运维·flutter·华为·自动化·harmonyos·devops
成为你的宁宁8 天前
Jenkins 自动化部署前后端分离若依项目全攻略:涵盖环境配置、Maven/Node.js 工具安装、GitLab 项目协同,及前后端构建、服务器推送与代码更新验证全步骤
node.js·自动化·gitlab·jenkins·maven
古斯塔夫歼星炮8 天前
Dify + Jenkins 实现AI应用持续集成与自动化部署
ci/cd·jenkins·dify
codingWhat8 天前
手把手系列之——前端工程化
ci/cd·devops·前端工程化