-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmakefile
107 lines (94 loc) · 3.12 KB
/
makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
SUBDIR := config
TEMPLATE_FILES := $(wildcard $(SUBDIR)/*.template)
CONFIG_FILES := $(patsubst %.template,%,$(TEMPLATE_FILES))
# 初始化项目配置信息
.PHONY: init
init:
@echo "检查配置文件..."
@for file in $(TEMPLATE_FILES); do \
new_file=$$(echo $$file | sed 's/\.template$$//'); \
if [ ! -f $$new_file ]; then \
cp $$file $$new_file; \
rm -f $$file; \
echo "生成文件 $$new_file"; \
else \
echo "文件 $$new_file 已存在,跳过生成"; \
fi \
done
@if [ ! -f token.json ]; then \
touch token.json; \
echo "生成 token.json 文件"; \
fi
# 本地运行
.PHONY: local
local:init
@# 检查是否存在 Python 3 环境
@if command -v python3 &> /dev/null; then \
echo "Python 3 已安装,运行 pip install..."; \
if pip3 install -r "$(PWD)/streamlit_app/requirements.txt" -i https://mirrors.aliyun.com/pypi/simple; then \
echo "依赖项安装成功"; \
else \
echo "依赖项安装失败,请检查错误信息"; \
exit 1; \
fi; \
else \
echo "Python 3 未安装,请先安装 Python 3"; \
exit 1; \
fi
@# 设置环境变量
@export CGO_ENABLED=1
@# 格式化 Go 代码
@if go fmt; then \
echo "Go 代码格式化成功"; \
else \
echo "Go 代码格式化失败,请检查错误信息"; \
exit 1; \
fi
@# 整理 Go 依赖
@if go mod tidy; then \
echo "依赖整理成功"; \
else \
echo "依赖整理失败,请检查错误信息"; \
exit 1; \
fi
@# 编译 Go 项目
@if go build .; then \
echo "项目构建成功"; \
else \
echo "项目构建失败,请检查错误信息"; \
exit 1; \
fi
@# 运行 Go 项目
@if ./wechat-gptbot; then \
echo "项目运行成功"; \
else \
echo "项目运行失败,请检查错误信息"; \
exit 1; \
fi
# 本地docker运行
.PHONY: brun
docker:init
@docker-compose -f build.yaml up --build -d
# 构建依赖的基础镜像并推送到远程仓库
.PHONY: base
base:
@# 创建一个名为builder的新构建器实例,并设置为当前使用的构建器
@docker buildx create --name builder --bootstrap --use
@docker buildx build --platform linux/amd64,linux/arm64 -f baseimage -t 1003941268/python3.11-alpine:latest --push .
@docker buildx rm
# 打包镜像并推送仓库
.PHONY: push
push:
@# 检查并删除已存在的构建器实例
@echo "Checking if buildx builder instance 'pushBuilder' already exists..."
@if docker buildx inspect pushBuilder > /dev/null 2>&1; then \
echo "Removing existing buildx builder instance 'pushBuilder'..."; \
docker buildx rm pushBuilder; \
fi
@# 创建一个名为pushBuilder的新构建器实例,并设置为当前使用的构建器
@echo "Creating and using new buildx builder instance 'pushBuilder'..."
docker buildx create --name pushBuilder --bootstrap --use
@# 使用 trap 确保在任务结束后删除构建器
@echo "Building and pushing Docker image..."
@trap 'echo "Cleaning up: removing buildx builder instance 'pushBuilder'"; docker buildx rm pushBuilder' EXIT INT TERM; \
docker buildx build --platform linux/amd64,linux/arm64 -f dockerfile -t 1003941268/wechat-gptbot:latest --push . || exit 1