Skip to content

Commit 9d1caef

Browse files
authored
chore(release): generate version.json in CI, fall back to git at runtime (#1070)
The hand-edited web/configs/version.json went stale between releases. The release workflow now writes it with the tag and short SHA before zipping the webpanel, and web/init.php falls back to `git describe --tags --always` via shell_exec for dev clones, then N/A if git is unavailable.
1 parent 418d12a commit 9d1caef

4 files changed

Lines changed: 26 additions & 6 deletions

File tree

.github/workflows/release.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ jobs:
5555
run: |
5656
pkg="sourcebans-pp-${GITHUB_REF_NAME}.webpanel-only"
5757
cp -R web "/tmp/${pkg}"
58+
cat > "/tmp/${pkg}/configs/version.json" <<EOF
59+
{
60+
"version": "${GITHUB_REF_NAME}",
61+
"git": "$(git rev-parse --short HEAD)",
62+
"dev": false
63+
}
64+
EOF
5865
(cd /tmp && zip -rq "${GITHUB_WORKSPACE}/${pkg}.zip" "${pkg}")
5966
6067
- uses: actions/upload-artifact@v4

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ release/
6565
plugins/
6666
.sourceknight
6767
.venv
68+
69+
# Generated by .github/workflows/release.yml; runtime falls back to `git describe`
70+
/web/configs/version.json

web/configs/version.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

web/init.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,22 @@
8989
require_once(INCLUDES_PATH.'/CUserManager.php');
9090
require_once(INCLUDES_PATH.'/AdminTabs.php');
9191

92-
$version = @json_decode(file_get_contents('configs/version.json'), true);
92+
$version = is_readable('configs/version.json')
93+
? @json_decode(file_get_contents('configs/version.json'), true)
94+
: null;
95+
96+
if (!$version) {
97+
$tag = trim((string) @shell_exec('git describe --tags --always 2>/dev/null'));
98+
$sha = trim((string) @shell_exec('git rev-parse --short HEAD 2>/dev/null'));
99+
if ($tag !== '' || $sha !== '') {
100+
$version = [
101+
'version' => $tag !== '' ? $tag : 'N/A',
102+
'git' => $sha,
103+
'dev' => true,
104+
];
105+
}
106+
}
107+
93108
define('SB_VERSION', $version['version'] ?? 'N/A');
94109
define('SB_GITREV', $version['git'] ?? 0);
95110
define('SB_DEV', $version['dev'] ?? false);

0 commit comments

Comments
 (0)