1. 准备源码
2. 编写dockfile
[admin@master01 oa ]$vim Dockerfile
FROM harbor.xx.com/library/tomcat:centos7-jdk8
ADD oa_platform/oa_console_web/target/ROOT.war webapps/ROOT
COPY unzip-6.0-24.el7_9.x86_64.rpm .
RUN rpm -ivh unzip-6.0-24.el7_9.x86_64.rpm && rm -rf unzip-6.0-24.el7_9.x86_64.rpm && cd webapps/ROOT && unzip ROOT.war && rm -rf ROOT.war
3. 编写 cat deploy.yaml
[admin@master01 oa ]$vim deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: oa
namespace: {NameSpace}
spec:
replicas: 1
selector:
matchLabels:
app: bms
template:
metadata:
labels:
app: bms
spec:
imagePullSecrets:
- name: harbor-admin
containers:
- name: oa
image: {Image}
ports:
- name: http
containerPort: 8080
env:
- name: JAVA_OPTS
value: "-server -Xms${JVM_XMS:-256m} -Xmx${JVM_XMX:-256m} -XX:+UseConcMarkSweepGC -Dsys.env=prod"
- name: JVM_XMS
valueFrom:
resourceFieldRef:
resource: requests.memory
- name: JVM_XMX
valueFrom:
resourceFieldRef:
resource: limits.memory
resources:
requests:
memory: 2048Mi
limits:
memory: 2048Mi
readinessProbe:
tcpSocket:
port: http
initialDelaySeconds: 10
failureThreshold: 3
livenessProbe:
tcpSocket:
port: http
initialDelaySeconds: 10
failureThreshold: 3
---
apiVersion: v1
kind: Service
metadata:
name: oa-svc
namespace: {NameSpace}
spec:
selector:
app: bms
ports:
- port: 8080
targetPort: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: oa-ingress
namespace: {NameSpace}
spec:
ingressClassName: "nginx"
rules:
- host: "{host}"
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: oa-svc
port:
number: 8080
3. 上传代码
[admin@master01 oa ]$git add .
[admin@master01 oa ]$git commit -m 'add xxx'
[admin@master01 oa ]$git push origin master
4. 编写Pipeline oa-CI脚本
pipeline {
agent {
kubernetes {
cloud 'kubernetes'
yaml '''
apiVersion: v1
kind: Pod
spec:
imagePullSecrets:
- name: harbor-admin
volumes:
- name: data
nfs:
server: 192.168.4.26
path: /data/maven
- name: dockersock
hostPath:
path: /run/docker.sock
containers:
- name: maven
image: harbor.xx.com/ops/maven:3.8.6
imagePullPolicy: IfNotPresent
command: ["cat"]
tty: true
volumeMounts:
- name: data
mountPath: /root/.m2
- name: nodejs
image: harbor.xx.com/ops/nodejs:14.20
imagePullPolicy: IfNotPresent
command: ["cat"]
tty: true
- name: sonar
image: harbor.xx.com/ops/sonar-scanner:2.3.0
imagePullPolicy: IfNotPresent
command: ["cat"]
tty: true
- name: docker
image: harbor.xx.com/ops/docker:20.10
imagePullPolicy: IfNotPresent
command: ["cat"]
tty: true
volumeMounts:
- name: dockersock
mountPath: /run/docker.sock
- name: kubectl
image: harbor.xx.com/ops/kubectl:1.22.2
imagePullPolicy: IfNotPresent
command: ["cat"]
tty: true
'''
}
}
environment {
gitlabID = "gitlab-root-token"
gitlabPro = "http://gitlab.xx.com/dev/staroffice.git"
//Harbor相关的全局变量
Url = "harbor.xx.com"
Pro = "oa"
ImageName = "${Url}/${Pro}/staroffice"
HARBOR_ID = "harbor-admin"
//对外暴露的域名
ingressHost = "oa.xx.com"
}
// 实现SpringBoot应用的CI
stages {
stage('获取代码') {
steps {
container('maven') {
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: "${gitlabID}", url: "${gitlabPro}"]]) }
}
}
stage('编译代码') {
steps {
container('maven') {
//sh 'mvn package -Dmaven.test.skip=true'
//sh 'mvn clean package -DskipTests -Pprod install -f staroffice_platform/pom.xml'
sh 'mvn clean install -Dmaven.test.skip=true -Pprod -f staroffice_platform/pom.xml '
}
}
}
stage('生成镜像Tag'){
steps {
container('maven') {
script {
//本次git提交的commid (git log -n1 --pretty=format:'%h')
env.COMMITID = sh(returnStdout: true, script: "git log -n1 --pretty=format:'%h'").trim()
//构建的时间 (date +%Y%m%d_%H%M%S)
env.BuildTime = sh(returnStdout: true, script: "date +%Y%m%d_%H%M%S").trim()
//完整的镜像Tag (c106654_20221115_133911)
env.ImageTag = COMMITID + "_" + BuildTime
}
sh 'echo "镜像的Tag: ${ImageTag}"'
}
}
}
stage('制作Docker镜像') {
steps {
container('docker') {
withCredentials([usernamePassword(credentialsId: "${HARBOR_ID}", passwordVariable: 'HARBOR_PASSWORD', usernameVariable: 'HARBOR_USER')]) {
//登录harbor
sh "echo ${HARBOR_PASSWORD} | docker login $Url -u ${HARBOR_USER} --password-stdin"
//构建镜像
sh "docker build -t ${ImageName}:${ImageTag} ."
//推送镜像
sh "docker push ${ImageName}:${ImageTag}"
//删除镜像
sh "docker rmi ${ImageName}:${ImageTag}"
}
}
}
}
stage('交付应用至K8S') {
steps {
container('kubectl') {
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
sh 'mkdir ~/.kube && cp ${KUBECONFIG} ~/.kube/config'
sh 'sed -i "s#{NameSpace}#oa#g" deploy.yaml'
sh 'sed -i "s#{Image}#${ImageName}:${ImageTag}#g" deploy.yaml'
sh 'sed -i "s#{host}#${ingressHost}#g" deploy.yaml'
sh 'cat deploy.yaml'
sh 'kubectl apply -f deploy.yaml'
}
}
}
}
}
}