feat:cicd

This commit is contained in:
devad 2023-03-24 09:05:45 +08:00
parent dc2b6a3cf2
commit c7a3e2b6d6
5 changed files with 186 additions and 0 deletions

6
Dockerfile Normal file
View File

@ -0,0 +1,6 @@
FROM nginx:stable-alpine
COPY dist/ /usr/share/nginx/html/jcce
RUN rm /etc/nginx/conf.d/default.conf
COPY deploy/nginx/default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

70
Jenkinsfile vendored Normal file
View File

@ -0,0 +1,70 @@
def JOB_NAME = "${env.JOB_NAME}"
def BUILD_NUMBER = "${env.BUILD_NUMBER}"
def label = "jenkins-${JOB_NAME}-${BUILD_NUMBER}-${UUID.randomUUID().toString()}"
def secret_name = "harbor-auth"
podTemplate(label: label, containers: [
containerTemplate(name: 'nodejs', image: 'node:14.20.0-alpine3.16', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'docker', image: 'docker:latest', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'kubectl', image: 'jcce/kubectl:1.23.7', command: 'cat', ttyEnabled: true)
], serviceAccount: 'jenkins', volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
]) {
node(label) {
def myRepo = checkout scm
// 获取 git commit id 作为镜像标签
def imageTag = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
// 仓库地址
def registryUrl = "hub.jcce.dev:18443/repository/docker-hub"
def imageEndpoint = "jcce/kubex-frontend"
// 镜像
def image = "${registryUrl}/${imageEndpoint}:${imageTag}"
def imageLatest = "${registryUrl}/${imageEndpoint}:latest"
stage('单元测试') {
echo "1.测试阶段"
}
stage('代码编译打包') {
try {
container('nodejs') {
echo "2.代码编译打包阶段"
sh('npm install --registry=https://registry.npmmirror.com')
sh('npm run build:prod')
}
} catch (exc) {
println "构建失败 - ${currentBuild.fullDisplayName}"
throw(exc)
}
}
stage('构建 Docker 镜像') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'docker-auth',
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASSWORD']]) {
container('docker') {
echo "3. 构建 Docker 镜像阶段"
sh('cat /etc/resolv.conf')
sh("docker login '${registryUrl}' -u '${DOCKER_USER}' -p '${DOCKER_PASSWORD}' ")
sh("docker build -t '${image}' -t '${imageLatest}' .")
sh("docker push '${image}'")
sh("docker push '${imageLatest}'")
sh("docker rmi '${image}' '${imageLatest}'")
}
}
}
stage('运行 Kubectl 部署到k8s平台') {
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
container('kubectl') {
echo "5.部署应用"
sh('mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config')
sh("sed -i 's#IMAGE_NAME#${image}#' deploy/k8s/deployment.yaml")
sh("sed -i 's#SECRET_NAME#${secret_name}#' deploy/k8s/deployment.yaml")
sh('kubectl apply -f deploy/k8s/')
sh('sleep 3')
echo "6.查看应用"
sh('kubectl get all -n jcce-system -l app=${JOB_NAME}')
}
}
}
}
}

View File

@ -0,0 +1,44 @@
kind: Deployment
apiVersion: apps/v1
metadata:
name: kubex-frontend-deployment
namespace: jcce-system
labels:
k8s-app: kubex-frontend
spec:
replicas: 1
selector:
matchLabels:
k8s-app: kubex-frontend
template:
metadata:
name: kubex-frontend
labels:
k8s-app: kubex-frontend
spec:
imagePullSecrets:
- name: SECRET_NAME
containers:
- name: kubex-frontend
image: IMAGE_NAME
resources: {}
imagePullPolicy: Always
securityContext:
privileged: false
procMount: Default
ports:
- containerPort: 80
volumeMounts: []
volumes: []
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
securityContext: {}
schedulerName: default-scheduler
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600

16
deploy/k8s/service.yaml Normal file
View File

@ -0,0 +1,16 @@
apiVersion: v1
kind: Service
metadata:
namespace: jcce-system
name: kubex-frontend-service
labels:
k8s-service: kubex-frontend
spec:
selector:
k8s-app: kubex-frontend
ports:
- name: web
protocol: TCP
port: 80
targetPort: 80
type: ClusterIP

50
deploy/nginx/default.conf Normal file
View File

@ -0,0 +1,50 @@
upstream backend-up {
server jcce-gateway-service:8000;
}
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/host.access.log main;
error_log /var/log/nginx/error.log error;
location / {
root /usr/share/nginx/html/jcce; #站点目录
index index.html index.htm;
}
#monitor项目
location /monitor {
alias /usr/share/nginx/html/jcce/;
try_files $uri $uri/ /monitor/index.html; #解决页面刷新404问题
index index.html index.htm;
autoindex on;
}
location /apis/ {
proxy_http_version 1.1;
proxy_pass http://backend-up/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 12288m;
root html;
index index.html index.htm;
}
#虚拟机
location ^~/jcc-vm/ {
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass https://10.101.14.2/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}