Merge branch 'dev'

This commit is contained in:
Timi
2026-11-07 16:50:42 +08:00
3 changed files with 219 additions and 58 deletions
+97
View File
@@ -0,0 +1,97 @@
name: Deploy
# 网页部署:构建后同步到 nginx 静态目录
# 热更新包由 release.yml 单独处理,两者互不影响
# 标题带 [skip deploy] 可跳过网页更新
on:
pull_request:
branches:
- master
types:
- closed
workflow_dispatch:
jobs:
deploy:
runs-on: act_runner_node
if: ${{ github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Check skip flag
id: guard
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "skip=false" >> $GITHUB_OUTPUT
exit 0
fi
title="${{ github.event.pull_request.title }}"
if echo "$title" | grep -q "\[skip deploy\]"; then
echo "标题含 [skip deploy],跳过网页部署"
echo "skip=true" >> $GITHUB_OUTPUT
else
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: Configure nexus auth
if: steps.guard.outputs.skip != 'true'
env:
NEXUS_USERNAME: ${{ secrets.NEXUS_USERNAME }}
NEXUS_PASSWORD: ${{ secrets.NEXUS_PASSWORD }}
run: |
if [ -z "$NEXUS_USERNAME" ] || [ -z "$NEXUS_PASSWORD" ]; then
echo "FAIL: 缺少 secrets.NEXUS_USERNAME 或 secrets.NEXUS_PASSWORD"
exit 1
fi
auth=$(printf '%s:%s' "$NEXUS_USERNAME" "$NEXUS_PASSWORD" | base64 | tr -d '\n')
{
echo "registry=https://nexus.imyeyu.com/repository/npm-group/"
echo "//nexus.imyeyu.com/repository/npm-group/:_auth=${auth}"
echo "//nexus.imyeyu.com/repository/npm-group/:always-auth=true"
echo "//nexus.imyeyu.com/repository/npm-timi/:_auth=${auth}"
echo "//nexus.imyeyu.com/repository/npm-timi/:always-auth=true"
} > ~/.npmrc
echo "OK: nexus 认证已配置"
- name: Install dependencies
if: steps.guard.outputs.skip != 'true'
run: pnpm install --frozen-lockfile
- name: Build
if: steps.guard.outputs.skip != 'true'
run: pnpm run build
- name: Deploy to nginx
if: steps.guard.outputs.skip != 'true'
run: |
target=/mnt/nginx-html/gao
if [ ! -d "$target" ]; then
echo "FAIL: 目标目录 $target 不存在"
exit 1
fi
if [ ! -f dist/index.html ]; then
echo "FAIL: dist/index.html 缺失"
exit 1
fi
# 不能删除旧文件:nginx 对 /assets/ 设了一年 immutable 缓存,删掉会让在用的客户端白屏
# 同名文件覆盖即可,历史 chunk 保留
cp -a dist/. "$target/"
echo "部署完成"
echo "文件总数:$(find "$target" -type f | wc -l)"
echo "index.html:$(stat -c '%y %s 字节' "$target/index.html")"
echo "assets 数量:$(find "$target/assets" -type f 2>/dev/null | wc -l)"
- name: Verify
if: steps.guard.outputs.skip != 'true'
run: |
echo "--- 产物一致性检查 ---"
local_index=$(node -p "require('fs').readFileSync('dist/index.html','utf8').match(/index-[A-Za-z0-9_-]+\\.js/)?.[0] || ''")
remote_index=$(grep -oE "index-[A-Za-z0-9_-]+\.js" /mnt/nginx-html/gao/index.html | head -1)
echo "本地入口:$local_index"
echo "线上入口:$remote_index"
if [ "$local_index" != "$remote_index" ]; then
echo "FAIL: 线上 index.html 与本次产物不一致"
exit 1
fi
echo "OK: 线上已是最新构建"
+34 -16
View File
@@ -64,7 +64,7 @@ export function useAppUpdate() {
return Boolean(target?.url) && target?.version !== currentVersion.value;
}
/** 下载最新版本并排队等待生效 */
/** 下载最新版本,只落盘不激活,等用户确认后再应用 */
async function download(): Promise<BundleInfo | null> {
const target = latest.value;
if (!target?.url) {
@@ -84,7 +84,6 @@ export function useAppUpdate() {
checksum: target.checksum,
sessionKey: target.sessionKey
});
await CapacitorUpdater.next({ id: bundle.id });
ready.value = bundle;
return bundle;
} catch (e) {
@@ -96,19 +95,28 @@ export function useAppUpdate() {
}
}
/** 立即重启应用使更新生效 */
async function reload(): Promise<void> {
await CapacitorUpdater.reload();
/** 应用已下载的版本并重启,只有用户明确点了才执行 */
async function apply(bundle: BundleInfo): Promise<void> {
await CapacitorUpdater.set({ id: bundle.id });
}
/** 检查并下载,供启动时自动更新调用 */
async function checkAndDownload(): Promise<boolean> {
await loadCurrent();
const result = await check();
if (!result?.url || result.version === currentVersion.value) {
return false;
/** 查找已下载但尚未应用的版本,用户不点就一直停在这里 */
async function restorePending(): Promise<void> {
if (!supported) {
return;
}
try {
const [current, list] = await Promise.all([CapacitorUpdater.current(), CapacitorUpdater.list()]);
const currentId = current.bundle?.id;
const pending = list.bundles
.filter(bundle => bundle.id !== currentId)
.sort((a, b) => Date.parse(b.downloaded) - Date.parse(a.downloaded))[0];
if (pending) {
ready.value = pending;
}
} catch {
ready.value = null;
}
return (await download()) !== null;
}
return {
@@ -125,8 +133,8 @@ export function useAppUpdate() {
check,
hasUpdate,
download,
reload,
checkAndDownload
apply,
restorePending
};
}
@@ -138,11 +146,21 @@ export function isAutoUpdateEnabled(): boolean {
/**
* 启动时静默检查更新
*
* 只下载排队,不重启,下次打开 App 自动生效
* 只下载不应用,是否切换版本始终由用户决定
*/
export async function runAutoUpdate(): Promise<void> {
if (!Capacitor.isNativePlatform() || !isAutoUpdateEnabled()) {
return;
}
await useAppUpdate().checkAndDownload();
const update = useAppUpdate();
await update.loadCurrent();
await update.restorePending();
if (update.ready.value) {
return;
}
const result = await update.check();
if (!result?.url || result.version === update.currentVersion.value) {
return;
}
await update.download();
}
+88 -42
View File
@@ -47,7 +47,7 @@
</template>
</t-cell>
</t-cell-group>
<t-cell-group title="版本更新" theme="card" v-entrance-transition>
<t-cell-group v-if="updateSupported" title="版本更新" theme="card" v-entrance-transition>
<t-cell title="外壳版本" :note="nativeVersion || '—'" />
<t-cell title="系统版本" :note="currentVersion || '—'" />
<t-cell title="自动检查更新">
@@ -55,22 +55,43 @@
<t-switch :model-value="autoUpdate" @change="handleAutoUpdateChange" />
</template>
</t-cell>
<t-cell
title="检查更新"
:note="updateNote"
:arrow="canCheckUpdate"
@click="handleCheckUpdate"
/>
<div v-if="downloading" class="update-progress">
<t-progress :percentage="percent" :label="false" />
</div>
<t-cell
v-if="ready"
title="立即重启生效"
arrow
@click="handleReload"
/>
</t-cell-group>
<div v-if="updateSupported" class="update-panel" v-entrance-transition>
<center-tips v-if="updateHint" v-text="updateHint" />
<t-button
v-if="updateStage === 'check'"
class="cell-button"
theme="primary"
size="large"
:loading="checking"
@click="handleCheck"
>
检查更新
</t-button>
<t-button
v-else-if="updateStage === 'download'"
class="cell-button"
theme="primary"
size="large"
@click="handleDownload"
>
下载更新
</t-button>
<template v-else-if="updateStage === 'downloading'">
<t-progress class="progress" stroke-width=".5rem" :percentage="percent" /> <t-button theme="primary" size="large" block disabled>
下载中 {{ percent }}%
</t-button>
</template>
<t-button
v-else-if="updateStage === 'apply'"
class="cell-button"
theme="primary"
size="large"
@click="handleApply"
>
现在更新
</t-button>
</div>
</div>
</main>
</div>
@@ -155,34 +176,46 @@ const {
check,
hasUpdate,
download,
reload
apply,
restorePending
} = useAppUpdate();
const autoUpdate = ref(isAutoUpdateEnabled());
const canCheckUpdate = computed(() => updateSupported && !checking.value && !downloading.value);
/** 当前所处的更新阶段 */
const updateStage = computed<"check" | "download" | "downloading" | "apply">(() => {
if (downloading.value) {
return "downloading";
}
if (ready.value) {
return "apply";
}
if (latest.value && hasUpdate()) {
return "download";
}
return "check";
});
const updateNote = computed<string>(() => {
/** 阶段说明文案 */
const updateHint = computed<string>(() => {
if (!updateSupported) {
return "仅在 App 内可用";
}
if (downloading.value) {
return `下载中 ${percent.value}%`;
}
if (ready.value) {
return "已下载,下次启动生效";
return `v${ready.value.version} 已下载,点击下方按钮重启生效`;
}
if (checking.value) {
return "检查中";
if (latest.value && hasUpdate()) {
return `发现新版本 v${latest.value.version}`;
}
if (latest.value) {
return hasUpdate() ? `发现 v${latest.value.version}` : "已是最新";
if (latest.value && !checking.value) {
return "当前已是最新版本";
}
return "";
});
onMounted(async () => {
await loadCurrent();
await restorePending();
});
function handleAutoUpdateChange(value: boolean | string | number): void {
@@ -191,10 +224,7 @@ function handleAutoUpdateChange(value: boolean | string | number): void {
localStorage.setItem(AUTO_UPDATE_KEY, enabled ? "1" : "0");
}
async function handleCheckUpdate(): Promise<void> {
if (!canCheckUpdate.value) {
return;
}
async function handleCheck(): Promise<void> {
const result = await check();
if (!result) {
Message.error(updateError.value || "检查更新失败");
@@ -202,18 +232,24 @@ async function handleCheckUpdate(): Promise<void> {
}
if (!hasUpdate()) {
Message.info("已是最新版本");
return;
}
const bundle = await download();
if (bundle) {
Message.success(`v${bundle.version} 已下载,下次启动生效`);
return;
}
Message.error(updateError.value || "下载失败");
}
async function handleReload(): Promise<void> {
await reload();
async function handleDownload(): Promise<void> {
const bundle = await download();
if (!bundle) {
Message.error(updateError.value || "下载失败");
return;
}
Message.success(`v${bundle.version} 已下载`);
}
async function handleApply(): Promise<void> {
const bundle = ready.value;
if (!bundle) {
return;
}
await apply(bundle);
}
</script>
@@ -254,8 +290,14 @@ async function handleReload(): Promise<void> {
}
}
.update-progress {
padding: 0 1rem .75rem;
.update-panel {
gap: .75rem;
display: flex;
flex-direction: column;
.progress {
margin: 0 1rem;
}
}
}
@@ -264,6 +306,10 @@ async function handleReload(): Promise<void> {
.volume-field span {
color: #94a3b8;
}
.update-hint {
color: #94a3b8;
}
}
}
</style>