├── Dockerfile ├── Jenkinsfile ├── README.md ├── deploy ├── deployment.yaml └── kubernetes.yaml ├── pom.xml ├── src └── main │ ├── java │ └── club │ │ └── mydlq │ │ └── springboothelloword │ │ ├── Application.java │ │ └── HelloController.java │ └── resources │ └── application.yaml └── values.yaml /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM registry.cn-shanghai.aliyuncs.com/mydlq/openjdk:8u201-jdk-alpine3.9 2 | VOLUME /tmp 3 | ADD target/*.jar app.jar 4 | RUN sh -c 'touch /app.jar' 5 | ENV JAVA_OPTS="-Xmx512M -Xms256M -Xss256k -Duser.timezone=Asia/Shanghai" 6 | ENV APP_OPTS="" 7 | ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar $APP_OPTS" ] -------------------------------------------------------------------------------- /Jenkinsfile: -------------------------------------------------------------------------------- 1 | // 执行Helm的方法 2 | def helmDeploy(Map args) { 3 | if(args.init){ 4 | println "Helm 初始化" 5 | sh "helm init --client-only --stable-repo-url ${args.url}" 6 | } else if (args.dry_run) { 7 | println "尝试 Helm 部署,验证是否能正常部署" 8 | sh "helm upgrade --install ${args.name} --namespace ${args.namespace} ${args.values} --set ${images},${tag} stable/${args.template} --dry-run --debug" 9 | } else { 10 | println "正式 Helm 部署" 11 | sh "helm upgrade --install ${args.name} --namespace ${args.namespace} ${args.values} --set ${images},${tag} stable/${args.template}" 12 | } 13 | } 14 | 15 | // jenkins slave 执行流水线任务 16 | timeout(time: 600, unit: 'SECONDS') { 17 | try{ 18 | def label = "jnlp-agent" 19 | podTemplate(label: label,cloud: 'kubernetes' ){ 20 | node (label) { 21 | stage('Git阶段'){ 22 | echo "Git 阶段" 23 | git branch: "master" ,changelog: true , url: "https://github.com/my-dlq/springboot-helloworld.git" 24 | } 25 | stage('Maven阶段'){ 26 | echo "Maven 阶段" 27 | container('maven') { 28 | //这里引用上面设置的全局的 settings.xml 文件,根据其ID将其引入并创建该文件 29 | configFileProvider([configFile(fileId: "75884c5a-4ec2-4dc0-8d87-58b6b1636f8a", targetLocation: "settings.xml")]){ 30 | sh "mvn clean install -Dmaven.test.skip=true --settings settings.xml" 31 | } 32 | } 33 | } 34 | stage('Docker阶段'){ 35 | echo "Docker 阶段" 36 | container('docker') { 37 | // 读取pom参数 38 | echo "读取 pom.xml 参数" 39 | pom = readMavenPom file: './pom.xml' 40 | // 设置镜像仓库地址 41 | hub = "registry.cn-shanghai.aliyuncs.com" 42 | // 设置仓库项目名 43 | project_name = "mydlq" 44 | echo "编译 Docker 镜像" 45 | docker.withRegistry("http://${hub}", "ffb3b544-108e-4851-b747-b8a00bfe7ee0") { 46 | echo "构建镜像" 47 | // 设置推送到aliyun仓库的mydlq项目下,并用pom里面设置的项目名与版本号打标签 48 | def customImage = docker.build("${hub}/${project_name}/${pom.artifactId}:${pom.version}") 49 | echo "推送镜像" 50 | customImage.push() 51 | echo "删除镜像" 52 | sh "docker rmi ${hub}/${project_name}/${pom.artifactId}:${pom.version}" 53 | } 54 | } 55 | } 56 | stage('Helm阶段'){ 57 | container('helm-kubectl') { 58 | withKubeConfig([credentialsId: "8510eda6-e1c7-4535-81af-17626b9575f7",serverUrl: "https://kubernetes.default.svc.cluster.local"]) { 59 | // 设置参数 60 | images = "image.repository=${hub}/${project_name}/${pom.artifactId}" 61 | tag = "image.tag=${pom.version}" 62 | template = "spring-boot" 63 | repo_url = "http://chart.mydlq.club" 64 | app_name = "${pom.artifactId}" 65 | // 检测是否存在yaml文件 66 | def values = "" 67 | if (fileExists('values.yaml')) { 68 | values = "-f values.yaml" 69 | } 70 | // 执行 Helm 方法 71 | echo "Helm 初始化" 72 | helmDeploy(init: true ,url: "${repo_url}"); 73 | echo "Helm 执行部署测试" 74 | helmDeploy(init: false ,dry_run: true ,name: "${app_name}" ,namespace: "mydlqcloud" ,image: "${images}" ,tag: "${tag}" , values: "${values}" ,template: "${template}") 75 | echo "Helm 执行正式部署" 76 | helmDeploy(init: false ,dry_run: false ,name: "${app_name}" ,namespace: "mydlqcloud",image: "${images}" ,tag: "${tag}" , values: "${values}" ,template: "${template}") 77 | } 78 | } 79 | } 80 | } 81 | } 82 | }catch(Exception e) { 83 | currentBuild.result = "FAILURE" 84 | }finally { 85 | // 获取执行状态 86 | def currResult = currentBuild.result ?: 'SUCCESS' 87 | // 判断执行任务状态,根据不同状态发送邮件 88 | stage('email'){ 89 | if (currResult == 'SUCCESS') { 90 | echo "发送成功邮件" 91 | emailext(subject: '任务执行成功',to: '3*****7@qq.com',body: '''任务已经成功构建完成...''') 92 | }else { 93 | echo "发送失败邮件" 94 | emailext(subject: '任务执行失败',to: '3*****7@qq.com',body: '''任务执行失败构建失败...''') 95 | } 96 | } 97 | } 98 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SpringBoot HelloWorld Project 2 | 3 | This project is the mirror docker image of Helm Chart template. 4 | -------------------------------------------------------------------------------- /deploy/deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: #APP_NAME 5 | labels: 6 | app: #APP_NAME 7 | annotations: 8 | uuid: #APP_UUID 9 | spec: 10 | type: NodePort 11 | ports: 12 | - name: server 13 | nodePort: 31311 14 | port: 8080 15 | targetPort: 8080 16 | - name: management 17 | nodePort: 31312 18 | port: 8081 19 | targetPort: 8081 20 | selector: 21 | app: #APP_NAME 22 | --- 23 | apiVersion: apps/v1 24 | kind: Deployment 25 | metadata: 26 | name: #APP_NAME 27 | labels: 28 | app: #APP_NAME 29 | spec: 30 | replicas: #APP_REPLICAS 31 | selector: 32 | matchLabels: 33 | app: #APP_NAME 34 | strategy: 35 | type: Recreate 36 | template: 37 | metadata: 38 | labels: 39 | app: #APP_NAME 40 | spec: 41 | containers: 42 | - name: #APP_NAME 43 | image: #APP_IMAGE_NAME 44 | imagePullPolicy: Always 45 | ports: 46 | - containerPort: 8080 47 | name: server 48 | - containerPort: 8081 49 | name: management 50 | resources: 51 | limits: 52 | cpu: 2000m 53 | memory: 512Mi 54 | requests: 55 | cpu: 1000m 56 | memory: 256Mi -------------------------------------------------------------------------------- /deploy/kubernetes.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: springboot-helloworld 5 | labels: 6 | app: springboot-helloworld 7 | spec: 8 | type: NodePort #---通过NodePort方式暴露端口,方便外界访问 9 | ports: 10 | - name: server #---服务端口名,用于访问监控 UI 11 | nodePort: 31311 12 | port: 8080 13 | targetPort: 8080 14 | - name: management #---指定监控端口名,表示此应用被 Springboot Admin 服务发现 15 | nodePort: 31312 16 | port: 8081 17 | targetPort: 8081 18 | selector: 19 | app: springboot-helloworld 20 | --- 21 | apiVersion: apps/v1 22 | kind: Deployment 23 | metadata: 24 | name: springboot-helloworld 25 | labels: 26 | app: springboot-helloworld 27 | spec: 28 | replicas: 1 29 | selector: 30 | matchLabels: 31 | app: springboot-helloworld 32 | template: 33 | metadata: 34 | labels: 35 | app: springboot-helloworld 36 | spec: 37 | containers: 38 | - name: springboot-helloworld 39 | image: 10.71.164.28:5000/springboot-helloworld:0.0.1 40 | imagePullPolicy: Always 41 | ports: 42 | - containerPort: 8080 43 | name: server 44 | - containerPort: 8081 45 | name: management 46 | resources: 47 | limits: 48 | cpu: 1000m 49 | memory: 512Mi 50 | requests: 51 | cpu: 500m 52 | memory: 256Mi -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.springframework.boot 8 | spring-boot-starter-parent 9 | 2.1.4.RELEASE 10 | 11 | 12 | 13 | club.mydlq 14 | springboot-helloworld 15 | 0.0.1 16 | springboot-helloworld 17 | This a project for Spring Boot , use docker build for helm 18 | 19 | 20 | 1.8 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-actuator 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-maven-plugin 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/main/java/club/mydlq/springboothelloword/Application.java: -------------------------------------------------------------------------------- 1 | package club.mydlq.springboothelloword; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/club/mydlq/springboothelloword/HelloController.java: -------------------------------------------------------------------------------- 1 | package club.mydlq.springboothelloword; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | @RestController 7 | public class HelloController { 8 | 9 | @GetMapping("/hello") 10 | public String getHello() { 11 | return "Hello World!"; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | tomcat: 4 | basedir: "/" 5 | 6 | spring: 7 | application: 8 | name: springboot-helloworld 9 | 10 | management: 11 | endpoint: 12 | restart: 13 | enabled: true 14 | endpoints: 15 | web: 16 | exposure: 17 | include: "*" 18 | server: 19 | port: 8081 20 | -------------------------------------------------------------------------------- /values.yaml: -------------------------------------------------------------------------------- 1 | kind: Deployment 2 | image: 3 | pullPolicy: "Always" 4 | replicas: 1 5 | resources: 6 | limits: 7 | memory: 512Mi 8 | cpu: 1000m 9 | requests: 10 | memory: 256Mi 11 | cpu: 500m 12 | 13 | #***java && app 环境变量设置 14 | env: 15 | - name: "JAVA_OPTS" 16 | value: "-Xmx512M -Xms355M -Xss256k -Duser.timezone=Asia/Shanghai" 17 | - name: "APP_OPTS" 18 | value: "" 19 | 20 | envFrom: 21 | #- configMapRef: 22 | # name: env-config 23 | 24 | service: 25 | type: NodePort #Service type设置 (可以设置为ClusterIP、NodePort、None) 26 | labels: 27 | svcEndpoints: actuator 28 | annotations: {} 29 | ports: 30 | - name: server 31 | port: 8080 32 | targetPort: 8080 33 | protocol: TCP 34 | nodePort: 30080 35 | - name: management 36 | port: 8081 37 | targetPort: 8081 38 | protocol: TCP 39 | nodePort: 30081 --------------------------------------------------------------------------------