部署脚本

main
root 2 weeks ago
parent ddebaa3c41
commit 05fa6f98a0

@ -1,4 +1,4 @@
FROM node:21.6.2 AS builder FROM m.daocloud.io/docker.io/library/node:21.6.2 AS builder
WORKDIR /src WORKDIR /src
@ -6,9 +6,10 @@ RUN npm install -g pnpm
COPY . . COPY . .
RUN pnpm config set registry https://registry.npmmirror.com
RUN make build-all RUN make build-all
FROM node:21.6.2-slim FROM m.daocloud.io/docker.io/library/node:21.6.2-slim
COPY --from=builder /src/dist/ /apps/dist/ COPY --from=builder /src/dist/ /apps/dist/
COPY --from=builder /src/node_modules/ /apps/node_modules/ COPY --from=builder /src/node_modules/ /apps/node_modules/

@ -41,11 +41,11 @@ start-prod:
.PHONY: build-image .PHONY: build-image
build-image: build-image:
docker build --platform linux/amd64 -t ${DOCKER_IMAGE}:${VERSION} . nerdctl -nk8s.io build --platform linux/amd64 -t ${DOCKER_IMAGE}:${VERSION} .
.PHONY: push-image .PHONY: push-image
push-image: push-image:
docker push ${DOCKER_IMAGE}:${VERSION} nerdctl -nk8s.io push ${DOCKER_IMAGE}:${VERSION}
.PHONY: release .PHONY: release
release: build-image push-image release: build-image push-image

@ -0,0 +1,150 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
meta.helm.sh/release-name: hami-webui
meta.helm.sh/release-namespace: kube-system
labels:
app.kubernetes.io/component: hami-webui
app.kubernetes.io/instance: hami-webui
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: hami-webui
app.kubernetes.io/version: 1.0.5
helm.sh/chart: hami-webui-1.0.5
name: hami-webui
namespace: kube-system
resourceVersion: '215714495'
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app.kubernetes.io/component: hami-webui
app.kubernetes.io/instance: hami-webui
app.kubernetes.io/name: hami-webui
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app.kubernetes.io/component: hami-webui
app.kubernetes.io/instance: hami-webui
app.kubernetes.io/name: hami-webui
spec:
containers:
- args:
- /apps/dist/main
command:
- node
env:
- name: TZ
value: Asia/Shanghai
image: 'projecthami/hami-webui-fe:dev'
imagePullPolicy: IfNotPresent
name: hami-webui-fe-oss
ports:
- containerPort: 3000
name: http
protocol: TCP
resources:
limits:
cpu: 200m
memory: 500Mi
requests:
cpu: 200m
memory: 500Mi
securityContext: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
- args:
- '--conf'
- /apps/config/config.yaml
command:
- /apps/server
env:
- name: TZ
value: Asia/Shanghai
image: 'projecthami/hami-webui-be:dev'
imagePullPolicy: IfNotPresent
name: hami-webui-be-oss
ports:
- containerPort: 8000
name: metrics
protocol: TCP
resources:
limits:
cpu: 50m
memory: 250Mi
requests:
cpu: 50m
memory: 250Mi
securityContext: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /apps/config/
name: config
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: hami-webui
serviceAccountName: hami-webui
terminationGracePeriodSeconds: 30
volumes:
- configMap:
defaultMode: 420
name: hami-webui-config
name: config
---
apiVersion: v1
kind: Service
metadata:
annotations:
meta.helm.sh/release-name: hami-webui
meta.helm.sh/release-namespace: kube-system
labels:
app.kubernetes.io/component: hami-webui
app.kubernetes.io/instance: hami-webui
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: hami-webui
app.kubernetes.io/version: 1.0.5
helm.sh/chart: hami-webui-1.0.5
name: hami-webui
namespace: kube-system
resourceVersion: '207243046'
spec:
clusterIP: 10.96.3.221
clusterIPs:
- 10.96.3.221
externalTrafficPolicy: Cluster
internalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- name: http
nodePort: 29999
port: 3000
protocol: TCP
targetPort: http
- name: metrics
nodePort: 5063
port: 8000
protocol: TCP
targetPort: metrics
selector:
app.kubernetes.io/component: hami-webui
app.kubernetes.io/instance: hami-webui
app.kubernetes.io/name: hami-webui
sessionAffinity: None
type: NodePort

@ -1,17 +1,44 @@
FROM --platform=$BUILDPLATFORM golang:1.23.1 AS builder FROM --platform=$BUILDPLATFORM m.daocloud.io/docker.io/library/golang:1.23.1 AS builder
WORKDIR /src WORKDIR /src
ARG TARGETARCH ARG TARGETARCH
COPY . . # 设置 Go 模块代理为清华源,避免国外网络超时
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct
# 替换 APT 源为清华,并清理默认源配置
RUN set -eux; \
release=$(grep -oP '(?<=VERSION_CODENAME=).*' /etc/os-release); \
echo "Using Debian release: $release"; \
echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian $release main contrib non-free\n\
deb http://mirrors.tuna.tsinghua.edu.cn/debian $release-updates main contrib non-free\n\
deb http://mirrors.tuna.tsinghua.edu.cn/debian-security $release-security main contrib non-free" \
> /etc/apt/sources.list && \
rm -rf /etc/apt/sources.list.d/* && \
apt-get update && \
apt-get install -y --no-install-recommends protobuf-compiler && \
apt-get clean && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y --no-install-recommends protobuf-compiler COPY . .
RUN make build TARGET_ARCH=${TARGETARCH} RUN make build TARGET_ARCH=${TARGETARCH}
FROM --platform=$TARGETPLATFORM debian:stable-slim # 运行阶段镜像
FROM --platform=$TARGETPLATFORM m.daocloud.io/docker.io/library/debian:stable-slim
# 如果运行时不需要 APT这一段可以删除否则保留
RUN set -eux; \
release=$(grep -oP '(?<=VERSION_CODENAME=).*' /etc/os-release); \
echo "Using Debian release: $release"; \
echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian $release main contrib non-free\n\
deb http://mirrors.tuna.tsinghua.edu.cn/debian $release-updates main contrib non-free\n\
deb http://mirrors.tuna.tsinghua.edu.cn/debian-security $release-security main contrib non-free" \
> /etc/apt/sources.list && \
rm -rf /etc/apt/sources.list.d/* && \
apt-get update && \
apt-get clean && rm -rf /var/lib/apt/lists/*
COPY --from=builder /src/build/ /apps/ COPY --from=builder /src/build/ /apps/
COPY --from=builder /src /src COPY --from=builder /src /src

@ -56,15 +56,15 @@ build-local:
# 5. Docker image commands # 5. Docker image commands
.PHONY: build-image .PHONY: build-image
build-image: build-image:
docker build --platform linux/amd64 -t ${DOCKER_IMAGE}:${VERSION} . nerdctl -nk8s.io build --platform linux/amd64 -t ${DOCKER_IMAGE}:${VERSION} .
.PHONY: push-image .PHONY: push-image
push-image: push-image:
docker push ${DOCKER_IMAGE}:${VERSION} nerdctl -nk8s.io push ${DOCKER_IMAGE}:${VERSION}
.PHONY: save-image .PHONY: save-image
save-image: save-image:
docker save -o $(BUILD_PATH)hami_webui_release_${VERSION}.tar ${DOCKER_IMAGE}:${VERSION} nerdctl -nk8s.io save -o $(BUILD_PATH)hami_webui_release_${VERSION}.tar ${DOCKER_IMAGE}:${VERSION}
gzip -f $(BUILD_PATH)hami_webui_release_${VERSION}.tar gzip -f $(BUILD_PATH)hami_webui_release_${VERSION}.tar
# 6. Debugging and Testing # 6. Debugging and Testing

@ -0,0 +1,80 @@
#!/bin/bash
# 定义颜色输出(可选,让提示更清晰)
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# 打印错误信息并退出
error_exit() {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
# 打印普通提示
info_msg() {
echo -e "${GREEN}$1${NC}"
}
# 更新前端
update_frontend() {
info_msg "Updating frontend..."
cd /root/hami || error_exit "Failed to enter /root/hami directory"
make build-image DOCKER_IMAGE=projecthami/hami-webui-fe VERSION=dev || error_exit "Frontend build failed"
UPDATED=1
}
# 更新后端
update_backend() {
info_msg "Updating backend..."
cd /root/hami/server || error_exit "Failed to enter /root/hami/server directory"
make build-image DOCKER_IMAGE=projecthami/hami-webui-be VERSION=dev || error_exit "Backend build failed"
UPDATED=1
}
# 重启 Kubernetes 服务
restart_service() {
info_msg "Restarting service with k8s.yml..."
cd /root/hami || error_exit "Failed to enter /root/hami directory"
kubectl delete -f k8s.yml || error_exit "Failed to delete k8s resources"
sleep 2 # 可选:等待资源清理
kubectl create -f k8s.yml || error_exit "Failed to create k8s resources"
info_msg "Service restarted successfully."
}
# 显示用法
usage() {
echo "Usage: $0 {all|frontend|backend}"
exit 1
}
# 主逻辑
UPDATED=0
case "$1" in
all)
update_frontend
update_backend
;;
frontend)
update_frontend
;;
backend)
update_backend
;;
"")
echo "Error: No argument provided."
usage
;;
*)
echo "Error: Invalid argument '$1'."
usage
;;
esac
# 如果执行了任何更新,则重启服务
if [ "$UPDATED" -eq 1 ]; then
restart_service
fi
info_msg "Update and restart process finished."
Loading…
Cancel
Save