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

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


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


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

文章目录


  • 详细含注释版本请参考另一篇模板:
    Dockerfile和.gitlab-ci.yml文件模板(含详细注释)
  • 关于Java项目Gitlab CI/CD自动化部署的应用可参考:
    K8S部署Java项目(Gitlab CI/CD自动化部署)

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

Dockerfile

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

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


感谢阅读,祝君暴富!

你可能感兴趣的:(#,GitLab,#,Kubernetes,#,Spring,Boot,gitlab,ci/cd,kubernetes,docker,容器,运维,java)