Dockerfile和.gitlab-ci.yml文件模板

天行健,君子以自强不息;地势坤,君子以厚德载物。


每个人都有惰性,但不断学习是好好生活的根本,共勉!


文章均为学习整理笔记,分享记录为主,如有错误请指正,共同学习进步。

文章目录



以下为Java项目自动化部署所需的配置模板,无注释版本

Dockerfile

java 复制代码
FROM openjdk:8-jdk-alpine

MAINTAINER hs

ENV JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.392.b08-4.0.3.al8.x86_64
ENV MAVEN_HOME=/usr/share/maven
ENV PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
ENV TZ=Asia/Shanghai

WORKDIR /root/jar/k8s-project

ENTRYPOINT ["nohup","java","-jar","app.jar"]

.gitlab-ci.yml

yml 复制代码
variables:
  HARBOR_IP: 172.30.0.224
  HARBOR_PORT: 8443
  HARBOR_URL: ${HARBOR_IP}:${HARBOR_PORT}
  HARBOR_USERNAME: admin
  HARBOR_PASSWORD: Harbor12345
  IMAGE_HARBOR_REPOSITORY: ${HARBOR_IP}:${HARBOR_PORT}/k8s-demo
  PROJECT_IMAGE_NAME: k8s-springboot
  PROJECT_IMAGE_TAG: v20240204
  PROJECT_JAR_DIR: /root/jar/k8s-project
  MAVEN_REPOSITORY_DIR: /root/maven/.m2
  CI_IMAGE_DIR: /root/images

stages:
  - package
  - build
  - test
  - deploy

before_script:
  - echo: "project ci/cd task start"
  - mkdir -p ${MAVEN_REPOSITORY_DIR}

job1-package:
  stage: package
  image: docker:latest
  tags:
    - runner-01
  only:
    - master
  script:
    - mvn clean package -Dmaven.test.skip=true
    - mkdir -p ${PROJECT_JAR_DIR}
    - cp target/app.jar ${PROJECT_JAR_DIR}/app.jar

job2-build:
  stage: build
  image: docker:latest
  tags:
    - runner-01
  before_script:
    - mkdir -p ${CI_IMAGE_DIR}
  script:
    - echo '打包任务开始---->清理之前的jar包,打包,将包复制到容器中'
    - ls -l ${PROJECT_JAR_DIR}
    - docker build -t ${PROJECT_IMAGE_NAME}:${PROJECT_IMAGE_TAG} .
    - docker push ${PROJECT_IMAGE_NAME}:${PROJECT_IMAGE_TAG}
    - docker images | ${PROJECT_IMAGE_NAME}
    - docker -t  ${PROJECT_IMAGE_NAME}:${PROJECT_IMAGE_TAG} ${IMAGE_HARBOR_REPOSITORY}/${PROJECT_IMAGE_NAME}:${PROJECT_IMAGE_TAG}
    - docker push ${IMAGE_HARBOR_REPOSITORY}/${PROJECT_IMAGE_NAME}:${PROJECT_IMAGE_TAG}
    - rm -rf ${PROJECT_JAR_DIR}/app.jar
    - docker rmi -f ${IMAGE_HARBOR_REPOSITORY}/${PROJECT_IMAGE_NAME}:${PROJECT_IMAGE_TAG}
    - docker rmi -f ${CI_IMAGE_DIR}:${PROJECT_IMAGE_TAG}

job4-deploy:
  stage: deploy
  image: ${IMAGE_HARBOR_REPOSITORY}/${PROJECT_IMAGE_NAME}:${PROJECT_IMAGE_TAG}
  tags:
    - runner-01
  only:
    - master
  script:
    - echo '项目部署--->开始部署,缓存部署,pod部署,服务部署'
    - kubectl apply -f /sb-p/sb-test/sb-pvc.yaml
    - kubectl apply -f /sb-p/sb-test/sb-dplm.yaml
    - kubectl apply -f /sb-p/sb-test/sb-svc.yaml

感谢阅读,祝君暴富!

相关推荐
大迪吃小迪3 小时前
每秒 400 请求场景下,线程池如何合理配置?
java·开发语言
ArabySide3 小时前
【ASP.NET Core】ASP.NET Core应用的Docker容器化与阿里云托管
阿里云·docker·asp.net
xuchaoxin13754 小时前
cdn节点代理的副作用@fail2ban对接cdn封锁恶意请求ip@fail2ban封锁ip有效性问题
运维·网络·cdn·cloudflare
雨中飘荡的记忆4 小时前
财务对账系统设计与实现
java
起风了___4 小时前
Jenkins + Kubernetes 多模块微服务一键流水线:从 Maven 打包到滚动发布完整脚本
kubernetes·jenkins
0***h9424 小时前
使用 java -jar 命令启动 Spring Boot 应用时,指定特定的配置文件的几种实现方式
java·spring boot·jar
雨中飘荡的记忆4 小时前
布式事务详解:从理论到实践(RocketMQ + Seata)
java·rocketmq
i***48614 小时前
微服务生态组件之Spring Cloud LoadBalancer详解和源码分析
java·spring cloud·微服务
zzlyx994 小时前
用C#采用Avalonia+Mapsui在离线地图上插入图片画信号扩散图
java·开发语言·c#
Aevget4 小时前
MyEclipse全新发布v2025.2——AI + Java 24 +更快的调试
java·ide·人工智能·eclipse·myeclipse