Commit bea364fa by kang.li

first commit

parents
FROM registry.cn-shanghai.aliyuncs.com/mydlq/openjdk:8u201-jdk-alpine3.9
VOLUME /tmp
ADD target/*.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS="-Xmx512M -Xms256M -Xss256k -Duser.timezone=Asia/Shanghai"
ENV APP_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar $APP_OPTS" ]
\ No newline at end of file
// 执行Helm的方法
def helmDeploy(Map args) {
if(args.init){
println "Helm 初始化"
sh "helm init --client-only --stable-repo-url ${args.url}"
} else if (args.dry_run) {
println "尝试 Helm 部署,验证是否能正常部署"
sh "helm upgrade --install ${args.name} --namespace ${args.namespace} ${args.values} --set ${images},${tag} stable/${args.template} --dry-run --debug"
} else {
println "正式 Helm 部署"
sh "helm upgrade --install ${args.name} --namespace ${args.namespace} ${args.values} --set ${images},${tag} stable/${args.template}"
}
}
// jenkins slave 执行流水线任务
timeout(time: 600, unit: 'SECONDS') {
try{
def label = "jnlp-agent"
podTemplate(label: label,cloud: 'kubernetes' ){
node (label) {
stage('Git阶段'){
echo "Git 阶段"
git branch: "master" ,changelog: true , url: "https://github.com/my-dlq/springboot-helloworld.git"
}
stage('Maven阶段'){
echo "Maven 阶段"
container('maven') {
//这里引用上面设置的全局的 settings.xml 文件,根据其ID将其引入并创建该文件
configFileProvider([configFile(fileId: "75884c5a-4ec2-4dc0-8d87-58b6b1636f8a", targetLocation: "settings.xml")]){
sh "mvn clean install -Dmaven.test.skip=true --settings settings.xml"
}
}
}
stage('Docker阶段'){
echo "Docker 阶段"
container('docker') {
// 读取pom参数
echo "读取 pom.xml 参数"
pom = readMavenPom file: './pom.xml'
// 设置镜像仓库地址
hub = "registry.cn-shanghai.aliyuncs.com"
// 设置仓库项目名
project_name = "mydlq"
echo "编译 Docker 镜像"
docker.withRegistry("http://${hub}", "ffb3b544-108e-4851-b747-b8a00bfe7ee0") {
echo "构建镜像"
// 设置推送到aliyun仓库的mydlq项目下,并用pom里面设置的项目名与版本号打标签
def customImage = docker.build("${hub}/${project_name}/${pom.artifactId}:${pom.version}")
echo "推送镜像"
customImage.push()
echo "删除镜像"
sh "docker rmi ${hub}/${project_name}/${pom.artifactId}:${pom.version}"
}
}
}
stage('Helm阶段'){
container('helm-kubectl') {
withKubeConfig([credentialsId: "8510eda6-e1c7-4535-81af-17626b9575f7",serverUrl: "https://kubernetes.default.svc.cluster.local"]) {
// 设置参数
images = "image.repository=${hub}/${project_name}/${pom.artifactId}"
tag = "image.tag=${pom.version}"
template = "spring-boot"
repo_url = "http://chart.mydlq.club"
app_name = "${pom.artifactId}"
// 检测是否存在yaml文件
def values = ""
if (fileExists('values.yaml')) {
values = "-f values.yaml"
}
// 执行 Helm 方法
echo "Helm 初始化"
helmDeploy(init: true ,url: "${repo_url}");
echo "Helm 执行部署测试"
helmDeploy(init: false ,dry_run: true ,name: "${app_name}" ,namespace: "mydlqcloud" ,image: "${images}" ,tag: "${tag}" , values: "${values}" ,template: "${template}")
echo "Helm 执行正式部署"
helmDeploy(init: false ,dry_run: false ,name: "${app_name}" ,namespace: "mydlqcloud",image: "${images}" ,tag: "${tag}" , values: "${values}" ,template: "${template}")
}
}
}
}
}
}catch(Exception e) {
currentBuild.result = "FAILURE"
}finally {
// 获取执行状态
def currResult = currentBuild.result ?: 'SUCCESS'
// 判断执行任务状态,根据不同状态发送邮件
stage('email'){
if (currResult == 'SUCCESS') {
echo "发送成功邮件"
emailext(subject: '任务执行成功',to: '3*****7@qq.com',body: '''任务已经成功构建完成...''')
}else {
echo "发送失败邮件"
emailext(subject: '任务执行失败',to: '3*****7@qq.com',body: '''任务执行失败构建失败...''')
}
}
}
}
\ No newline at end of file
# SpringBoot HelloWorld Project
This project is the mirror docker image of Helm Chart template.
apiVersion: v1
kind: Service
metadata:
name: #APP_NAME
labels:
app: #APP_NAME
annotations:
uuid: #APP_UUID
spec:
type: NodePort
ports:
- name: server
nodePort: 31311
port: 8080
targetPort: 8080
- name: management
nodePort: 31312
port: 8081
targetPort: 8081
selector:
app: #APP_NAME
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: #APP_NAME
labels:
app: #APP_NAME
spec:
replicas: #APP_REPLICAS
selector:
matchLabels:
app: #APP_NAME
strategy:
type: Recreate
template:
metadata:
labels:
app: #APP_NAME
spec:
containers:
- name: #APP_NAME
image: #APP_IMAGE_NAME
imagePullPolicy: Always
ports:
- containerPort: 8080
name: server
- containerPort: 8081
name: management
resources:
limits:
cpu: 2000m
memory: 512Mi
requests:
cpu: 1000m
memory: 256Mi
\ No newline at end of file
apiVersion: v1
kind: Service
metadata:
name: springboot-helloworld
labels:
app: springboot-helloworld
spec:
type: NodePort #---通过NodePort方式暴露端口,方便外界访问
ports:
- name: server #---服务端口名,用于访问监控 UI
nodePort: 31311
port: 8080
targetPort: 8080
- name: management #---指定监控端口名,表示此应用被 Springboot Admin 服务发现
nodePort: 31312
port: 8081
targetPort: 8081
selector:
app: springboot-helloworld
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-helloworld
labels:
app: springboot-helloworld
spec:
replicas: 1
selector:
matchLabels:
app: springboot-helloworld
template:
metadata:
labels:
app: springboot-helloworld
spec:
containers:
- name: springboot-helloworld
image: 10.71.164.28:5000/springboot-helloworld:0.0.1
imagePullPolicy: Always
ports:
- containerPort: 8080
name: server
- containerPort: 8081
name: management
resources:
limits:
cpu: 1000m
memory: 512Mi
requests:
cpu: 500m
memory: 256Mi
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>club.mydlq</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>0.0.1</version>
<name>springboot-helloworld</name>
<description>This a project for Spring Boot , use docker build for helm</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package club.mydlq.springboothelloword;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package club.mydlq.springboothelloword;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String getHello() {
return "Hello World!";
}
}
server:
port: 8080
tomcat:
basedir: "/"
spring:
application:
name: springboot-helloworld
management:
endpoint:
restart:
enabled: true
endpoints:
web:
exposure:
include: "*"
server:
port: 8081
kind: Deployment
image:
pullPolicy: "Always"
replicas: 1
resources:
limits:
memory: 512Mi
cpu: 1000m
requests:
memory: 256Mi
cpu: 500m
#***java && app 环境变量设置
env:
- name: "JAVA_OPTS"
value: "-Xmx512M -Xms355M -Xss256k -Duser.timezone=Asia/Shanghai"
- name: "APP_OPTS"
value: ""
envFrom:
#- configMapRef:
# name: env-config
service:
type: NodePort #Service type设置 (可以设置为ClusterIP、NodePort、None)
labels:
svcEndpoints: actuator
annotations: {}
ports:
- name: server
port: 8080
targetPort: 8080
protocol: TCP
nodePort: 30080
- name: management
port: 8081
targetPort: 8081
protocol: TCP
nodePort: 30081
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment