├── LICENSE
├── README.md
└── shell
├── build-sz-admin-alone.sh
├── build-sz-admin.sh
├── common.env
├── common.sh
├── deploy-minio.sh
├── deploy-nginx.sh
├── deploy-sz-service-admin.sh
├── deploy-sz-service-websocket.sh
├── env-init.sh
└── package
├── build-web-alone.sh
├── deploy-api-alone.sh
└── deploy-api-docker.sh
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [2024] [升职哦(sz)]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
Sz-Admin
2 | 基于SpringBoot3、Vue3的轻量级脚手架
3 |
4 |
5 |
6 |
7 | # 简介
8 |
9 | > 接触了很多优秀的开源和闭源项目,在使用过程中也发现一些问题,不甘满足的我遂产生了想法:于是利用休息时间编写了一套后台管理系统,它**灵活、简洁、高效**,拥抱最新的技术,因此**Sz-Admin**便诞生了,也意为升职Admin,升职加薪节节高。
10 |
11 | **[Sz Admin](https://szadmin.cn/)** ,一个基于 Spring Boot 3、Vue 3 和 Element-Plus 的开源中后台管理框架,致力于为您提供一个流畅、直观且功能强大的开发框架。它不仅融合了最新的技术趋势,而且通过精心设计,确保了系统的简洁性和高效,让使用者可以专注业务。
12 |
13 |
14 |
15 | ## 在线体验
16 |
17 | - 官网地址:https://szadmin.cn
18 | - 文档地址:https://szadmin.cn/md/Help/doc/start.html
19 | - 预览地址:https://preview.szadmin.cn
20 | - 代码仓库:
21 | - 前端:**[sz-admin](https://github.com/feiyuchuixue/sz-admin.git)**
22 | - 后端:**[sz-boot-parent](https://github.com/feiyuchuixue/sz-boot-parent.git)**
23 | - 部署:**[sz-deploy](https://github.com/feiyuchuixue/sz-deploy.git)**
24 |
25 | ## 系统要求
26 |
27 | - JDK >= 21
28 | - MySQL >= 8.0.34
29 | - Maven >= 3.8
30 | - Node >= 16.x
31 |
32 | ## 核心技术
33 |
34 | - **SpringBoot 3.x:** 最新的Spring Boot版本,提供更优的性能和更丰富的特性。
35 | - **Sa-Token**:一个轻量级 Java 权限认证框架,简化权限认证,保障应用的安全性。
36 | - **Mybatis Flex**:一个优雅的 `MyBatis` 增强框架,它非常轻量、同时拥有极高的性能与灵活性。
37 | - **Flyway**:`数据库版本控制`工具,确保数据库迁移的可靠性。
38 | - **Knife4j**:一个为 `Swagger` 接口文档增强的工具,提供了更直观的 API 文档展示和更便捷的接口测试体验。
39 | - **Minio**:一个开源的对象存储服务,提供高性能、分布式存储解决方案,兼容 S3 API。
40 | - **HikariCP**:选择 `HikariCP` 作为 JDBC 连接池,提供快速且高效的数据库连接管理。
41 |
42 | - **Vue 3.4**:采用 `Vue 3.4`,Vue.js 的最新稳定版本,提供更强的性能和更丰富的功能,构建响应式用户界面。
43 | - **Vite 5**:使用 `Vite 5` 作为前端开发和构建工具,它利用现代浏览器的原生 ES 模块导入特性,提供了快速的冷启动和即时模块热更新。
44 | - **TypeScript**:通过 `TypeScript` 的集成,引入静态类型检查,增强了代码的可维护性和可读性,提前避免潜在的错误。
45 | - **Pinia**:状态管理采用 `Pinia`,这是 Vue 3 的解构式状态管理库,它简单、灵活且易于使用,优化了应用的状态管理。
46 | - **Element-Plus**:一个基于 Vue 3 的组件库,提供了一系列高质量的 UI 组件,帮助开发者快速构建美观、功能完备的用户界面。
47 |
48 | ## 功能列表
49 |
50 | - **账户管理**:负责管理系统用户的创建、配置及权限分配,确保用户身份的合法性和操作的合规性。
51 | - **角色管理**:实现角色与权限的精细绑定,通过角色分配简化用户权限管理,提高系统安全性和灵活性。
52 | - **菜单管理**:定制化系统导航结构,通过权限细分确保用户仅访问授权的操作界面,增强操作的直观性和可控性。
53 | - **字典管理**:维护系统内静态数据字典,如配置项、枚举值等,以统一管理和优化数据的一致性。
54 | - **参数管理**:动态调整系统运行参数,无需重启即可实时生效,提升系统响应速度和运维效率。
55 | - **客户端管理**:监管客户端接入,确保客户端的合法性和安全性,维护系统的整体稳定性。
56 | - **部门管理**:构建组织架构,通过树状结构展示,支持数据权限的层级化管理,加强信息的有序性和安全性。
57 | - **代码生成器**:自动化生成前后端代码模板,支持CRUD操作,加速开发周期,提升开发效率。
58 | - **WebSocket**:提供WebSocket支持。
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 |
--------------------------------------------------------------------------------
/shell/build-sz-admin-alone.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # 以下脚本使用 打包机和 应用机模式,此脚本在打包机中运行。
3 | # 引用公共函数和变量
4 | source ./common.sh
5 | set -e # 如果命令失败,则立即退出
6 |
7 | # 定义变量
8 | BRANCH_NAME="main" # 分支名
9 | PROJECT_NAME="sz-admin" # 项目名称
10 |
11 | PROJECT_REPO="https://gitee.com/feiyuchuixue/sz-admin.git" # git clone 地址
12 |
13 | APP_TMP_PATH="${ENV_ROOT_DIR}/${ENV_DOWNLOAD_DIR}/${PROJECT_NAME}" # 项目下载路径
14 | DEPLOY_VERSION=$(date +%Y%m%d%H%M%S) # 部署版本号
15 | APP_PACKAGE_JSON_PATH="${APP_TMP_PATH}/${DEPLOY_VERSION}" # 项目package.json路径
16 | PUBLIC_NODE_MODULES_REPOSITORY="${ENV_PUBLIC_NODE_MODULES_REPOSITORY}/${PROJECT_NAME}" # 挂载node_modules地址
17 | DEPLOY_DIR="${ENV_ROOT_DIR}/${ENV_APP_DIR}/${PROJECT_NAME}/${DEPLOY_VERSION}" # 部署文件备份地址
18 |
19 | USE_ENV_CONFIG="true"
20 | CONFIG_DIR="${ENV_ROOT_DIR}/${ENV_CONF_DIR}/${PROJECT_NAME}" # 环境变量配置文件目录
21 | BUILD_DIR="/home/build/${PROJECT_NAME}" # build后的输出地址
22 | BUILD_BACK_DIR="/home/build/${PROJECT_NAME}/back" # 历史版本记录
23 |
24 | PACKAGE_FILE="dist.zip"
25 | REMOTE_USER="root" # 应用机的用户名
26 | REMOTE_HOST="应用主机Ip" # 应用机的主机名或IP地址
27 | REMOTE_DIR="/home/tmp" # 应用机上的目标目录
28 | TARGET_DIR="/home/app/nginx/static" # 应用机上的目标目录
29 |
30 | # 主执行函数
31 | main() {
32 | local start_time=$(date +%s)
33 | # 拉取项目代码
34 | echo "========== 正在拉取项目代码 =========="
35 | mkdir -p "$APP_TMP_PATH"
36 | cd "$APP_TMP_PATH" || exit
37 | git clone -b "$BRANCH_NAME" "$PROJECT_REPO" "$DEPLOY_VERSION"
38 | cd "$DEPLOY_VERSION" || exit
39 |
40 | # 如果使用环境变量配置文件
41 | if [ "$USE_ENV_CONFIG" = "true" ]; then
42 | mkdir -p "${CONFIG_DIR}"
43 | echo "========== 替换环境变量配置文件 =========="
44 | if [ -n "$(ls -A "${CONFIG_DIR}")" ]; then
45 | cp -rf "${CONFIG_DIR}/.env.production" "${APP_PACKAGE_JSON_PATH}"
46 | else
47 | echo "${CONFIG_DIR} 目录为空,跳过复制操作"
48 | fi
49 | fi
50 |
51 | mkdir -p "$DEPLOY_DIR"
52 | mv ./nginx "$DEPLOY_DIR"
53 |
54 | echo "========== 拉取完成,开始打包 =========="
55 | # 构建项目
56 | docker run --rm \
57 | --name "$PROJECT_NAME"-node \
58 | -v "${APP_PACKAGE_JSON_PATH}":/app \
59 | -v "${PUBLIC_NODE_MODULES_REPOSITORY}":/app/node_modules \
60 | -w /app \
61 | node:21 \
62 | sh -c "npm install -g pnpm && pnpm install && pnpm run build"
63 | echo "========== 打包完成 =========="
64 | ls -l
65 | pwd
66 | mkdir -p "$BUILD_DIR"
67 | rm -rf "$BUILD_DIR/dist"
68 | mkdir -p "$BUILD_BACK_DIR/$DEPLOY_VERSION"
69 | cp -rf "${APP_PACKAGE_JSON_PATH}/dist" "$BUILD_BACK_DIR/$DEPLOY_VERSION"
70 | # 移动dist目录到部署目录
71 | mv "${APP_PACKAGE_JSON_PATH}/dist" "$BUILD_DIR"
72 | echo "=============压缩================"
73 | cd "$BUILD_DIR"
74 | zip -r dist.zip "./dist" > /dev/null
75 |
76 | # 提示用户输入密码
77 | read -s -p "Enter password for $REMOTE_USER@$REMOTE_HOST: " REMOTE_PASS
78 | echo
79 |
80 | # 将文件传输到应用机
81 | echo "Transferring package to remote host..."
82 | sshpass -p "$REMOTE_PASS" scp "$PACKAGE_FILE" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR"
83 |
84 | # 在应用机上解压文件,并将文件复制到目标目录
85 | echo "Cleaning up target directory on remote host..."
86 | sshpass -p "$REMOTE_PASS" ssh "$REMOTE_USER@$REMOTE_HOST" "rm -rf $TARGET_DIR/$PROJECT_NAME"
87 |
88 | echo "Extracting package on remote host..."
89 | sshpass -p "$REMOTE_PASS" ssh "$REMOTE_USER@$REMOTE_HOST" "unzip -o $REMOTE_DIR/$PACKAGE_FILE -d $REMOTE_DIR > /dev/null"
90 |
91 | echo "Copying extracted files to target directory..."
92 | sshpass -p "$REMOTE_PASS" ssh "$REMOTE_USER@$REMOTE_HOST" "mv $REMOTE_DIR/dist $TARGET_DIR/$PROJECT_NAME"
93 |
94 | # 可选:删除应用机上的压缩包
95 | echo "Cleaning up..."
96 | sshpass -p "$REMOTE_PASS" ssh "$REMOTE_USER@$REMOTE_HOST" "rm $REMOTE_DIR/$PACKAGE_FILE"
97 |
98 | echo "Deployment complete!"
99 |
100 | echo "========== 清理 =========="
101 | # 删除源代码文件
102 | rm -rf "$APP_PACKAGE_JSON_PATH"
103 |
104 | local end_time=$(date +%s)
105 | local duration=$((end_time - start_time))
106 | echo "==================== 整体执行耗时为: ${duration} 秒===================="
107 |
108 | }
109 |
110 | # 执行主函数
111 | main "$@"
112 |
--------------------------------------------------------------------------------
/shell/build-sz-admin.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # 引用公共函数和变量
3 | source ./common.sh
4 | set -e # 如果命令失败,则立即退出
5 |
6 | # 定义变量
7 | BRANCH_NAME="main" # 分支名
8 | PROJECT_NAME="sz-admin" # 项目名称
9 |
10 | USE_GIT_SSH=true # 是否使用Git SSH
11 | GIT_PATH="feiyuchuixue/sz-admin.git" # git项目路径
12 | GIT_PATH_SSH="feiyuchuixue/sz-admin.git" # git项目路径 # 是否使用git(github) ssh
13 | PROJECT_REPO="${ENV_GIT_PROTOCOL}://${ENV_GIT_USERNAME}:${ENV_GIT_PASSWORD}@${ENV_GIT_HOST}/${GIT_PATH}" # git clone 地址
14 | PROJECT_REPO_SSH="git@${ENV_GIT_HOST_SSH}:$GIT_PATH_SSH"
15 |
16 | USE_NPM_PROXY=true # 是否使用npm镜像代理
17 | NPM_CONFIG_REGISTRY="https://mirrors.cloud.tencent.com/npm/" # npm镜像加速地址
18 |
19 | APP_TMP_PATH="${ENV_ROOT_DIR}/${ENV_DOWNLOAD_DIR}/${PROJECT_NAME}" # 项目下载路径
20 | DEPLOY_VERSION=$(date +%Y%m%d%H%M%S) # 部署版本号
21 | APP_PACKAGE_JSON_PATH="${APP_TMP_PATH}/${DEPLOY_VERSION}" # 项目package.json路径
22 | PUBLIC_NODE_MODULES_REPOSITORY="${ENV_PUBLIC_NODE_MODULES_REPOSITORY}/${PROJECT_NAME}" # 挂载node_modules地址
23 | DEPLOY_DIR="${ENV_ROOT_DIR}/${ENV_APP_DIR}/${PROJECT_NAME}/${DEPLOY_VERSION}" # 部署文件备份地址
24 |
25 | USE_ENV_CONFIG="true"
26 | CONFIG_DIR="${ENV_ROOT_DIR}/${ENV_CONF_DIR}/${PROJECT_NAME}" # 环境变量配置文件目录
27 |
28 | # 主执行函数
29 | main() {
30 | local start_time=$(date +%s)
31 | # 拉取项目代码
32 | echo "========== 正在拉取项目代码 =========="
33 | mkdir -p "$APP_TMP_PATH"
34 | cd "$APP_TMP_PATH" || exit
35 |
36 | # 如果使用github ssh clone 需要设置认证密钥。这里设置的认证密钥目录在~/.ssh/id_rsa下
37 | if [ "$USE_GIT_SSH" = "true" ]; then
38 | git config --global core.sshCommand "ssh -i ~/.ssh/id_rsa"
39 | # 将PROJECT_REPO_SSH 的值赋值给 PROJECT_REPO
40 | PROJECT_REPO="$PROJECT_REPO_SSH"
41 | fi
42 | git clone -b "$BRANCH_NAME" "$PROJECT_REPO" "$DEPLOY_VERSION"
43 | cd "$DEPLOY_VERSION" || exit
44 |
45 | # 如果使用环境变量配置文件
46 | if [ "$USE_ENV_CONFIG" = "true" ]; then
47 | mkdir -p "${CONFIG_DIR}"
48 | echo "========== 替换环境变量配置文件 =========="
49 | if [ -n "$(ls -A "${CONFIG_DIR}")" ]; then
50 | cp -rf "${CONFIG_DIR}/.env.production" "${APP_PACKAGE_JSON_PATH}"
51 | else
52 | echo "${CONFIG_DIR} 目录为空,跳过复制操作"
53 | fi
54 | fi
55 |
56 | mkdir -p "$DEPLOY_DIR"
57 | mv ./nginx "$DEPLOY_DIR"
58 |
59 | echo "========== 拉取完成,开始打包 =========="
60 | # 检查环境变量USE_NPM_PROXY的值
61 | if [ "$USE_NPM_PROXY" == "true" ]; then
62 | # 如果USE_NPM_PROXY为true,则使用npm代理
63 | local proxy_arg="-e NPM_CONFIG_REGISTRY=${NPM_CONFIG_REGISTRY}"
64 | else
65 | # 如果USE_NPM_PROXY不为true,则不使用npm代理
66 | local proxy_arg=""
67 | fi
68 | # 构建项目
69 | docker run --rm \
70 | --name "$PROJECT_NAME"-node \
71 | "$proxy_arg" \
72 | -v "${APP_PACKAGE_JSON_PATH}":/app \
73 | -v "${PUBLIC_NODE_MODULES_REPOSITORY}":/app/node_modules \
74 | -w /app \
75 | node:21 \
76 | sh -c "npm install -g pnpm && pnpm install && pnpm run build"
77 | echo "========== 打包完成 =========="
78 | ls -l
79 | pwd
80 | # 移动dist目录到部署目录
81 | mv "${APP_PACKAGE_JSON_PATH}/dist" "$DEPLOY_DIR"
82 | cd "$DEPLOY_DIR"
83 | echo "DEPLOY_DIR == $DEPLOY_DIR"
84 | ls -l
85 | mkdir -p "$ENV_NGINX_STATIC_DIR/$PROJECT_NAME"
86 | rm -rf "$ENV_NGINX_STATIC_DIR/$PROJECT_NAME" # 删除之前的静态资源路径
87 | cp -rf "./dist" "$ENV_NGINX_STATIC_DIR/$PROJECT_NAME" # 复制静态资源到nginx路径下
88 |
89 | mkdir -p "$ENV_NGINX_CNF_DIR"
90 | find "./nginx" -type f -name "*.conf" -exec cp -f {} "$ENV_NGINX_CNF_DIR"/ \;
91 |
92 | echo "========== 清理 =========="
93 | # 删除源代码文件
94 | rm -rf "$APP_PACKAGE_JSON_PATH"
95 |
96 | local end_time=$(date +%s)
97 | local duration=$((end_time - start_time))
98 | echo "==================== 整体执行耗时为: ${duration} 秒===================="
99 |
100 | # 查看新应用容器的日志
101 | echo "==================== 查看日志 ===================="
102 | tail -f "$ENV_NGINX_LOG_DIR/default.access.log"
103 | echo "==================== 部署完成 ===================="
104 | }
105 |
106 | # 执行主函数
107 | main "$@"
108 |
--------------------------------------------------------------------------------
/shell/common.env:
--------------------------------------------------------------------------------
1 | # deploy 环境变量
2 | export ENV_ROOT_DIR="/home";
3 | export ENV_APP_DIR="app";
4 | export ENV_DOWNLOAD_DIR="download";
5 | export ENV_CONF_DIR="conf";
6 | export ENV_DOCKER_NETWORK_NAME="sz";
7 |
8 | export ENV_GIT_USERNAME="账户";
9 | export ENV_GIT_PASSWORD="密码";
10 | export ENV_GIT_HOST="服务器HOST地址";
11 | export ENV_GIT_PROTOCOL="http";
12 | export ENV_GIT_HOST_SSH="github.com";
13 |
14 | export ENV_NGINX_ROOT_DIR="$ENV_ROOT_DIR/$ENV_APP_DIR/nginx"; # nginx 根路径
15 | export ENV_NGINX_LOG_DIR="$ENV_NGINX_ROOT_DIR/logs"; # nginx 日志目录
16 | export ENV_NGINX_STATIC_DIR="$ENV_NGINX_ROOT_DIR/static"; # nginx 静态资源目录
17 | export ENV_NGINX_CNF_DIR="$ENV_NGINX_ROOT_DIR/conf.d"; # nginx conf 配置文件目录
18 | export ENV_NGINX_CERT_DIR="$ENV_NGINX_ROOT_DIR/cert"; # nginx ssl证书目录
19 |
20 | export ENV_PUBLIC_MAVEN_REPOSITORY="$ENV_ROOT_DIR/repository/mvn"; # maven 仓库路径(项目隔离)
21 | export ENV_PUBLIC_NODE_MODULES_REPOSITORY="$ENV_ROOT_DIR/repository/node_modules"; # node_modules 模块仓库路径(项目隔离)
--------------------------------------------------------------------------------
/shell/common.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # 引用环境变量
3 | source ./common.env
4 | # 检查 Docker 网络是否存在
5 | function common_check_docker_network {
6 | # 检查网络是否存在
7 | if docker network ls --format '{{.Name}}' | grep -q "^${ENV_DOCKER_NETWORK_NAME}$"; then
8 | echo "检查 Docker 网络 ${ENV_DOCKER_NETWORK_NAME} 已存在"
9 | else
10 | # 创建网络
11 | echo "Docker 网络不存在,创建 ${ENV_DOCKER_NETWORK_NAME}"
12 | docker network create "${ENV_DOCKER_NETWORK_NAME}"
13 | fi
14 | }
15 |
16 |
--------------------------------------------------------------------------------
/shell/deploy-minio.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # 引用公共函数和变量
4 | source ./common.sh
5 | set -e # 如果命令失败,则立即退出
6 |
7 | TZ="Asia/Shanghai" # 时区
8 |
9 | PROJECT_NAME="minio" # 项目名称
10 | ROOT_USER="用户名" # minio 用户名1
11 | ROOT_PWD="密码" # minio 密码
12 | DATA_DIR="/mnt/minio/data" # minio文件存储磁盘
13 | CONFIG_DIR="/mnt/minio/config" # minio配置路径
14 |
15 | WEB_PORT="9000" # minio API
16 | SERVER_PORT="9001" # minio控制台
17 | MINIO_DOMAIN="https://your.domain.com" # minio API域名
18 | MINIO_DOMAIN_BROWSER="https://your.domain.com" # minio 域名
19 |
20 | echo "========== 更新服务器时间 =========="
21 | # 1. 更新服务器时间
22 | # 更新时区
23 | yum -y install ntpdate
24 | # 向阿里云服务器同步时间
25 | ntpdate time1.aliyun.com
26 | # 删除本地时间并设置时区为上海
27 | rm -rf /etc/localtime
28 | ln -s /usr/share/zoneinfo/"$TZ" /etc/localtime
29 | echo "========== 当前服务器时间 =========="
30 | # 查看时间
31 | date -R || date
32 |
33 | echo "========== 创建磁盘映射文件夹 =========="
34 | # 2. 创建磁盘映射文件夹
35 | mkdir -p "$DATA_DIR"
36 | mkdir -p "$CONFIG_DIR"
37 |
38 | echo "========== 下载minio =========="
39 | # 3. docker 下载 minio
40 | docker pull minio/minio
41 |
42 | echo "========== 运行minio =========="
43 | # 4. 运行minio镜像,设置账户密码,映射
44 | docker run -d \
45 | --name $PROJECT_NAME \
46 | -p "$WEB_PORT":"$WEB_PORT" \
47 | -p "$SERVER_PORT":"$SERVER_PORT" \
48 | --privileged=true \
49 | -e "MINIO_ROOT_USER=$ROOT_USER" \
50 | -e "MINIO_ROOT_PASSWORD=$ROOT_PWD" \
51 | -e "MINIO_SERVER_URL=$MINIO_DOMAIN" \
52 | -e "MINIO_BROWSER_REDIRECT_URL=$MINIO_DOMAIN_BROWSER" \
53 | -e "TZ=$TZ" \
54 | -v "$DATA_DIR":/data \
55 | -v "$CONFIG_DIR":/root/.minio \
56 | minio/minio server \
57 | --console-address ":$SERVER_PORT" \
58 | --address ":$WEB_PORT" /data
59 |
60 | echo "========== 加入minio网络 =========="
61 | # 检查Docker网络
62 | common_check_docker_network
63 | # 连接Docker网络
64 | docker network connect "$ENV_DOCKER_NETWORK_NAME" "$PROJECT_NAME"
65 | echo "========== minio 日志 =========="
66 | docker logs -f minio
67 |
--------------------------------------------------------------------------------
/shell/deploy-nginx.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # 引用公共函数和变量
3 | source ./common.sh
4 | set -e # 如果命令失败,则立即退出
5 |
6 | # 映射端口变量
7 | PORT_MAPPINGS=(
8 | "9800:9800"
9 | "80:80"
10 | "443:443"
11 | )
12 |
13 | PROJECT_NAME="nginx" # 项目名称
14 | USE_ENV_CONFIG=true # 是否使用环境变量配置文件
15 | CONFIG_DIR="${ENV_ROOT_DIR}/${ENV_CONF_DIR}/${PROJECT_NAME}" # 环境变量配置文件目录
16 |
17 | # 平滑重载NGINX配置
18 | reload_nginx() {
19 | echo "==================== 平滑重载NGINX配置 ===================="
20 | docker exec "$PROJECT_NAME" nginx -s reload
21 | }
22 |
23 | # 获取NGINX容器的状态
24 | get_container_status() {
25 | docker inspect -f '{{.State.Running}}' "$PROJECT_NAME" 2>/dev/null || echo "false"
26 | }
27 |
28 | run_nginx() {
29 | echo "==================== 启动新应用容器 ===================="
30 | docker run -itd \
31 | --name "$PROJECT_NAME" \
32 | --restart always \
33 | $(printf -- ' -p %s' "${PORT_MAPPINGS[@]}") \
34 | -e TZ=Asia/Shanghai \
35 | -v "${ENV_NGINX_LOG_DIR}":/var/log/nginx \
36 | -v "${ENV_NGINX_STATIC_DIR}":/usr/share/nginx \
37 | -v "${ENV_NGINX_CNF_DIR}":/etc/nginx/conf.d \
38 | -v "${ENV_NGINX_CERT_DIR}":/etc/nginx/cert \
39 | nginx:1.25.4
40 |
41 | echo "==================== 加入网络 ===================="
42 | # 加入网络
43 | docker network connect "$ENV_DOCKER_NETWORK_NAME" "$PROJECT_NAME"
44 | }
45 |
46 | # 检查并删除同名容器
47 | check_and_remove_duplicate_container() {
48 | local duplicate_container_id
49 | duplicate_container_id=$(docker ps -aqf "name=$PROJECT_NAME")
50 | if [ -n "$duplicate_container_id" ]; then
51 | echo "发现同名容器 $PROJECT_NAME,删除中..."
52 | docker stop "$PROJECT_NAME" >/dev/null
53 | docker rm "$PROJECT_NAME" >/dev/null
54 | echo "同名容器 $PROJECT_NAME 已删除"
55 | fi
56 | }
57 |
58 | ask_restart_container() {
59 | # 交互式询问用户是否认为端口发生了改变
60 | read -p "是否需要重启容器?(y/n): " answer
61 | case "$answer" in
62 | [Yy]*)
63 | echo "重新启动容器"
64 | check_and_remove_duplicate_container
65 | run_nginx
66 | ;;
67 | [Nn]*)
68 | echo "不执行任何操作"
69 | ;;
70 | *)
71 | echo "无效输入,请输入 y 或 n"
72 | ;;
73 | esac
74 | }
75 |
76 | check_use_env_config() {
77 | # 如果使用环境变量配置文件
78 | if [ "$USE_ENV_CONFIG" = "true" ]; then
79 | mkdir -p "${CONFIG_DIR}"
80 | mkdir -p "${ENV_NGINX_CNF_DIR}"
81 | echo "========== 替换环境变量配置文件 =========="
82 | if [ -n "$(ls -A "${CONFIG_DIR}")" ]; then
83 | cp -rf "${CONFIG_DIR}"/* "${ENV_NGINX_CNF_DIR}"
84 | else
85 | echo "${CONFIG_DIR} 目录为空,跳过复制操作"
86 | fi
87 | fi
88 | }
89 |
90 | # 如果NGINX容器不存在或者处于非运行状态,执行初始化操作
91 | if [ "$(get_container_status)" != "true" ]; then
92 | mkdir -p "$ENV_NGINX_LOG_DIR"
93 | mkdir -p "$ENV_NGINX_STATIC_DIR"
94 | mkdir -p "$ENV_NGINX_CNF_DIR"
95 | mkdir -p "$ENV_NGINX_CERT_DIR"
96 |
97 | echo "NGINX容器不存在或者处于非运行状态,执行初始化操作"
98 | echo "==================== 删除旧镜像 ===================="
99 | docker image prune -f
100 |
101 | check_and_remove_duplicate_container
102 | check_use_env_config
103 | run_nginx
104 | echo "==================== 打印nginx日志 ===================="
105 | docker logs -f "$PROJECT_NAME"
106 |
107 | else
108 | echo "NGINX容器存在且处于运行状态"
109 | echo "NGINX平滑重载"
110 | check_use_env_config
111 | reload_nginx
112 | ask_restart_container
113 | fi
114 |
115 | echo "部署完成"
116 |
--------------------------------------------------------------------------------
/shell/deploy-sz-service-admin.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # 引用公共函数和变量
4 | source ./common.sh
5 |
6 | set -e # 如果命令失败,则立即退出
7 |
8 | BRANCH_NAME="main" #分支名
9 | PROJECT_NAME="sz-service-admin"
10 | PORT=9991 # 服务端口
11 | PROFILE_ACTIVE="preview" # java -jar -Dspring.profiles.active=dev java部署环境
12 | # 根磁盘路径
13 | APP_TMP_PATH="${ENV_ROOT_DIR}/${ENV_DOWNLOAD_DIR}/${PROJECT_NAME}" #项目下载路径
14 |
15 | DEPLOY_VERSION=$(date +%Y%m%d%H%M%S)
16 | APP_POM_PATH="${APP_TMP_PATH}/${DEPLOY_VERSION}"
17 | APP_TARGET_PATH="${APP_POM_PATH}/sz-service/sz-service-admin/target"
18 | APP_CONFIG_PATH="${APP_POM_PATH}/sz-service/sz-service-admin/src/main/resources/config" # 子级配置文件目录
19 | DEPLOY_DIR="${ENV_ROOT_DIR}/${ENV_APP_DIR}/${PROJECT_NAME}/${DEPLOY_VERSION}" # 部署文件备份地址
20 | LOG_DIR="${ENV_ROOT_DIR}/${ENV_APP_DIR}/${PROJECT_NAME}/logs"
21 |
22 | USE_GIT_SSH=true # 是否使用git(github) ssh
23 | GIT_PATH="dev/sz-boot-parent.git" # git项目路径
24 | GIT_PATH_SSH="feiyuchuixue/sz-boot-parent.git" # git项目路径
25 | PROJECT_REPO="${ENV_GIT_PROTOCOL}://${ENV_GIT_USERNAME}:${ENV_GIT_PASSWORD}@${ENV_GIT_HOST}/${GIT_PATH}" # git clone 地址
26 | PROJECT_REPO_SSH="git@${ENV_GIT_HOST_SSH}:$GIT_PATH_SSH"
27 |
28 | # !!!docker 将config目录做了映射,必须手动指定宿主机的配置文件路径
29 | #USE_ENV_CONFIG=true # 是否使用环境变量配置文件
30 | CONFIG_DIR="${ENV_ROOT_DIR}/${ENV_CONF_DIR}/${PROJECT_NAME}" # 环境变量配置文件目录
31 | main() {
32 | local start_time=$(date +%s)
33 | echo "========== 正在拉取项目代码 =========="
34 | mkdir -p "$APP_TMP_PATH"
35 | cd "$APP_TMP_PATH" || exit
36 |
37 | # 如果使用github ssh clone 需要设置认证密钥。这里设置的认证密钥目录在~/.ssh/id_rsa下
38 | if [ "$USE_GIT_SSH" = "true" ]; then
39 | git config --global core.sshCommand "ssh -i ~/.ssh/id_rsa"
40 | # 将PROJECT_REPO_SSH 的值赋值给 PROJECT_REPO
41 | PROJECT_REPO="$PROJECT_REPO_SSH"
42 | fi
43 | git clone -b "$BRANCH_NAME" "$PROJECT_REPO" "$DEPLOY_VERSION"
44 | cd "$APP_POM_PATH" || exit
45 |
46 | # 如果使用环境变量配置文件
47 | # if [ "$USE_ENV_CONFIG" = "true" ]; then
48 | # mkdir -p "${CONFIG_DIR}"
49 | # echo "========== 替换环境变量配置文件 =========="
50 | # if [ -n "$(ls -A "${CONFIG_DIR}")" ]; then
51 | # cp -rf "${CONFIG_DIR}"/* "${APP_CONFIG_PATH}"
52 | # else
53 | # echo "${CONFIG_DIR} 目录为空,跳过复制操作"
54 | # fi
55 | # fi
56 |
57 | mkdir -p "${DEPLOY_DIR}"
58 | # 移动Dockerfile到部署目录
59 | mv Dockerfile "${DEPLOY_DIR}"
60 |
61 | echo "========== 拉取完成,开始打包 =========="
62 | docker run --rm \
63 | --name "$PROJECT_NAME"-maven \
64 | -v "${APP_POM_PATH}":/app \
65 | -v "${ENV_PUBLIC_MAVEN_REPOSITORY}/${PROJECT_NAME}":/custom-maven-repo \
66 | maven:3.9.6 \
67 | mvn -f /app clean package -Dmaven.repo.local=/custom-maven-repo -Dmaven.test.skip=true
68 | # 移动jar包到部署目录
69 | mv "${APP_TARGET_PATH}"/*.jar "${DEPLOY_DIR}"
70 | # 删除源代码文件
71 | rm -rf "$APP_POM_PATH"
72 |
73 | echo "========== 打包完成 =========="
74 | # 切换到Dockerfile目录
75 | cd "$DEPLOY_DIR" || exit
76 |
77 | echo "==================== 删除旧镜像 ===================="
78 | docker image prune -f
79 | docker builder prune -f
80 |
81 | echo "==================== 构建 Docker 镜像 ===================="
82 | docker build -t "$PROJECT_NAME" "$DEPLOY_DIR"
83 |
84 | echo "==================== 停止旧应用容器 ===================="
85 | docker stop "$PROJECT_NAME" || true
86 | docker rm "$PROJECT_NAME" || true
87 |
88 | echo "==================== 启动新应用容器 ===================="
89 | docker run -itd \
90 | --name "$PROJECT_NAME" \
91 | --restart always \
92 | -p "$PORT":"$PORT" \
93 | -v "$LOG_DIR":/logs \
94 | -v "$CONFIG_DIR":/config \
95 | -e "SPRING_PROFILES_ACTIVE=$PROFILE_ACTIVE" \
96 | "$PROJECT_NAME"
97 | # 检查Docker网络
98 | common_check_docker_network
99 | # 连接Docker网络
100 | docker network connect "$ENV_DOCKER_NETWORK_NAME" "$PROJECT_NAME"
101 | local end_time=$(date +%s)
102 | local duration=$((end_time - start_time))
103 | echo "==================== 整体执行耗时为: ${duration} 秒===================="
104 |
105 | echo "==================== 查看日志 ===================="
106 | docker logs -f "$PROJECT_NAME"
107 | }
108 |
109 | # 执行主函数
110 | main "$@"
111 |
--------------------------------------------------------------------------------
/shell/deploy-sz-service-websocket.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # 引用公共函数和变量
4 | source ./common.sh
5 |
6 | set -e # 如果命令失败,则立即退出
7 |
8 | BRANCH_NAME="main" #分支名
9 | PROJECT_NAME="sz-service-websocket"
10 | PORT=9993 # 服务端口
11 | PROFILE_ACTIVE="preview" # java -jar -Dspring.profiles.active=dev java部署环境
12 | # 根磁盘路径
13 | APP_TMP_PATH="${ENV_ROOT_DIR}/${ENV_DOWNLOAD_DIR}/${PROJECT_NAME}" #项目下载路径
14 |
15 | DEPLOY_VERSION=$(date +%Y%m%d%H%M%S)
16 | APP_POM_PATH="${APP_TMP_PATH}/${DEPLOY_VERSION}"
17 | APP_TARGET_PATH="${APP_POM_PATH}/sz-service/sz-service-websocket/target"
18 | APP_CONFIG_PATH="${APP_POM_PATH}/sz-service/sz-service-websocket/src/main/resources/config" # 子级配置文件目录
19 | DEPLOY_DIR="${ENV_ROOT_DIR}/${ENV_APP_DIR}/${PROJECT_NAME}/${DEPLOY_VERSION}" # 部署文件备份地址
20 | LOG_DIR="${ENV_ROOT_DIR}/${ENV_APP_DIR}/${PROJECT_NAME}/logs"
21 |
22 | USE_GIT_SSH=true # 是否使用git(github) ssh
23 | GIT_PATH="dev/sz-boot-parent.git" # git项目路径
24 | GIT_PATH_SSH="feiyuchuixue/sz-boot-parent.git" # git项目路径
25 | PROJECT_REPO="${ENV_GIT_PROTOCOL}://${ENV_GIT_USERNAME}:${ENV_GIT_PASSWORD}@${ENV_GIT_HOST}/${GIT_PATH}" # git clone 地址
26 | PROJECT_REPO_SSH="git@${ENV_GIT_HOST_SSH}:$GIT_PATH_SSH"
27 |
28 | USE_ENV_CONFIG=true # 是否使用环境变量配置文件
29 | CONFIG_DIR="${ENV_ROOT_DIR}/${ENV_CONF_DIR}/${PROJECT_NAME}" # 环境变量配置文件目录
30 | main() {
31 | local start_time=$(date +%s)
32 | echo "========== 正在拉取项目代码 =========="
33 | mkdir -p "$APP_TMP_PATH"
34 | cd "$APP_TMP_PATH" || exit
35 |
36 | # 如果使用github ssh clone 需要设置认证密钥。这里设置的认证密钥目录在~/.ssh/id_rsa下
37 | if [ "$USE_GIT_SSH" = "true" ]; then
38 | git config --global core.sshCommand "ssh -i ~/.ssh/id_rsa"
39 | # 将PROJECT_REPO_SSH 的值赋值给 PROJECT_REPO
40 | PROJECT_REPO="$PROJECT_REPO_SSH"
41 | fi
42 | git clone -b "$BRANCH_NAME" "$PROJECT_REPO" "$DEPLOY_VERSION"
43 | cd "$APP_POM_PATH" || exit
44 |
45 | # 如果使用环境变量配置文件
46 | if [ "$USE_ENV_CONFIG" = "true" ]; then
47 | mkdir -p "${CONFIG_DIR}"
48 | echo "========== 替换环境变量配置文件 =========="
49 | if [ -n "$(ls -A "${CONFIG_DIR}")" ]; then
50 | cp -rf "${CONFIG_DIR}"/* "${APP_CONFIG_PATH}"
51 | else
52 | echo "${CONFIG_DIR} 目录为空,跳过复制操作"
53 | fi
54 | fi
55 |
56 | mkdir -p "${DEPLOY_DIR}"
57 | # 移动Dockerfile到部署目录
58 | mv Dockerfile "${DEPLOY_DIR}"
59 |
60 | echo "========== 拉取完成,开始打包 =========="
61 | docker run --rm \
62 | --name "$PROJECT_NAME"-maven \
63 | -v "${APP_POM_PATH}":/app \
64 | -v "${ENV_PUBLIC_MAVEN_REPOSITORY}/${PROJECT_NAME}":/custom-maven-repo \
65 | maven:3.9.6 \
66 | mvn -f /app clean package -Dmaven.repo.local=/custom-maven-repo -Dmaven.test.skip=true
67 | # 移动jar包到部署目录
68 | mv "${APP_TARGET_PATH}"/*.jar "${DEPLOY_DIR}"
69 | # 删除源代码文件
70 | rm -rf "$APP_POM_PATH"
71 |
72 | echo "========== 打包完成 =========="
73 | # 切换到Dockerfile目录
74 | cd "$DEPLOY_DIR" || exit
75 |
76 | echo "==================== 删除旧镜像 ===================="
77 | docker image prune -f
78 | docker builder prune -f
79 |
80 | echo "==================== 构建 Docker 镜像 ===================="
81 | docker build -t "$PROJECT_NAME" "$DEPLOY_DIR"
82 |
83 | echo "==================== 停止旧应用容器 ===================="
84 | docker stop "$PROJECT_NAME" || true
85 | docker rm "$PROJECT_NAME" || true
86 |
87 | echo "==================== 启动新应用容器 ===================="
88 | docker run -itd \
89 | --name "$PROJECT_NAME" \
90 | --restart always \
91 | -p "$PORT":"$PORT" \
92 | -v "$LOG_DIR":/logs \
93 | -e "SPRING_PROFILES_ACTIVE=$PROFILE_ACTIVE" \
94 | "$PROJECT_NAME"
95 | # 检查Docker网络
96 | common_check_docker_network
97 | # 连接Docker网络
98 | docker network connect "$ENV_DOCKER_NETWORK_NAME" "$PROJECT_NAME"
99 | local end_time=$(date +%s)
100 | local duration=$((end_time - start_time))
101 | echo "==================== 整体执行耗时为: ${duration} 秒===================="
102 |
103 | echo "==================== 查看日志 ===================="
104 | docker logs -f "$PROJECT_NAME"
105 | }
106 |
107 | # 执行主函数
108 | main "$@"
109 |
--------------------------------------------------------------------------------
/shell/env-init.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # 环境初始化
4 | # 定义变量,用于标识是否需要配置docker代理
5 | USE_DOCKER_PROXY="false"
6 | DOCKER_PROXY="http://192.168.124.7:7890/"
7 | echo "Docker proxy is set to $DOCKER_PROXY"
8 |
9 | echo "========== 安装git =========="
10 | # 1. 安装git
11 | yum install -y git
12 |
13 | echo "========== 安装docker =========="
14 | # 2. 安装docker
15 | sudo yum install -y yum-utils
16 | sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
17 | sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
18 |
19 | # 3. 如果需要配置docker代理地址
20 | # 判断是否需要docker代理
21 | if [ "$USE_DOCKER_PROXY" = "true" ]; then
22 | echo "========== 配置docker代理 =========="
23 | # 设置docker代理
24 | sudo mkdir -p /etc/systemd/system/docker.service.d
25 | sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf <<-EOF
26 | [Service]
27 | Environment="HTTP_PROXY=${DOCKER_PROXY}" "HTTPS_PROXY=${DOCKER_PROXY}" "NO_PROXY=localhost,127.0.0.1,::1,/var/run/docker.sock"
28 | EOF
29 | fi
30 |
31 | echo "========== 启动docker =========="
32 | # 4. 启动docker服务
33 | sudo systemctl daemon-reload
34 | sudo systemctl restart docker
35 |
36 | echo "========== 配置docker开机自启 =========="
37 | # 设置docker开机自启
38 | sudo systemctl enable docker.service
39 | sudo systemctl enable containerd.service
40 |
41 | echo "========== deploy依赖初始环境完成 =========="
42 |
--------------------------------------------------------------------------------
/shell/package/build-web-alone.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # 以下脚本使用 打包机和 应用机模式,此脚本在打包机中运行。
3 | # 引用公共函数和变量
4 | set -e # 如果命令失败,则立即退出
5 |
6 | ENV_ROOT_DIR="/home"
7 | ENV_APP_DIR="app"
8 | ENV_DOWNLOAD_DIR="download"
9 |
10 | ENV_GIT_USERNAME="你的git账户"
11 | ENV_GIT_PASSWORD="你的git密码"
12 | ENV_GIT_HOST="你的git地址host" # 如gitee.com
13 | ENV_GIT_PROTOCOL="https"
14 | GIT_PATH="你的仓库地址" # 如feiyuchuixue/sz-admin.git
15 |
16 | # 定义变量
17 | BRANCH_NAME="main" # 分支名
18 | PROJECT_NAME="sz-admin" # 项目名称
19 |
20 | PROJECT_REPO="${ENV_GIT_PROTOCOL}://${ENV_GIT_USERNAME}:${ENV_GIT_PASSWORD}@${ENV_GIT_HOST}/${GIT_PATH}" # git clone 地址
21 |
22 | APP_TMP_PATH="${ENV_ROOT_DIR}/${ENV_DOWNLOAD_DIR}/${PROJECT_NAME}" # 项目下载路径
23 | DEPLOY_VERSION=$(date +%Y%m%d%H%M%S) # 部署版本号
24 | APP_PACKAGE_JSON_PATH="${APP_TMP_PATH}/${DEPLOY_VERSION}" # 项目package.json路径
25 | PUBLIC_NODE_MODULES_REPOSITORY="${ENV_PUBLIC_NODE_MODULES_REPOSITORY}/${PROJECT_NAME}" # 挂载node_modules地址
26 | DEPLOY_DIR="${ENV_ROOT_DIR}/${ENV_APP_DIR}/${PROJECT_NAME}/${DEPLOY_VERSION}" # 部署文件备份地址
27 |
28 | USE_ENV_CONFIG="true"
29 | CONFIG_DIR="${ENV_ROOT_DIR}/${ENV_CONF_DIR}/${PROJECT_NAME}" # 环境变量配置文件目录
30 | BUILD_DIR="/home/build/${PROJECT_NAME}" # build后的输出地址
31 | BUILD_BACK_DIR="/home/build/${PROJECT_NAME}/back" # 历史版本记录
32 |
33 | PACKAGE_FILE="dist.zip"
34 | REMOTE_USER="root" # 应用机的用户名
35 | REMOTE_HOST="你的主机IP" # 应用机的主机名或IP地址
36 | REMOTE_DIR="/home/tmp" # 应用机上的目标目录
37 | TARGET_DIR="/home/app/nginx/static" # 应用机上的目标目录
38 |
39 | # 主执行函数
40 | main() {
41 | local start_time=$(date +%s)
42 | # 拉取项目代码
43 | echo "========== 正在拉取项目代码 =========="
44 | mkdir -p "$APP_TMP_PATH"
45 | cd "$APP_TMP_PATH" || exit
46 | git clone -b "$BRANCH_NAME" "$PROJECT_REPO" "$DEPLOY_VERSION"
47 | cd "$DEPLOY_VERSION" || exit
48 |
49 | # 如果使用环境变量配置文件
50 | if [ "$USE_ENV_CONFIG" = "true" ]; then
51 | mkdir -p "${CONFIG_DIR}"
52 | echo "========== 替换环境变量配置文件 =========="
53 | if [ -n "$(ls -A "${CONFIG_DIR}")" ]; then
54 | cp -rf "${CONFIG_DIR}/.env.production" "${APP_PACKAGE_JSON_PATH}"
55 | else
56 | echo "${CONFIG_DIR} 目录为空,跳过复制操作"
57 | fi
58 | fi
59 |
60 | mkdir -p "$DEPLOY_DIR"
61 | mv ./nginx "$DEPLOY_DIR"
62 |
63 | echo "========== 拉取完成,开始打包 =========="
64 | # 构建项目
65 | # npm加速有两种方式:
66 | # 1. 使用国内镜像源 npm config set registry https://registry.npmmirror.com/
67 | # 2. 使用代理加速 npm config set proxy http://192.168.1.1:xxxx
68 | docker run --rm \
69 | --name "$PROJECT_NAME"-node \
70 | -v "${APP_PACKAGE_JSON_PATH}":/app \
71 | -v "${PUBLIC_NODE_MODULES_REPOSITORY}":/app/node_modules \
72 | -w /app \
73 | node:21 \
74 | sh -c "npm config set registry https://registry.npmmirror.com/ && npm install -g pnpm && pnpm install && pnpm run build"
75 | echo "========== 打包完成 =========="
76 | ls -l
77 | pwd
78 | mkdir -p "$BUILD_DIR"
79 | rm -rf "$BUILD_DIR/dist"
80 | mkdir -p "$BUILD_BACK_DIR/$DEPLOY_VERSION"
81 | cp -rf "${APP_PACKAGE_JSON_PATH}/dist" "$BUILD_BACK_DIR/$DEPLOY_VERSION"
82 | # 移动dist目录到部署目录
83 | mv "${APP_PACKAGE_JSON_PATH}/dist" "$BUILD_DIR"
84 | echo "=============压缩================"
85 | cd "$BUILD_DIR"
86 | zip -r dist.zip "./dist" > /dev/null
87 |
88 | # 提示用户输入密码
89 | read -s -p "Enter password for $REMOTE_USER@$REMOTE_HOST: " REMOTE_PASS
90 | echo
91 |
92 | # 将文件传输到应用机
93 | echo "Transferring package to remote host..."
94 | sshpass -p "$REMOTE_PASS" scp "$PACKAGE_FILE" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR"
95 |
96 | # 在应用机上解压文件,并将文件复制到目标目录
97 | echo "Cleaning up target directory on remote host..."
98 | sshpass -p "$REMOTE_PASS" ssh "$REMOTE_USER@$REMOTE_HOST" "rm -rf $TARGET_DIR/$PROJECT_NAME"
99 |
100 | echo "Extracting package on remote host..."
101 | sshpass -p "$REMOTE_PASS" ssh "$REMOTE_USER@$REMOTE_HOST" "unzip -o $REMOTE_DIR/$PACKAGE_FILE -d $REMOTE_DIR > /dev/null"
102 |
103 | echo "Copying extracted files to target directory..."
104 | sshpass -p "$REMOTE_PASS" ssh "$REMOTE_USER@$REMOTE_HOST" "mv $REMOTE_DIR/dist $TARGET_DIR/$PROJECT_NAME"
105 |
106 | # 可选:删除应用机上的压缩包
107 | echo "Cleaning up..."
108 | sshpass -p "$REMOTE_PASS" ssh "$REMOTE_USER@$REMOTE_HOST" "rm $REMOTE_DIR/$PACKAGE_FILE"
109 |
110 | echo "Deployment complete!"
111 |
112 | echo "========== 清理 =========="
113 | # 删除源代码文件
114 | rm -rf "$APP_PACKAGE_JSON_PATH"
115 |
116 | local end_time=$(date +%s)
117 | local duration=$((end_time - start_time))
118 | echo "==================== 整体执行耗时为: ${duration} 秒===================="
119 |
120 | }
121 |
122 | # 执行主函数
123 | main "$@"
124 |
--------------------------------------------------------------------------------
/shell/package/deploy-api-alone.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # 以下脚本使用了 阿里云 ACR 作为私有镜像仓库
3 |
4 | set -e # 如果命令失败,则立即退出
5 |
6 | ENV_ROOT_DIR="/home"
7 | ENV_APP_DIR="app"
8 | ENV_DOWNLOAD_DIR="download"
9 | ENV_DOCKER_SH="deploy-api-docker.sh" # 应用机器docker脚本
10 |
11 | ENV_GIT_USERNAME="你的git账户"
12 | ENV_GIT_PASSWORD="你的账户密码"
13 | ENV_GIT_HOST="你的git仓库host"
14 | ENV_GIT_PROTOCOL="https"
15 | GIT_PATH="你的仓库地址"
16 |
17 | BRANCH_NAME="main" #分支名
18 | PROJECT_NAME="sz-service-admin"
19 | VERSION="latest" # 镜像版本号
20 | # 根磁盘路径
21 | APP_TMP_PATH="${ENV_ROOT_DIR}/${ENV_DOWNLOAD_DIR}/${PROJECT_NAME}" #项目下载路径
22 |
23 | DEPLOY_VERSION=$(date +%Y%m%d%H%M%S)
24 | APP_POM_PATH="${APP_TMP_PATH}/${DEPLOY_VERSION}"
25 | APP_TARGET_PATH="${APP_POM_PATH}/sz-service/sz-service-admin/target"
26 | APP_CONFIG_PATH="${APP_POM_PATH}/sz-service/sz-service-admin/src/main/resources/config" # 子级配置文件目录
27 | DEPLOY_DIR="${ENV_ROOT_DIR}/${ENV_APP_DIR}/${PROJECT_NAME}/${DEPLOY_VERSION}" # 部署文件备份地址
28 |
29 | PROJECT_REPO="${ENV_GIT_PROTOCOL}://${ENV_GIT_USERNAME}:${ENV_GIT_PASSWORD}@${ENV_GIT_HOST}/${GIT_PATH}" # git clone 地址
30 | USE_ENV_CONFIG=true # 是否使用环境变量配置文件
31 | CONFIG_DIR="${ENV_ROOT_DIR}/${ENV_CONF_DIR}/${PROJECT_NAME}" # 环境变量配置文件目录
32 |
33 | REMOTE_USER="root" # 应用机的用户名
34 | REMOTE_HOST="你的应用机器IP" # 应用机的主机名或IP地址
35 | IMAGE_URL="registry.cn-beijing.aliyuncs.com/xxxx/xxxx" # 阿里ACR镜像仓库地址
36 | ACR_USERNAME="你的ACR账户" # ACR账户
37 | ACR_PWD="你的ACR密码" # ACR固定密码
38 |
39 | main() {
40 | local start_time=$(date +%s)
41 | echo "========== 正在拉取项目代码 =========="
42 | mkdir -p "$APP_TMP_PATH"
43 | cd "$APP_TMP_PATH" || exit
44 |
45 | git clone -b "$BRANCH_NAME" "$PROJECT_REPO" "$DEPLOY_VERSION"
46 | cd "$APP_POM_PATH" || exit
47 |
48 | # 如果使用环境变量配置文件
49 | if [ "$USE_ENV_CONFIG" = "true" ]; then
50 | mkdir -p "${CONFIG_DIR}"
51 | echo "========== 替换环境变量配置文件 =========="
52 | if [ -n "$(ls -A "${CONFIG_DIR}")" ]; then
53 | cp -rf "${CONFIG_DIR}"/* "${APP_CONFIG_PATH}"
54 | else
55 | echo "${CONFIG_DIR} 目录为空,跳过复制操作"
56 | fi
57 | fi
58 |
59 | mkdir -p "${DEPLOY_DIR}"
60 | # 移动Dockerfile到部署目录
61 | mv Dockerfile "${DEPLOY_DIR}"
62 |
63 | echo "========== 拉取完成,开始打包 =========="
64 | docker run --rm \
65 | --name "$PROJECT_NAME"-maven \
66 | -v "${APP_POM_PATH}":/app \
67 | -v "${ENV_PUBLIC_MAVEN_REPOSITORY}/${PROJECT_NAME}":/custom-maven-repo \
68 | maven:3.9.6 \
69 | mvn -f /app clean package -Dmaven.repo.local=/custom-maven-repo -Dmaven.test.skip=true
70 | # 移动jar包到部署目录
71 | mv "${APP_TARGET_PATH}"/*.jar "${DEPLOY_DIR}"
72 | # 删除源代码文件
73 | rm -rf "$APP_POM_PATH"
74 |
75 | echo "========== 打包完成 =========="
76 | # 切换到Dockerfile目录
77 | cd "$DEPLOY_DIR" || exit
78 |
79 | echo "==================== 删除旧镜像 ===================="
80 | docker image prune -f
81 | docker builder prune -f
82 |
83 | echo "==================== 构建 Docker 镜像 ===================="
84 | docker build -t "$PROJECT_NAME:${VERSION}" "$DEPLOY_DIR"
85 |
86 | echo "==================== 上传 Image 到制品库 ===================="
87 | docker login --username="$ACR_USERNAME" -p="$ACR_PWD" registry.cn-beijing.aliyuncs.com
88 | docker tag "$PROJECT_NAME:${VERSION}" ${IMAGE_URL}:"${VERSION}"
89 | echo "==================== Image 推送 ===================="
90 | docker push ${IMAGE_URL}:"${VERSION}"
91 |
92 | echo "==================== 登陆应用机 ===================="
93 | # 提示用户输入密码
94 | read -s -p "Enter password for $REMOTE_USER@$REMOTE_HOST: " REMOTE_PASS
95 | echo
96 |
97 | echo "==================== 应用机拉取镜像 ===================="
98 | # 确保环境变量 IMAGE_URL 和 VERSION 已经设置
99 | if [ -z "$IMAGE_URL" ] || [ -z "$VERSION" ]; then
100 | echo "Error: IMAGE_URL or VERSION is not set."
101 | exit 1
102 | fi
103 |
104 | sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "bash /home/deploy/$ENV_DOCKER_SH $VERSION"
105 | local end_time=$(date +%s)
106 | local duration=$((end_time - start_time))
107 | echo "==================== 整体执行耗时为: ${duration} 秒===================="
108 | }
109 |
110 | # 执行主函数
111 | main "$@"
112 |
--------------------------------------------------------------------------------
/shell/package/deploy-api-docker.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | set -e # 如果命令失败,则立即退出
4 |
5 | PROJECT_NAME="sz-service-admin"
6 | PORT=9991 # 服务端口
7 | PROFILE_ACTIVE="prod" # java -jar -Dspring.profiles.active=dev java部署环境
8 |
9 | ENV_ROOT_DIR="/home"
10 | ENV_APP_DIR="app"
11 | ENV_CONF_DIR="conf"
12 | LOG_DIR="${ENV_ROOT_DIR}/${ENV_APP_DIR}/${PROJECT_NAME}/logs"
13 | CONFIG_DIR="${ENV_ROOT_DIR}/${ENV_CONF_DIR}/${PROJECT_NAME}" # 环境变量配置文件目录
14 |
15 | ACR_URL=registry.cn-beijing.aliyuncs.com
16 | IMAGE_URL="$ACR_URL/你的命名空间/${PROJECT_NAME}"
17 | ACR_USERNAME="你的ACR账户"
18 | ACR_PWD="你的ACR密码"
19 |
20 | # 检查是否提供了版本号参数
21 | if [ -z "$1" ]; then
22 | echo "Error: No version specified."
23 | echo "Usage: $0 "
24 | exit 1
25 | fi
26 |
27 | VERSION="$1" # 镜像版本号
28 |
29 | main() {
30 | local start_time=$(date +%s)
31 |
32 | echo "==================== 登陆 acr ===================="
33 | docker login --username="$ACR_USERNAME" -p="$ACR_PWD" "$ACR_URL"
34 | echo "==================== 拉取制品 ===================="
35 | docker pull ${IMAGE_URL}:${VERSION}
36 |
37 | echo "==================== 停止旧应用容器 ===================="
38 | docker stop "$PROJECT_NAME" || true
39 | docker rm "$PROJECT_NAME" || true
40 |
41 | docker image prune -f
42 | docker builder prune -f
43 |
44 | echo "==================== 启动新应用容器 ===================="
45 | docker run -itd \
46 | --name "$PROJECT_NAME" \
47 | --restart always \
48 | -p "$PORT":"$PORT" \
49 | -v "$LOG_DIR":/logs \
50 | -v "$CONFIG_DIR":/config \
51 | -e "SPRING_PROFILES_ACTIVE=$PROFILE_ACTIVE" \
52 | "$IMAGE_URL:$VERSION"
53 |
54 | local end_time=$(date +%s)
55 | local duration=$((end_time - start_time))
56 | echo "==================== 整体执行耗时为: ${duration} 秒===================="
57 |
58 | echo "==================== 查看日志 ===================="
59 | docker logs "$PROJECT_NAME"
60 | }
61 |
62 | # 执行主函数
63 | main "$@"
64 |
--------------------------------------------------------------------------------