Skip to content

Commit 746afed

Browse files
frankslinclaude
andauthored
feat: 在頁腳添加版本號顯示功能 (cbdb-project#451)
實作內容: - 新增 app/helpers.php 輔助函數檔案,包含 get_app_version() 函數 - 優先從 version.txt 檔案讀取版本號(適用於生產環境) - 若檔案不存在,則從 Git commit ID 動態獲取(適用於開發環境) - 在 composer.json 中配置自動載入 helpers.php - 更新頁腳視圖,在右側顯示版本號 - 使用快取機制(10分鐘)避免頻繁讀取檔案或執行 Git 指令 - 新增 deploy.sh 部署腳本,自動生成版本號檔案並執行部署操作 - 將 version.txt 加入 .gitignore(由部署腳本動態生成) 使用方式: 在生產環境部署時執行 ./deploy.sh 即可自動生成版本號檔案 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 7ddfe48 commit 746afed

5 files changed

Lines changed: 83 additions & 3 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ Homestead.json
99
Homestead.yaml
1010
npm-debug.log
1111
yarn-error.log
12-
.env
12+
.env
13+
version.txt

app/helpers.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
if (!function_exists('get_app_version')) {
4+
/**
5+
* 获取应用版本号(基于 Git commit ID)
6+
*
7+
* @return string 返回短版本的 commit ID(前7位)或 'unknown'
8+
*/
9+
function get_app_version()
10+
{
11+
// 尝试从缓存中获取版本号(避免频繁读取文件或执行 git 命令)
12+
$cacheKey = 'app_version';
13+
$cachedVersion = \Cache::get($cacheKey);
14+
15+
if ($cachedVersion !== null) {
16+
return $cachedVersion;
17+
}
18+
19+
$version = 'unknown';
20+
21+
try {
22+
// 优先从 version.txt 文件读取(适用于生产环境)
23+
$versionFile = base_path('version.txt');
24+
if (file_exists($versionFile)) {
25+
$version = trim(file_get_contents($versionFile));
26+
}
27+
28+
// 如果文件不存在或为空,尝试从 Git 获取(适用于开发环境)
29+
if (empty($version) || $version === 'unknown') {
30+
$gitVersion = trim(shell_exec('git rev-parse --short=7 HEAD 2>/dev/null') ?? '');
31+
if (!empty($gitVersion)) {
32+
$version = $gitVersion;
33+
}
34+
}
35+
36+
// 缓存版本号10分钟
37+
\Cache::put($cacheKey, $version, 10);
38+
39+
return $version;
40+
} catch (\Exception $e) {
41+
return 'unknown';
42+
}
43+
}
44+
}

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
],
2828
"psr-4": {
2929
"App\\": "app/"
30-
}
30+
},
31+
"files": [
32+
"app/helpers.php"
33+
]
3134
},
3235
"autoload-dev": {
3336
"psr-4": {

deploy.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
# CBDB Online Main Server 部署脚本
4+
# 用于在服务器上部署应用时执行必要的更新操作
5+
6+
set -e
7+
8+
echo "开始部署..."
9+
10+
# 1. 生成版本号文件
11+
echo "生成版本号文件..."
12+
git rev-parse --short=7 HEAD > version.txt
13+
echo "版本号: $(cat version.txt)"
14+
15+
# 2. 安装/更新依赖
16+
echo "更新 Composer 依赖..."
17+
composer install --no-dev --optimize-autoloader
18+
19+
# 3. 清除缓存
20+
echo "清除应用缓存..."
21+
php artisan cache:clear
22+
php artisan config:clear
23+
php artisan route:clear
24+
php artisan view:clear
25+
26+
# 4. 优化缓存(可选,生产环境建议开启)
27+
# echo "优化缓存..."
28+
# php artisan config:cache
29+
# php artisan route:cache
30+
# php artisan view:cache
31+
32+
echo "部署完成!"

resources/views/layouts/footer.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<footer class="main-footer">
33
<!-- To the right -->
44
<div class="pull-right hidden-xs">
5-
5+
<b>Version:</b> {{ get_app_version() }}
66
</div>
77
<!-- Default to the left -->
88
<strong>Copyright &copy; <a href="https://cbdb.hsites.harvard.edu/" target="_blank">Chinese Biographical Database Project (CBDB)</a>.</strong> Content licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0 International</a>.

0 commit comments

Comments
 (0)