Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:construction_worker: Improve release caches and Android build verification

Danielbfaeb3c4b23c
diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml
index 5bdd213617..6f9f4d2620 100644
--- a/.github/workflows/cd.yml
+++ b/.github/workflows/cd.yml
@@ -10,6 +10,7 @@ on:
env:
package_json: "app/package.json"
+ GOMODCACHE: ${{ github.workspace }}/go/pkg/mod
jobs:
prepare:
@@ -147,7 +148,7 @@ jobs:
- name: Test workflow outcome reporting
id: workflow_report_tests
continue-on-error: true
- run: node --test scripts/test-report-workflow-outcomes.mjs
+ run: node --test scripts/test-report-workflow-outcomes.mjs scripts/go-cache.test.mjs
- name: Report non-blocking step outcomes
if: always()
@@ -168,12 +169,24 @@ jobs:
- uses: actions/setup-go@v6
with:
go-version-file: kernel/go.mod
- cache-dependency-path: kernel/go.sum
+ cache: false
- uses: actions/setup-node@v7
with:
node-version: 24
+ - name: Prepare Go cache
+ id: go-cache
+ run: node scripts/go-cache.mjs
+ - name: Restore Go cache
+ id: restore-go-cache
+ uses: actions/cache/restore@v6
+ with:
+ path: |
+ ${{ steps.go-cache.outputs.modules }}
+ ${{ steps.go-cache.outputs.build }}
+ key: ${{ steps.go-cache.outputs.key }}
+
- name: Install repository pnpm version
- run: npm install --global ${{ needs.prepare.outputs.packageManager }}
+ run: npm install --global --allow-scripts=pnpm ${{ needs.prepare.outputs.packageManager }}
- name: Install frontend dependencies
run: pnpm install --frozen-lockfile
working-directory: app
@@ -196,10 +209,22 @@ jobs:
- uses: actions/setup-go@v6
with:
go-version-file: kernel/go.mod
- cache-dependency-path: kernel/go.sum
+ cache: false
- uses: actions/setup-node@v7
with:
node-version: 24
+ - name: Prepare Go cache
+ id: go-cache
+ run: node scripts/go-cache.mjs
+ - name: Restore Go cache
+ id: restore-go-cache
+ uses: actions/cache/restore@v6
+ with:
+ path: |
+ ${{ steps.go-cache.outputs.modules }}
+ ${{ steps.go-cache.outputs.build }}
+ key: ${{ steps.go-cache.outputs.key }}
+
- name: Install filesystem fault injection dependency
run: |
sudo apt-get update
@@ -235,6 +260,16 @@ jobs:
path: ${{ runner.temp }}/kernel-test-results
if-no-files-found: ignore
+ # 同一平台只有指定作业保存缓存,其他并行作业只恢复。
+ - name: Save Go cache
+ if: ${{ steps.restore-go-cache.outputs.cache-hit != 'true' }}
+ uses: actions/cache/save@v6
+ with:
+ path: |
+ ${{ steps.go-cache.outputs.modules }}
+ ${{ steps.go-cache.outputs.build }}
+ key: ${{ steps.go-cache.outputs.key }}
+
- name: Report non-blocking step outcomes
if: always()
env:
@@ -257,7 +292,19 @@ jobs:
- uses: actions/setup-go@v6
with:
go-version-file: kernel/go.mod
- cache-dependency-path: kernel/go.sum
+ cache: false
+ - name: Prepare Go cache
+ id: go-cache
+ run: node scripts/go-cache.mjs
+ - name: Restore Go cache
+ id: restore-go-cache
+ uses: actions/cache/restore@v6
+ with:
+ path: |
+ ${{ steps.go-cache.outputs.modules }}
+ ${{ steps.go-cache.outputs.build }}
+ key: ${{ steps.go-cache.outputs.key }}
+
- name: Set up Windows kernel test compiler
uses: msys2/setup-msys2@v2
with:
@@ -285,6 +332,16 @@ jobs:
path: ${{ runner.temp }}/kernel-test-results
if-no-files-found: ignore
+ # 同一平台只有指定作业保存缓存,其他并行作业只恢复。
+ - name: Save Go cache
+ if: ${{ steps.restore-go-cache.outputs.cache-hit != 'true' }}
+ uses: actions/cache/save@v6
+ with:
+ path: |
+ ${{ steps.go-cache.outputs.modules }}
+ ${{ steps.go-cache.outputs.build }}
+ key: ${{ steps.go-cache.outputs.key }}
+
- name: Report non-blocking step outcomes
if: always()
env:
@@ -304,7 +361,7 @@ jobs:
with:
node-version: 24
- name: Install repository pnpm version
- run: npm install --global ${{ needs.prepare.outputs.packageManager }}
+ run: npm install --global --allow-scripts=pnpm ${{ needs.prepare.outputs.packageManager }}
- name: Install frontend dependencies
run: pnpm install --frozen-lockfile
working-directory: app
@@ -434,7 +491,7 @@ jobs:
- name: Install test dependencies
id: dependencies
run: |
- npm install --global ${{ needs.prepare.outputs.packageManager }}
+ npm install --global --allow-scripts=pnpm ${{ needs.prepare.outputs.packageManager }}
pnpm install --frozen-lockfile
working-directory: siyuan-testing
@@ -590,8 +647,7 @@ jobs:
uses: actions/setup-go@v6
with:
go-version-file: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}/kernel/go.mod
- cache-dependency-path: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}/kernel/go.sum
- cache: true
+ cache: false
- run: go version
- name: Set up Node
@@ -599,8 +655,21 @@ jobs:
with:
node-version: 24
+ - name: Prepare Go cache
+ id: go-cache
+ run: node scripts/go-cache.mjs
+ working-directory: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}
+ - name: Restore Go cache
+ id: restore-go-cache
+ uses: actions/cache/restore@v6
+ with:
+ path: |
+ ${{ steps.go-cache.outputs.modules }}
+ ${{ steps.go-cache.outputs.build }}
+ key: ${{ steps.go-cache.outputs.key }}
+
- name: Install Node pnpm
- run: npm install -g ${{ needs.prepare.outputs.packageManager }}
+ run: npm install --global --allow-scripts=pnpm ${{ needs.prepare.outputs.packageManager }}
working-directory: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}/app
- name: Get pnpm store path
@@ -611,12 +680,13 @@ jobs:
echo "path=$STORE_PATH" >> "$GITHUB_OUTPUT"
working-directory: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}/app
- - name: Cache pnpm store
- uses: actions/cache@v6
+ - name: Restore pnpm store
+ id: restore-pnpm-cache
+ uses: actions/cache/restore@v6
with:
path: ${{ steps.pnpm-store.outputs.path }}
- key: pnpm-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles(format('go/src/github.com/{0}/app/pnpm-lock.yaml', github.repository)) }}
- restore-keys: pnpm-${{ runner.os }}-${{ runner.arch }}-
+ key: pnpm-v2-${{ runner.os }}-${{ runner.arch }}-${{ needs.prepare.outputs.packageManager }}-${{ hashFiles(format('go/src/github.com/{0}/app/pnpm-lock.yaml', github.repository)) }}
+ restore-keys: pnpm-v2-${{ runner.os }}-${{ runner.arch }}-${{ needs.prepare.outputs.packageManager }}-
- name: Install Node Dependencies
run: pnpm install --no-frozen-lockfile
@@ -728,6 +798,10 @@ jobs:
run: |
echo "sdk.dir=$ANDROID_HOME" > local.properties
chmod +x ./gradlew
+ # 新版 Android 工程验证所有渠道;固定到旧提交的构建仍可使用原有流程。
+ if [ -f scripts/verify-build.gradle ]; then
+ ./gradlew --init-script scripts/verify-build.gradle :app:verifyBuildConfiguration
+ fi
./gradlew assembleOfficialRelease
cd app/build/outputs/apk/official/release
mv siyuan-*-official-release.apk siyuan-${{ needs.prepare.outputs.version }}.apk
diff --git a/.github/workflows/desktop-build.yml b/.github/workflows/desktop-build.yml
index c2277fe512..bf68576500 100644
--- a/.github/workflows/desktop-build.yml
+++ b/.github/workflows/desktop-build.yml
@@ -20,6 +20,7 @@ jobs:
env:
# 缓存恢复、保存与编译共用模块目录。
GOPATH: ${{ github.workspace }}/go
+ GOMODCACHE: ${{ github.workspace }}/go/pkg/mod
SIYUAN_E2E_SHARDS: ${{ fromJSON(inputs.config).e2e_shards }}
steps:
- uses: actions/checkout@v7
@@ -37,8 +38,25 @@ jobs:
uses: actions/setup-go@v6
with:
go-version-file: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}/kernel/go.mod
- cache-dependency-path: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}/kernel/go.sum
- cache: true
+ cache: false
+ - name: Set up Node
+ uses: actions/setup-node@v7
+ with:
+ node-version: 24
+
+ - name: Prepare Go cache
+ id: go-cache
+ run: node scripts/go-cache.mjs
+ working-directory: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}
+ - name: Restore Go cache
+ id: restore-go-cache
+ uses: actions/cache/restore@v6
+ with:
+ path: |
+ ${{ steps.go-cache.outputs.modules }}
+ ${{ steps.go-cache.outputs.build }}
+ key: ${{ steps.go-cache.outputs.key }}
+
- run: go version
- name: Set up Linux musl compiler
@@ -74,13 +92,8 @@ jobs:
GOOS: ${{ fromJSON(inputs.config).goos }}
GOARCH: ${{ fromJSON(inputs.config).goarch }}
- - name: Set up Node
- uses: actions/setup-node@v7
- with:
- node-version: 24
-
- name: Install Node pnpm
- run: npm install -g ${{ inputs.packageManager }}
+ run: npm install --global --allow-scripts=pnpm ${{ inputs.packageManager }}
working-directory: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}/app
- name: Get pnpm store path
@@ -91,12 +104,13 @@ jobs:
echo "path=$STORE_PATH" >> "$GITHUB_OUTPUT"
working-directory: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}/app
- - name: Cache pnpm store
- uses: actions/cache@v6
+ - name: Restore pnpm store
+ id: restore-pnpm-cache
+ uses: actions/cache/restore@v6
with:
path: ${{ steps.pnpm-store.outputs.path }}
- key: pnpm-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles(format('go/src/github.com/{0}/app/pnpm-lock.yaml', github.repository)) }}
- restore-keys: pnpm-${{ runner.os }}-${{ runner.arch }}-
+ key: pnpm-v2-${{ runner.os }}-${{ runner.arch }}-${{ inputs.packageManager }}-${{ hashFiles(format('go/src/github.com/{0}/app/pnpm-lock.yaml', github.repository)) }}
+ restore-keys: pnpm-v2-${{ runner.os }}-${{ runner.arch }}-${{ inputs.packageManager }}-
- name: Install Node Dependencies
run: pnpm install --no-frozen-lockfile
@@ -423,6 +437,23 @@ jobs:
${{ github.workspace }}/go/src/github.com/${{ github.repository }}/app/build/siyuan-*-linux.deb
${{ github.workspace }}/go/src/github.com/${{ github.repository }}/app/build/siyuan-*-linux.rpm
+ # 同一平台只有指定作业保存缓存,其他并行作业只恢复。
+ - name: Save Go cache
+ if: ${{ steps.restore-go-cache.outputs.cache-hit != 'true' && fromJSON(inputs.config).goos == 'darwin' && fromJSON(inputs.config).goarch == 'arm64' }}
+ uses: actions/cache/save@v6
+ with:
+ path: |
+ ${{ steps.go-cache.outputs.modules }}
+ ${{ steps.go-cache.outputs.build }}
+ key: ${{ steps.go-cache.outputs.key }}
+
+ - name: Save pnpm store
+ if: ${{ steps.restore-pnpm-cache.outputs.cache-hit != 'true' && !(fromJSON(inputs.config).goos == 'darwin' && fromJSON(inputs.config).goarch == 'amd64') }}
+ uses: actions/cache/save@v6
+ with:
+ path: ${{ steps.pnpm-store.outputs.path }}
+ key: ${{ steps.restore-pnpm-cache.outputs.cache-primary-key }}
+
- name: Report non-blocking step outcomes
if: always()
env:
diff --git a/scripts/go-cache.mjs b/scripts/go-cache.mjs
new file mode 100644
index 0000000000..a8b36e84fb
--- /dev/null
+++ b/scripts/go-cache.mjs
@@ -0,0 +1,33 @@
+import {execFileSync} from "node:child_process";
+import {createHash} from "node:crypto";
+import {appendFileSync, readFileSync} from "node:fs";
+import {dirname, resolve} from "node:path";
+import {fileURLToPath} from "node:url";
+
+export const goCacheKey = ({os, arch, image = "", version, sum}) => {
+ for (const value of [os, arch, version, ...(os === "Linux" ? [image] : [])]) {
+ if (!value || !/^[\w.-]+$/.test(value)) {
+ throw new Error("Invalid Go cache metadata");
+ }
+ }
+ const digest = createHash("sha256").update(sum).digest("hex");
+ return `siyuan-go-v1-${os}-${arch}-${os === "Linux" ? image + "-" : ""}${version}-${digest}`;
+};
+
+// 所有作业从当前 Go 环境读取目录,恢复与保存使用同一组路径。
+if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
+ const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
+ const metadata = JSON.parse(execFileSync("go", ["env", "-json", "GOMODCACHE", "GOCACHE", "GOVERSION"], {
+ cwd: resolve(root, "kernel"), encoding: "utf8", windowsHide: true,
+ }));
+ const key = goCacheKey({os: process.env.RUNNER_OS, arch: process.env.RUNNER_ARCH,
+ image: process.env.ImageOS, version: metadata.GOVERSION,
+ sum: readFileSync(resolve(root, "kernel/go.sum"))});
+ for (const value of [metadata.GOMODCACHE, metadata.GOCACHE]) {
+ if (!value || /[\r\n]/.test(value)) {
+ throw new Error("Invalid Go cache path");
+ }
+ }
+ appendFileSync(process.env.GITHUB_OUTPUT,
+ `key=${key}\nmodules=${metadata.GOMODCACHE}\nbuild=${metadata.GOCACHE}\n`);
+}
diff --git a/scripts/go-cache.test.mjs b/scripts/go-cache.test.mjs
new file mode 100644
index 0000000000..ea3987995c
--- /dev/null
+++ b/scripts/go-cache.test.mjs
@@ -0,0 +1,18 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import {goCacheKey} from "./go-cache.mjs";
+
+test("Go cache sharing depends on host, toolchain and dependencies rather than the build target", () => {
+ const inputs = {os: "Linux", arch: "X64", image: "ubuntu24", version: "go1.26.5", sum: "dependencies"};
+ const key = goCacheKey(inputs);
+ assert.match(key, /^siyuan-go-v1-Linux-X64-ubuntu24-go1\.26\.5-[0-9a-f]{64}$/);
+ assert.equal(goCacheKey({...inputs, sum: Buffer.from(inputs.sum)}), key);
+ for (const changes of [{os: "Windows"}, {arch: "ARM64"}, {image: "ubuntu26"},
+ {version: "go1.27.2"}, {sum: "new dependencies"}]) {
+ assert.notEqual(goCacheKey({...inputs, ...changes}), key);
+ }
+ assert.equal(goCacheKey({...inputs, os: "macOS", image: "unused"}),
+ goCacheKey({...inputs, os: "macOS", image: "different"}));
+ assert.throws(() => goCacheKey({...inputs, version: ""}), /Invalid Go cache metadata/);
+ assert.throws(() => goCacheKey({...inputs, image: "ubuntu24\ninjected=value"}), /Invalid Go cache metadata/);
+});
diff --git a/scripts/test_editor_e2e_workflow.py b/scripts/test_editor_e2e_workflow.py
index 0b082924ed..1955133e49 100644
--- a/scripts/test_editor_e2e_workflow.py
+++ b/scripts/test_editor_e2e_workflow.py
@@ -2,6 +2,7 @@
import hashlib
import itertools
+import json
import os
from pathlib import Path
import re
@@ -213,7 +214,9 @@ class EditorWorkflowTests(unittest.TestCase):
check = job(name)
self.assertIn("uses: actions/setup-go@v6", check)
self.assertIn("go-version-file: kernel/go.mod", check)
- self.assertIn("cache-dependency-path: kernel/go.sum", check)
+ self.assertIn("cache: false", check)
+ self.assertIn("uses: actions/cache/restore@v6", check)
+ self.assertIn("run: node scripts/go-cache.mjs", check)
linux = job("kernel-tests-linux")
self.assertIn("runs-on: ubuntu-24.04", linux)
self.assertIn("sudo apt-get install -y strace", linux)
@@ -250,11 +253,72 @@ class EditorWorkflowTests(unittest.TestCase):
self.assertIn(" GOPATH: ${{ github.workspace }}/go", header)
self.assertNotIn("GOPATH:", steps)
setup = steps.split("uses: actions/setup-go@v6", 1)[1].split(" - ", 1)[0]
- self.assertIn("cache: true", setup)
- self.assertIn("cache-dependency-path:", setup)
+ self.assertIn("cache: false", setup)
+ self.assertNotIn("cache-dependency-path:", setup)
+ self.assertIn("run: node scripts/go-cache.mjs", steps)
+ self.assertIn("${{ steps.go-cache.outputs.modules }}", steps)
+ self.assertIn("${{ steps.go-cache.outputs.build }}", steps)
+ self.assertIn(" GOMODCACHE: ${{ github.workspace }}/go/pkg/mod", CD.split("jobs:", 1)[0])
+ self.assertIn(" GOMODCACHE: ${{ github.workspace }}/go/pkg/mod", DESKTOP)
self.assertIn('"$GOPATH/bin/gomobile" init', job("build_android"))
self.assertIn("${{ github.workspace }}\\go\\${{ fromJSON(inputs.config).gobin }}\\goversioninfo", DESKTOP)
+ def test_parallel_jobs_have_one_writer_per_shared_cache(self):
+ desktop_configs = []
+ for section in job("build").split(" - os: ")[1:]:
+ desktop_configs.append({key: re.search(rf"^ {key}: (\S+)", section, re.M)[1].strip('"')
+ for key in ["goos", "goarch"]})
+ desktop_configs.append(json.loads(re.search(r"config: >-\n\s*(\{[^\n]+\})", job("build_macos_arm64"))[1]))
+
+ def saves(workflow, name, config, hit):
+ matches = re.findall(rf"- name: {name}\n(.*?)(?= - |\Z)", workflow, re.S)
+ if not matches:
+ return False
+ self.assertEqual(len(matches), 1)
+ step = matches[0]
+ self.assertIn("uses: actions/cache/save@v6", step)
+ expression = re.search(r"if: \$\{\{ (.*?) \}\}", step)[1]
+ expression = re.sub(r"steps\.[\w-]+\.outputs\.cache-hit", repr(str(hit).lower()), expression)
+ expression = expression.replace("fromJSON(inputs.config).goos", repr(config.get("goos")))
+ expression = expression.replace("fromJSON(inputs.config).goarch", repr(config.get("goarch")))
+ expression = expression.replace("&&", " and ").replace("||", " or ")
+ expression = re.sub(r"!(?!=)", "not ", expression)
+ return eval(expression, {"__builtins__": {}}, {})
+
+ readers = [("linux", job("contracts"), {}), ("linux", job("kernel-tests-linux"), {}),
+ ("windows", job("kernel-tests-windows"), {}), ("linux", job("build_android"), {})]
+ readers += [(config["goos"], DESKTOP, config) for config in desktop_configs]
+ for cache in ["Go cache", "pnpm store"]:
+ for host in ["linux", "windows", "darwin"]:
+ with self.subTest(cache=cache, host=host):
+ candidates = [(workflow, config) for os_name, workflow, config in readers if os_name == host]
+ self.assertEqual(sum(saves(workflow, "Save " + cache, config, False)
+ for workflow, config in candidates), 1)
+ self.assertFalse(any(saves(workflow, "Save " + cache, config, True)
+ for workflow, config in candidates))
+ for workflow, config in [(workflow, config) for _, workflow, config in readers]:
+ self.assertNotIn("uses: actions/cache@", workflow)
+ if "uses: actions/cache/save@v6" in workflow:
+ self.assertIn("uses: actions/cache/restore@v6", workflow)
+
+ def test_global_pnpm_installations_allow_only_the_pnpm_script(self):
+ for workflow in [CD, DESKTOP]:
+ installs = re.findall(r"\bnpm install[^\n]+", workflow)
+ self.assertTrue(installs)
+ for command in installs:
+ self.assertIn("--global --allow-scripts=pnpm", command)
+ self.assertNotIn("dangerously-allow-all-scripts", command)
+ self.assertIn("packageManager", command)
+ self.assertIn("scripts/go-cache.test.mjs", CD)
+
+ def test_android_build_verification_preserves_pinned_legacy_checkouts(self):
+ build = job("build_android")
+ step = build.split("- name: Building Android App\n", 1)[1].split(" - ", 1)[0]
+ self.assertIn("if [ -f scripts/verify-build.gradle ]; then", step)
+ self.assertIn("./gradlew --init-script scripts/verify-build.gradle :app:verifyBuildConfiguration", step)
+ self.assertLess(step.index(":app:verifyBuildConfiguration"), step.index("./gradlew assembleOfficialRelease"))
+ self.assertIn("mv siyuan-*-official-release.apk siyuan-${{ needs.prepare.outputs.version }}.apk", step)
+
def test_independent_editor_runners_and_serial_workers(self):
editor = job("editor_e2e")
self.assertIn("needs: [prepare, build_macos_arm64]", editor)