所需中间件
Jenkins+harbor+nexus+docker+k8s
Jenkins执行流水线脚本命令
harbor维护管理docker镜像
nexus维护私有maven依赖
docker创建服务各个版本的docker镜像
k8s部署服务镜像
后端:
Jenkins流水线脚本
pipeline {
agent none
environment {
FLAG = 'ruoyigatewaydev'
}
options {
timeout(time:25, unit: 'MINUTES')
}
stages {
stage('Build Package') {
agent {
docker {
image 'maven:3.6.1-jdk-8'
args '-v /var/lib/jenkins/repository:/repository '
}
}
steps {
git branch: 'dev',
credentialsId: 'gitlab_local_pwd',
url: 'http://git_ip:git_port/lhy/ruoyi-gateway.git'
sh '''
mvn -s settings.xml -Dmaven.repo.local=/repository -B clean package -DskipTests=true
'''
}
}
stage('Build image and Push') {
agent any
environment {
IMAGE_TAG = sh(returnStdout: true,script: 'echo $image_tag').trim()
}
steps {
sh """
docker build -t harbor_ip:harbor_port/public/${FLAG}:${IMAGE_TAG} ./
docker push harbor_ip:harbor_port/public/${FLAG}:${IMAGE_TAG}
docker image rm harbor_ip:harbor_port/public/${FLAG}:${IMAGE_TAG}
"""
}
}
stage('Deploy') {
agent any
steps {
script{
def remote = [:]
remote.name = 'kube'
remote.host = 'k8s_ip'
remote.port = 22
remote.allowAnyHosts = true
//通过withCredentials调用Jenkins凭据中已保存的凭据,credentialsId需要填写,其他保持默认即可
withCredentials([usernamePassword(credentialsId: 'Vm_local_pwd', passwordVariable: 'password', usernameVariable: 'userName')]) {
remote.user = "${userName}"
remote.password = "${password}"
}
sshCommand remote: remote, command: """
if [ ! -d '/home/ruoyi/${FLAG}' ];then
mkdir /home/ruoyi/${FLAG}
else
echo "文件夹已经存在"
fi
"""
sshPut remote: remote, from: 'k8s.yaml', into: "/home/ruoyi/${FLAG}"
sshCommand remote: remote, command: """
sed -i 's#${FLAG}:.*#${FLAG}:${IMAGE_TAG}#g' /home/ruoyi/${FLAG}/k8s.yaml
kubectl apply -f /home/ruoyi/${FLAG}/k8s.yaml
rm -rf /home/ruoyi/${FLAG}/k8s.yaml
"""
}
}
}
}
}
k8s.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ruoyigateway
namespace: ruoyi-dev
labels:
app: ruoyigateway
spec:
replicas: 1
selector:
matchLabels:
app: ruoyigateway
template:
metadata:
name: ruoyigateway
labels:
app: ruoyigateway
spec:
containers:
- name: ruoyigateway
image: harbor_ip:harbor_port/public/ruoyigatewaydev:7.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: containerPort
protocol: TCP
restartPolicy: Always
imagePullSecrets:
- name: harbor-lhy
---
apiVersion: v1
kind: Service
metadata:
name: ruoyigateway
namespace: ruoyi-dev
spec:
selector:
app: ruoyigateway
ports:
- protocol: TCP
port: port
targetPort: targetPort
nodePort: nodePort
type: NodePort
DockerFile
FROM openjdk:8-jre
LABEL authors="lhyshome_pc"
ADD target/ruoyi-gateway.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
前端
Jenkins流水线脚本
pipeline {
agent none
environment {
FLAG = 'ruoyiuidev'
}
options {
timeout(time:25, unit: 'MINUTES')
}
stages {
stage('npm') {
agent {
docker {
image 'node:14'
args '-u root:root --network=host'
}
}
steps {
git branch: 'dev',
credentialsId: 'gitlab_local_pwd',
url: 'http://git_ip:git_port/lhy/ruoyi-ui.git'
sh """
npm install && npm run build:stage
"""
}
}
stage('Build image and Push') {
agent any
environment {
IMAGE_TAG = sh(returnStdout: true,script: 'echo $image_tag').trim()
}
steps {
sh """
docker build -t harbor_ip:harbor_port/public/${FLAG}:${IMAGE_TAG} ./
docker push harbor_ip:harbor_port/public/${FLAG}:${IMAGE_TAG}
docker image rm harbor_ip:harbor_port/public/${FLAG}:${IMAGE_TAG}
"""
}
}
stage('Deploy') {
agent any
steps {
script{
def remote = [:]
remote.name = 'kube'
remote.host = 'k8s_ip'
remote.port = 22
remote.allowAnyHosts = true
//通过withCredentials调用Jenkins凭据中已保存的凭据,credentialsId需要填写,其他保持默认即可
withCredentials([usernamePassword(credentialsId: 'Vm_local_pwd', passwordVariable: 'password', usernameVariable: 'userName')]) {
remote.user = "${userName}"
remote.password = "${password}"
}
sshCommand remote: remote, command: """
if [ ! -d '/home/ruoyi/${FLAG}' ];then
mkdir /home/ruoyi/${FLAG}
else
echo "文件夹已经存在"
fi
"""
sshPut remote: remote, from: 'k8s.yaml', into: "/home/ruoyi/${FLAG}"
sshCommand remote: remote, command: """
sed -i 's#${FLAG}:.*#${FLAG}:${IMAGE_TAG}#g' /home/ruoyi/${FLAG}/k8s.yaml
kubectl apply -f /home/ruoyi/${FLAG}/k8s.yaml
rm -rf /home/ruoyi/${FLAG}/k8s.yaml
"""
}
}
}
}
}
k8s.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ruoyiui
namespace: ruoyi-dev
spec:
replicas: 1
selector:
matchLabels:
app: ruoyiui
template:
metadata:
labels:
app: ruoyiui
spec:
containers:
- name: ruoyiui
image: harbor_ip:harbor_port/public/ruoyiuiprod:9.0
ports:
- containerPort: containerPort
---
apiVersion: v1
kind: Service
metadata:
name: ruoyiui
namespace: ruoyi-dev
spec:
type: LoadBalancer
selector:
app: ruoyiui
ports:
- protocol: TCP
port: port
targetPort: targetPort
nodePort: nodePort
DockerFile
FROM nginx:1.14.1
ADD ./dist /home
ADD ./nginx.conf /etc/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /home;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
# 避免actuator暴露
if ($request_uri ~ "/actuator") {
return 403;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}