实践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 [email protected]

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 [email protected]

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

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

查看流水线:

确认推送成功:

相关推荐
云攀登者-望正茂2 小时前
通过 Azure DevOps 探索 Helm 和 Azure AKS
azure·devops
极小狐8 小时前
如何从极狐GitLab 容器镜像库中删除容器镜像?
java·linux·开发语言·数据库·python·elasticsearch·gitlab
极小狐8 小时前
如何使用极狐GitLab 软件包仓库功能托管 terraform?
linux·运维·git·ssh·gitlab·terraform
极小狐14 小时前
如何使用极狐GitLab 软件包仓库功能托管 maven?
java·运维·数据库·安全·c#·gitlab·maven
Once_day19 小时前
研发效率破局之道阅读总结(5)管理文化
研发效能·devops
Johny_Zhao1 天前
思科安全大模型SOC作业应用分析
linux·网络·人工智能·网络安全·ai·信息安全·云计算·shell·devops·cisco·yum源·系统运维·itsm
杰克逊的日记1 天前
CI/CD面试题及答案
ci/cd·gitlab
剑哥在胡说1 天前
高并发PHP部署演进:从虚拟机到K8S的DevOps实践优化
kubernetes·php·devops
杰克逊的日记1 天前
集成管理工具Gitlab
gitlab