.gitignore
· 100 B · Text
Sin formato
# Python
.venv/
build/
docsite/
translated/
tmp/
**.log
**.txt
!commit.txt
aitr.toml
config.toml
| 1 | # Python |
| 2 | .venv/ |
| 3 | |
| 4 | build/ |
| 5 | docsite/ |
| 6 | translated/ |
| 7 | tmp/ |
| 8 | |
| 9 | **.log |
| 10 | **.txt |
| 11 | !commit.txt |
| 12 | |
| 13 | aitr.toml |
| 14 | config.toml |
| 15 |
DOCS.md
· 1.5 KiB · Markdown
Sin formato
# 中文文档
本文档使用 AI 翻译
## 项目流程
### 首次使用
1. 创建空分支
> 未初始化
```bash
git init
git branch -m docs
```
> 已存在项目
```bash
git switch --orphan docs
```
2. 首次提交
```bash
git add README.md
git commit -am init
git push origin docs
```
3. 拉取上游源码
```bash
mkdir -p docsite
pushd docsite
git init
git remote add upstream https://github.com/xxx/docs.git
git reset --hard
git fetch upstream main
git merge upstream/main
git rev-parse --short HEAD > ../commit.txt
popd
```
4. 复制源文档
```bash
rm -rf docs
# cp -r docsite/docs .
cp -r docsite/content .
mv content docs
```
5. 全量翻译
```bash
aitr
```
6. 本地测试与构建
```bash
git clone https://github.com/xxx/docs.git docsite
# cp -r docs_zh/* ./docsite/docs
cp -r docs_zh/* ./docsite/content
```
8. 启动或构建
```bash
cd docsite
# NodeJS
npm install
npm run dev
# Hugo
hugo server
...
```
### 2. AI 翻译
- 安装 [**CLI**](https://git.jetsung.com/jetsung/ai-translator) 工具 (增量更新直接使用 AI CLI 工具直接对比)
```bash
curl -L https://fx4.cn/aitr | bash
```
1. 设置环境变量 [`config.toml`](config.example.toml)
```bash
...
[[providers]]
enabled = true
name = "grok"
api_key = "xxx"
base_url = "https://api.x.ai/v1"
model = "grok-3"
concurrency = 1 # 线程数
rate_delay = 3.0 # 每个请求后等待 1.0 秒(可根据限流调整)
```
2. AI 翻译
```bash
aitr
```
中文文档
本文档使用 AI 翻译
项目流程
首次使用
- 创建空分支
未初始化
git init
git branch -m docs
已存在项目
git switch --orphan docs
- 首次提交
git add README.md
git commit -am init
git push origin docs
- 拉取上游源码
mkdir -p docsite
pushd docsite
git init
git remote add upstream https://github.com/xxx/docs.git
git reset --hard
git fetch upstream main
git merge upstream/main
git rev-parse --short HEAD > ../commit.txt
popd
- 复制源文档
rm -rf docs
# cp -r docsite/docs .
cp -r docsite/content .
mv content docs
- 全量翻译
aitr
- 本地测试与构建
git clone https://github.com/xxx/docs.git docsite
# cp -r docs_zh/* ./docsite/docs
cp -r docs_zh/* ./docsite/content
- 启动或构建
cd docsite
# NodeJS
npm install
npm run dev
# Hugo
hugo server
...
2. AI 翻译
- 安装 CLI 工具 (增量更新直接使用 AI CLI 工具直接对比)
curl -L https://fx4.cn/aitr | bash
- 设置环境变量
config.toml
...
[[providers]]
enabled = true
name = "grok"
api_key = "xxx"
base_url = "https://api.x.ai/v1"
model = "grok-3"
concurrency = 1 # 线程数
rate_delay = 3.0 # 每个请求后等待 1.0 秒(可根据限流调整)
- AI 翻译
aitr
README.md
· 711 B · Markdown
Sin formato
# 教程
本项目使用的教程
## 初始化项目
1. 创建空分支
```bash
# 新项目
git init
git branch -m docs
```
```bash
# 已存在的项目,创建空分支
git switch --orphan docs
```
2. 将本项目的文件下载至创建的翻译项目
```bash
curl -fsSL https://fx4.cn/gendocs | bash
```
3. 更新上游项目链接
```bash
GIT_DOCS=含.git结尾的项目地址
sed -i "s#https://github.com/xxx/docs.git#${GIT_DOCS}#g" *.*
sed -i "s#https://github.com/xxx/docs.git#${GIT_DOCS}#g" .github/workflows/docs.yml
```
4. 设置项目地址
```bash
# 新项目
git remote add origin <https 或 git 地址>
```
```bash
# 已存在的项目
git remote set-url origin <https 或 git 地址>
```
教程
本项目使用的教程
初始化项目
- 创建空分支
# 新项目
git init
git branch -m docs
# 已存在的项目,创建空分支
git switch --orphan docs
- 将本项目的文件下载至创建的翻译项目
curl -fsSL https://fx4.cn/gendocs | bash
- 更新上游项目链接
GIT_DOCS=含.git结尾的项目地址
sed -i "s#https://github.com/xxx/docs.git#${GIT_DOCS}#g" *.*
sed -i "s#https://github.com/xxx/docs.git#${GIT_DOCS}#g" .github/workflows/docs.yml
- 设置项目地址
# 新项目
git remote add origin <https 或 git 地址>
# 已存在的项目
git remote set-url origin <https 或 git 地址>
config.example.toml
· 3.9 KiB · TOML
Sin formato
# 根目录 - 包含需要翻译的 Markdown 文件
root_dir = "./docs"
# 输出目录 - 翻译后的文件保存位置
output_dir = "./docs_zh"
# 输出模式: "overwrite" (覆盖原文件) 或 "new_folder" (保存到新文件夹)
output_mode = "new_folder"
# 排除的目录 (逗号分隔)
exclude_dir = "node_modules,.git,_build"
# 白名单文件扩展名 (逗号分隔,留空表示使用默认支持的格式)
# whitelist_extensions = "md,txt,srt"
# 黑名单文件扩展名 (逗号分隔,留空表示不排除任何格式)
# blacklist_extensions = "pdf,docx"
# 最大 token 数
max_tokens = 99999
# 大文件拆分阈值 (单位:字符)
max_chunk_size = 8192
# 系统提示词(支持多行,使用 """ 包裹)
system_prompt = """
你是一个专业的技术文档翻译专家。请将以下英文 Markdown 文档翻译成流畅、自然的简体中文。
严格要求:
1. 保留完整的 Markdown 格式,包括标题、列表、表格、代码块、链接、图片等一切结构完全不变。
2. 代码块、命令行、文件名、路径、API 名称、配置文件内容、技术术语等保持原样(不要翻译)。
3. 专有名词(如产品名、框架名、软件名,例如 Traefik、Docker、Kubernetes)保持英文原名。
4. Hugo 短代码和模板语法(如 {{< xxx >}}、{{ xxx }}、{{</ xxx >}} 等所有以 {{ 或 {{< 开头的标签)必须完整保留原样,不翻译、不修改其内部任何内容。
5. 对于 YAML frontmatter(文档开头的 --- 之间的内容):
- 只翻译实际需要本地化的字符串值内容(如 title、description、linkTitle 等字段的值)。
- tags 字段的值(通常为字符串或字符串列表)必须完全保持原英文不变,不进行任何翻译或修改。
- 其他字段(如 keywords 等)按正常规则处理:如果为字符串值则翻译(如 description),如果为列表则视情况保留英文(专有名词不翻译)。
- 严格保留原文档中的所有字段、键名、结构、格式、缩进和所有机制(包括已有的 YAML 锚点 &xxx 和别名 *xxx)。
- 如果原文档中已有锚点定义,则在翻译该锚点对应的字符串值时保留锚点标记(如 &desc);别名引用(如 *desc)保持原样不变。
- 如果原文档中没有锚点或别名,则翻译后绝对不得新增任何锚点、别名、params 块或其他额外字段。
- 绝对不得添加、删除或修改任何原有字段、锚点、别名或 params 等结构,仅对需要本地化的字符串值进行翻译。
- aliases、keywords、tags 等列表字段的值,按原文档格式保留(保持原有引号使用情况,不统一添加或移除引号)。
- 必须输出且仅输出一次完整的 YAML Front Matter(以 --- 开头,以 --- 结尾)。
- 结尾的 --- 必须独占一行,且在其后必须紧跟一个空行。
- 绝对不要在翻译后的 Front Matter 之前或之后保留原始的英文 Front Matter。
6. 翻译要准确、专业、易懂,技术术语使用业界通用中文表达。
7. 绝对禁止在文档开头或 YAML frontmatter 前后添加任何 Markdown 代码块标记(如 ```markdown 或 ```),保持文档结构的纯净。
8. 绝对禁止自作主张地添加任何原文档中不存在的内容,包括但不限于:完整的语言指南、示例代码、学习资源、最佳实践等。只翻译原文档中已有的内容,不多不少。
9. 只输出翻译后的完整 Markdown 内容,不要添加任何说明、注释或多余文本。
10. Markdown 链接 `[text](url)` 中的 `url` 必须完全保持原样,绝对不要修改、翻译或移除路径中的任何部分。即使是相对路径或绝对路径,也都必须保持原样。
"""
[[providers]]
name = "grok"
api_key = "xxx"
base_url = "https://api.x.ai/v1"
model = "grok-3"
concurrency = 4 # 线程数
rate_delay = 3.0 # 每个请求后等待 1.0 秒(可根据限流调整)
| 1 | # 根目录 - 包含需要翻译的 Markdown 文件 |
| 2 | root_dir = "./docs" |
| 3 | |
| 4 | # 输出目录 - 翻译后的文件保存位置 |
| 5 | output_dir = "./docs_zh" |
| 6 | |
| 7 | # 输出模式: "overwrite" (覆盖原文件) 或 "new_folder" (保存到新文件夹) |
| 8 | output_mode = "new_folder" |
| 9 | |
| 10 | # 排除的目录 (逗号分隔) |
| 11 | exclude_dir = "node_modules,.git,_build" |
| 12 | |
| 13 | # 白名单文件扩展名 (逗号分隔,留空表示使用默认支持的格式) |
| 14 | # whitelist_extensions = "md,txt,srt" |
| 15 | |
| 16 | # 黑名单文件扩展名 (逗号分隔,留空表示不排除任何格式) |
| 17 | # blacklist_extensions = "pdf,docx" |
| 18 | |
| 19 | # 最大 token 数 |
| 20 | max_tokens = 99999 |
| 21 | |
| 22 | # 大文件拆分阈值 (单位:字符) |
| 23 | max_chunk_size = 8192 |
| 24 | |
| 25 | # 系统提示词(支持多行,使用 """ 包裹) |
| 26 | system_prompt = """ |
| 27 | 你是一个专业的技术文档翻译专家。请将以下英文 Markdown 文档翻译成流畅、自然的简体中文。 |
| 28 | |
| 29 | 严格要求: |
| 30 | 1. 保留完整的 Markdown 格式,包括标题、列表、表格、代码块、链接、图片等一切结构完全不变。 |
| 31 | 2. 代码块、命令行、文件名、路径、API 名称、配置文件内容、技术术语等保持原样(不要翻译)。 |
| 32 | 3. 专有名词(如产品名、框架名、软件名,例如 Traefik、Docker、Kubernetes)保持英文原名。 |
| 33 | 4. Hugo 短代码和模板语法(如 {{< xxx >}}、{{ xxx }}、{{</ xxx >}} 等所有以 {{ 或 {{< 开头的标签)必须完整保留原样,不翻译、不修改其内部任何内容。 |
| 34 | 5. 对于 YAML frontmatter(文档开头的 --- 之间的内容): |
| 35 | - 只翻译实际需要本地化的字符串值内容(如 title、description、linkTitle 等字段的值)。 |
| 36 | - tags 字段的值(通常为字符串或字符串列表)必须完全保持原英文不变,不进行任何翻译或修改。 |
| 37 | - 其他字段(如 keywords 等)按正常规则处理:如果为字符串值则翻译(如 description),如果为列表则视情况保留英文(专有名词不翻译)。 |
| 38 | - 严格保留原文档中的所有字段、键名、结构、格式、缩进和所有机制(包括已有的 YAML 锚点 &xxx 和别名 *xxx)。 |
| 39 | - 如果原文档中已有锚点定义,则在翻译该锚点对应的字符串值时保留锚点标记(如 &desc);别名引用(如 *desc)保持原样不变。 |
| 40 | - 如果原文档中没有锚点或别名,则翻译后绝对不得新增任何锚点、别名、params 块或其他额外字段。 |
| 41 | - 绝对不得添加、删除或修改任何原有字段、锚点、别名或 params 等结构,仅对需要本地化的字符串值进行翻译。 |
| 42 | - aliases、keywords、tags 等列表字段的值,按原文档格式保留(保持原有引号使用情况,不统一添加或移除引号)。 |
| 43 | - 必须输出且仅输出一次完整的 YAML Front Matter(以 --- 开头,以 --- 结尾)。 |
| 44 | - 结尾的 --- 必须独占一行,且在其后必须紧跟一个空行。 |
| 45 | - 绝对不要在翻译后的 Front Matter 之前或之后保留原始的英文 Front Matter。 |
| 46 | 6. 翻译要准确、专业、易懂,技术术语使用业界通用中文表达。 |
| 47 | 7. 绝对禁止在文档开头或 YAML frontmatter 前后添加任何 Markdown 代码块标记(如 ```markdown 或 ```),保持文档结构的纯净。 |
| 48 | 8. 绝对禁止自作主张地添加任何原文档中不存在的内容,包括但不限于:完整的语言指南、示例代码、学习资源、最佳实践等。只翻译原文档中已有的内容,不多不少。 |
| 49 | 9. 只输出翻译后的完整 Markdown 内容,不要添加任何说明、注释或多余文本。 |
| 50 | 10. Markdown 链接 `[text](url)` 中的 `url` 必须完全保持原样,绝对不要修改、翻译或移除路径中的任何部分。即使是相对路径或绝对路径,也都必须保持原样。 |
| 51 | """ |
| 52 | |
| 53 | [[providers]] |
| 54 | name = "grok" |
| 55 | api_key = "xxx" |
| 56 | base_url = "https://api.x.ai/v1" |
| 57 | model = "grok-3" |
| 58 | concurrency = 4 # 线程数 |
| 59 | rate_delay = 3.0 # 每个请求后等待 1.0 秒(可根据限流调整) |
deploy.sh
· 14 KiB · Bash
Sin formato
#!/usr/bin/env bash
#============================================================
# File: deploy.sh
# Description: 部署中文文档
# URL:
# Author: Jetsung Chan <[email protected]>
# Version: 0.2.0
# CreatedAt: 2025-12-16
# UpdatedAt: 2026-01-29
#============================================================
if [[ -n "${DEBUG:-}" ]]; then
set -eux
else
set -euo pipefail
fi
DEFAULT_BRANCH="${BRANCH:-main}"
HUGO_VERSION=${HUGO:-0.154.2}
DELETE_FILE="deleted_docs.txt"
ADD_FILE="new_docs.txt"
MODIFIED_FILE="modified_docs.txt"
TRANSLATE_LIST="translate_list.txt"
UPSTREAM_URL="${UPSTREAM_URL:-}"
UPSTREAM_NAME="upstream"
# 命令行参数
CUSTOM_BRANCH=""
CUSTOM_UPSTREAM=""
install_hugo() {
echo "======================================"
echo "📦 开始安装 Hugo"
echo "======================================"
echo "→ 目标版本: $HUGO_VERSION"
echo "→ 正在下载并安装..."
curl -L https://fx4.cn/hugo | bash -s -- -v "$HUGO_VERSION" -w
echo "✓ Hugo 安装完成"
hugo version
echo "======================================"
}
setup_config() {
echo "======================================"
echo "⚙️ 开始设置配置文件"
echo "======================================"
if [[ -f config.example.toml ]]; then
echo "→ 从 config.example.toml 提取基础配置..."
sed '/providers/,$d' ./config.example.toml | tee config.toml > /dev/null
echo "✓ 基础配置提取完成"
else
echo "⚠️ config.example.toml 不存在,跳过基础配置"
fi
if [[ -f aitr.toml ]]; then
echo "→ 从 aitr.toml 追加日志配置..."
sed -n '/logging/,$p' aitr.toml | tee -a config.toml > /dev/null
echo "✓ 日志配置追加完成"
else
echo "⚠️ aitr.toml 不存在,跳过日志配置"
fi
echo "✓ 配置文件设置完成"
echo "======================================"
}
sync_source() {
echo "======================================"
echo "📚 开始同步源文档"
echo "======================================"
echo "→ 准备工作目录..."
[[ -d docsite ]] || mkdir docsite
pushd docsite > /dev/null
if [[ ! -d .git ]]; then
echo "→ 初始化 git 仓库..."
git init
echo "→ 添加 upstream remote: ${UPSTREAM_URL}"
git remote add "${UPSTREAM_NAME}" "${UPSTREAM_URL}"
else
# 检查是否已经存在 upstream
if ! git remote get-url "${UPSTREAM_NAME}" >/dev/null 2>&1; then
echo "→ 添加 upstream remote..."
git remote add "${UPSTREAM_NAME}" "${UPSTREAM_URL}"
else
current_url=$(git remote get-url "${UPSTREAM_NAME}")
if [[ "${current_url}" != "${UPSTREAM_URL}" ]]; then
echo "→ 更新 upstream URL"
echo " 旧 URL: ${current_url}"
echo " 新 URL: ${UPSTREAM_URL}"
git remote set-url "${UPSTREAM_NAME}" "${UPSTREAM_URL}"
fi
fi
fi
echo "→ 正在拉取最新代码..."
echo " 分支: $DEFAULT_BRANCH"
echo " Remote: ${UPSTREAM_NAME}"
git reset --hard
git fetch "${UPSTREAM_NAME}" "${DEFAULT_BRANCH}"
git merge "${UPSTREAM_NAME}"/"${DEFAULT_BRANCH}"
COMMIT_SHA=$(git rev-parse --short HEAD)
echo "$COMMIT_SHA" > ../commit.txt
echo "✓ 代码同步完成 (commit: $COMMIT_SHA)"
popd > /dev/null
echo "→ 清理旧文档目录..."
rm -rf docs
echo "→ 复制源文档..."
if [[ -d docsite/content ]]; then
cp -r docsite/content .
mv content docs
echo "✓ 已从 docsite/content 复制文档"
elif [[ -d docsite/docs ]]; then
cp -r docsite/docs .
echo "✓ 已从 docsite/docs 复制文档"
else
echo "❌ docsite 目录下没有 content 或 docs 目录,无法同步文档。" >&2
exit 1
fi
echo "✓ 源文档同步完成"
echo "======================================"
}
# 增量更新
update_incremental() {
echo "======================================"
echo "🔄 开始增量更新流程"
echo "======================================"
if [[ -z "$UPSTREAM_URL" ]]; then
echo "❌ UPSTREAM_URL 未设置,无法进行增量更新。" >&2
exit 1
fi
echo "→ 步骤 1/6: 同步源文档"
sync_source
# 记录删除的文件
echo "→ 步骤 2/6: 检测已删除的文件"
echo " 正在扫描已删除的文档..."
git ls-files --deleted docs/ | tee "$DELETE_FILE"
ROOT_DIR=$(grep root_dir config.toml | cut -d'"' -f 2 | sed 's|^\./||')
export ROOT_DIR
OUTPUT_DIR=$(grep output_dir config.toml | cut -d'"' -f 2 | sed 's|^\./||')
export OUTPUT_DIR
echo " 根目录: $ROOT_DIR"
echo " 输出目录: $OUTPUT_DIR"
# sed -i "s|^$ROOT_DIR/|$OUTPUT_DIR/|g" "$DELETE_FILE"
# 删除对应的输出文件
echo " 正在清理对应的输出文件..."
while read -r file; do
new_file="${file/$ROOT_DIR/$OUTPUT_DIR}"
echo " 删除: $new_file"
rm -rf "$new_file" || true
done < "$DELETE_FILE"
echo "✓ 已清理删除的文件"
# 更新 git 索引
echo "→ 步骤 3/6: 更新 git 索引"
git add .
# 记录新增和修改的文件
echo " 正在检测新增的文件..."
git diff --cached --name-only --diff-filter=A docs/ | tee "$ADD_FILE"
echo " 正在检测修改的文件..."
git diff --cached --name-only --diff-filter=M docs/ | tee "$MODIFIED_FILE"
echo "→ 步骤 4/6: 生成待翻译文件列表"
cat "$ADD_FILE" "$MODIFIED_FILE" | tee "$TRANSLATE_LIST"
# 移除以 .png .jpg .jpeg .gif .svg 结尾的文件
echo " 过滤图片文件..."
sed -i '/\.\(png\|jpg\|jpeg\|gif\|svg\)$/d' "$TRANSLATE_LIST"
echo "✓ 已生成待翻译文件列表: $TRANSLATE_LIST"
echo "→ 步骤 5/6: 设置配置文件"
setup_config
# # 翻译增量文件
# if ! command -v aitr &> /dev/null; then
# echo "正在安装 aitr ..."
# curl -L https://fx4.cn/aitr | bash
# fi
# 调用 aitr 进行翻译
echo "→ 步骤 6/6: 执行翻译"
if command -v aitr &> /dev/null; then
echo " 正在调用 aitr 翻译文件..."
aitr --input "$TRANSLATE_LIST" --list --output translated
echo " 正在复制翻译结果到输出目录..."
cp -r "translated/${ROOT_DIR}/"* "${OUTPUT_DIR}"/
echo "✓ 翻译完成"
else
echo "⚠️ aitr 未安装,跳过翻译步骤。"
fi
echo "======================================"
echo "✓ 增量更新完成"
echo "======================================"
}
# 部署 docs_zh 到 docsite
deploy_docs_zh() {
echo "======================================"
echo "📋 开始部署 docs_zh 到 docsite"
echo "======================================"
if [[ ! -d docsite ]]; then
if [[ -z "$UPSTREAM_URL" ]]; then
echo "❌ UPSTREAM_URL 未设置,无法克隆文档站点。" >&2
exit 1
fi
echo "→ docsite 目录不存在,正在克隆..."
git clone "$UPSTREAM_URL" docsite
echo "✓ 克隆完成"
fi
if [[ -d "translated/docs" ]]; then
echo "→ 检测到 translated/docs,正在覆盖 docs_zh..."
cp -r translated/docs/* docs_zh/
echo "✓ 已覆盖 docs_zh"
fi
if [[ ! -d docs_zh ]]; then
echo "❌ docs_zh 目录不存在,无法部署。" >&2
exit 1
fi
echo "→ 正在部署 docs_zh 到 docsite..."
if [[ -d docsite/content ]]; then
cp -r docs_zh/* ./docsite/content/
echo "✓ 已部署到 docsite/content/"
elif [[ -d docsite/docs ]]; then
cp -r docs_zh/* ./docsite/docs/
echo "✓ 已部署到 docsite/docs/"
else
echo "❌ docsite 目录下没有 content 或 docs 目录,无法部署。" >&2
exit 1
fi
echo "======================================"
echo "✓ 文档部署完成"
echo "======================================"
}
# 更新项目信息
update_info() {
local repo_url="${1:-}"
local title="${2:-}"
if [[ -z "$repo_url" ]]; then
echo "❌ 缺少参数: repo_url" >&2
echo "示例: $0 --init https://github.com/user/repo.git [\"我的文档\"]"
exit 1
fi
echo "======================================"
echo "📝 开始更新项目信息"
echo "======================================"
echo "→ 仓库地址: $repo_url"
if [[ -n "$title" ]]; then
echo "→ 文档标题: $title 中文文档"
fi
if [[ -f README.md ]]; then
sed -i "s#https://github.com/xxx/docs.git#$repo_url#g" README.md
if [[ -n "$title" ]]; then
sed -i "s/^# 中文文档/# $title 中文文档/g" README.md
fi
echo "✓ README.md 已更新"
else
echo "⚠️ README.md 不存在,跳过"
fi
# 同步更新 docs.yml
if [[ -f .github/workflows/docs.yml ]]; then
sed -i "s#https://github.com/xxx/docs.git#$repo_url#g" .github/workflows/docs.yml
echo "✓ .github/workflows/docs.yml 已更新"
fi
echo "======================================"
echo "✓ 项目信息更新完成"
echo "======================================"
}
# 调用翻译脚本
translate() {
echo "======================================"
echo "🌐 开始执行翻译"
echo "======================================"
if ! command -v aitr &> /dev/null; then
echo "❌ aitr 未安装,请先安装 aitr。" >&2
exit 1
fi
echo "→ 正在调用 aitr 翻译工具..."
aitr
echo "======================================"
echo "✓ 翻译完成"
echo "======================================"
}
usage() {
cat << EOF
用法: $0 [选项]
选项:
-i --init 初始化项目信息 (替换仓库地址和标题)
-d --deploy 部署 docs_zh 到 docsite
-c --config 设置配置文件
-s --sync 同步源文档
-u --update 增量更新(同步+翻译)
-t --translate 执行翻译
-b --branch 指定分支 (默认: main)
-U --upstream 指定上游 URL
-h --help 显示此帮助信息
示例:
$0 --init https://github.com/user/repo.git "我的文档"
$0 --translate
$0 --update
$0 --sync --branch develop
$0 --update --upstream https://github.com/user/repo.git
EOF
}
main() {
echo "======================================"
echo "🚀 部署脚本启动"
echo "======================================"
if [[ $# -eq 0 ]]; then
echo "❌ 未指定任何操作" >&2
usage
exit 1
fi
# 先解析所有参数
local actions=()
local INIT_REPO=""
local INIT_TITLE=""
while [[ $# -gt 0 ]]; do
case $1 in
-i|--init)
INIT_REPO="$2"
# 检查 $3 是否存在且不是一个选项 (以 - 开头)
if [[ -n "${3:-}" && "${3:-}" != -* ]]; then
INIT_TITLE="$3"
shift 3
else
shift 2
fi
actions+=("init")
;;
-b|--branch)
CUSTOM_BRANCH="$2"
shift 2
;;
-U|--upstream)
CUSTOM_UPSTREAM="$2"
shift 2
;;
-d|--deploy)
actions+=("deploy")
shift
;;
-c|--config)
actions+=("config")
shift
;;
-s|--sync)
actions+=("sync")
shift
;;
-u|--update)
actions+=("update")
shift
;;
-t|--translate)
actions+=("translate")
shift
;;
--help|-h)
usage
exit 0
;;
*)
echo "❌ 未知参数: $1" >&2
usage
exit 1
;;
esac
done
# 应用自定义参数
if [[ -n "$CUSTOM_BRANCH" ]]; then
DEFAULT_BRANCH="$CUSTOM_BRANCH"
fi
if [[ -n "$CUSTOM_UPSTREAM" ]]; then
UPSTREAM_URL="$CUSTOM_UPSTREAM"
fi
echo "当前分支: $DEFAULT_BRANCH"
echo "Hugo 版本: $HUGO_VERSION"
if [[ -n "$UPSTREAM_URL" ]]; then
echo "Upstream URL: $UPSTREAM_URL"
else
echo "Upstream URL: (未设置)"
fi
echo "======================================"
# 执行操作
for action in "${actions[@]}"; do
case $action in
init)
echo "→ 执行操作: 初始化项目信息"
update_info "$INIT_REPO" "$INIT_TITLE"
;;
deploy)
echo "→ 执行操作: 部署 docs_zh"
deploy_docs_zh
;;
config)
echo "→ 执行操作: 设置配置文件"
setup_config
;;
sync)
echo "→ 执行操作: 同步源文档"
sync_source
;;
update)
echo "→ 执行操作: 增量更新"
update_incremental
;;
translate)
echo "→ 执行操作: 执行翻译"
translate
;;
esac
done
echo "======================================"
echo "✓ 部署脚本执行完成"
echo "======================================"
}
main "$@"
| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | #============================================================ |
| 4 | # File: deploy.sh |
| 5 | # Description: 部署中文文档 |
| 6 | # URL: |
| 7 | # Author: Jetsung Chan <[email protected]> |
| 8 | # Version: 0.2.0 |
| 9 | # CreatedAt: 2025-12-16 |
| 10 | # UpdatedAt: 2026-01-29 |
| 11 | #============================================================ |
| 12 | |
| 13 | |
| 14 | if [[ -n "${DEBUG:-}" ]]; then |
| 15 | set -eux |
| 16 | else |
| 17 | set -euo pipefail |
| 18 | fi |
| 19 | |
| 20 | DEFAULT_BRANCH="${BRANCH:-main}" |
| 21 | HUGO_VERSION=${HUGO:-0.154.2} |
| 22 | |
| 23 | DELETE_FILE="deleted_docs.txt" |
| 24 | ADD_FILE="new_docs.txt" |
| 25 | MODIFIED_FILE="modified_docs.txt" |
| 26 | TRANSLATE_LIST="translate_list.txt" |
| 27 | |
| 28 | UPSTREAM_URL="${UPSTREAM_URL:-}" |
| 29 | UPSTREAM_NAME="upstream" |
| 30 | |
| 31 | # 命令行参数 |
| 32 | CUSTOM_BRANCH="" |
| 33 | CUSTOM_UPSTREAM="" |
| 34 | |
| 35 | install_hugo() { |
| 36 | echo "======================================" |
| 37 | echo "📦 开始安装 Hugo" |
| 38 | echo "======================================" |
| 39 | echo "→ 目标版本: $HUGO_VERSION" |
| 40 | echo "→ 正在下载并安装..." |
| 41 | curl -L https://fx4.cn/hugo | bash -s -- -v "$HUGO_VERSION" -w |
| 42 | echo "✓ Hugo 安装完成" |
| 43 | hugo version |
| 44 | echo "======================================" |
| 45 | } |
| 46 | |
| 47 | setup_config() { |
| 48 | echo "======================================" |
| 49 | echo "⚙️ 开始设置配置文件" |
| 50 | echo "======================================" |
| 51 | if [[ -f config.example.toml ]]; then |
| 52 | echo "→ 从 config.example.toml 提取基础配置..." |
| 53 | sed '/providers/,$d' ./config.example.toml | tee config.toml > /dev/null |
| 54 | echo "✓ 基础配置提取完成" |
| 55 | else |
| 56 | echo "⚠️ config.example.toml 不存在,跳过基础配置" |
| 57 | fi |
| 58 | |
| 59 | if [[ -f aitr.toml ]]; then |
| 60 | echo "→ 从 aitr.toml 追加日志配置..." |
| 61 | sed -n '/logging/,$p' aitr.toml | tee -a config.toml > /dev/null |
| 62 | echo "✓ 日志配置追加完成" |
| 63 | else |
| 64 | echo "⚠️ aitr.toml 不存在,跳过日志配置" |
| 65 | fi |
| 66 | echo "✓ 配置文件设置完成" |
| 67 | echo "======================================" |
| 68 | } |
| 69 | |
| 70 | sync_source() { |
| 71 | echo "======================================" |
| 72 | echo "📚 开始同步源文档" |
| 73 | echo "======================================" |
| 74 | echo "→ 准备工作目录..." |
| 75 | [[ -d docsite ]] || mkdir docsite |
| 76 | |
| 77 | pushd docsite > /dev/null |
| 78 | if [[ ! -d .git ]]; then |
| 79 | echo "→ 初始化 git 仓库..." |
| 80 | git init |
| 81 | echo "→ 添加 upstream remote: ${UPSTREAM_URL}" |
| 82 | git remote add "${UPSTREAM_NAME}" "${UPSTREAM_URL}" |
| 83 | else |
| 84 | # 检查是否已经存在 upstream |
| 85 | if ! git remote get-url "${UPSTREAM_NAME}" >/dev/null 2>&1; then |
| 86 | echo "→ 添加 upstream remote..." |
| 87 | git remote add "${UPSTREAM_NAME}" "${UPSTREAM_URL}" |
| 88 | else |
| 89 | current_url=$(git remote get-url "${UPSTREAM_NAME}") |
| 90 | if [[ "${current_url}" != "${UPSTREAM_URL}" ]]; then |
| 91 | echo "→ 更新 upstream URL" |
| 92 | echo " 旧 URL: ${current_url}" |
| 93 | echo " 新 URL: ${UPSTREAM_URL}" |
| 94 | git remote set-url "${UPSTREAM_NAME}" "${UPSTREAM_URL}" |
| 95 | fi |
| 96 | fi |
| 97 | fi |
| 98 | echo "→ 正在拉取最新代码..." |
| 99 | echo " 分支: $DEFAULT_BRANCH" |
| 100 | echo " Remote: ${UPSTREAM_NAME}" |
| 101 | git reset --hard |
| 102 | git fetch "${UPSTREAM_NAME}" "${DEFAULT_BRANCH}" |
| 103 | git merge "${UPSTREAM_NAME}"/"${DEFAULT_BRANCH}" |
| 104 | COMMIT_SHA=$(git rev-parse --short HEAD) |
| 105 | echo "$COMMIT_SHA" > ../commit.txt |
| 106 | echo "✓ 代码同步完成 (commit: $COMMIT_SHA)" |
| 107 | popd > /dev/null |
| 108 | |
| 109 | echo "→ 清理旧文档目录..." |
| 110 | rm -rf docs |
| 111 | echo "→ 复制源文档..." |
| 112 | if [[ -d docsite/content ]]; then |
| 113 | cp -r docsite/content . |
| 114 | mv content docs |
| 115 | echo "✓ 已从 docsite/content 复制文档" |
| 116 | elif [[ -d docsite/docs ]]; then |
| 117 | cp -r docsite/docs . |
| 118 | echo "✓ 已从 docsite/docs 复制文档" |
| 119 | else |
| 120 | echo "❌ docsite 目录下没有 content 或 docs 目录,无法同步文档。" >&2 |
| 121 | exit 1 |
| 122 | fi |
| 123 | echo "✓ 源文档同步完成" |
| 124 | echo "======================================" |
| 125 | } |
| 126 | |
| 127 | # 增量更新 |
| 128 | update_incremental() { |
| 129 | echo "======================================" |
| 130 | echo "🔄 开始增量更新流程" |
| 131 | echo "======================================" |
| 132 | if [[ -z "$UPSTREAM_URL" ]]; then |
| 133 | echo "❌ UPSTREAM_URL 未设置,无法进行增量更新。" >&2 |
| 134 | exit 1 |
| 135 | fi |
| 136 | |
| 137 | echo "→ 步骤 1/6: 同步源文档" |
| 138 | sync_source |
| 139 | |
| 140 | # 记录删除的文件 |
| 141 | echo "→ 步骤 2/6: 检测已删除的文件" |
| 142 | echo " 正在扫描已删除的文档..." |
| 143 | git ls-files --deleted docs/ | tee "$DELETE_FILE" |
| 144 | ROOT_DIR=$(grep root_dir config.toml | cut -d'"' -f 2 | sed 's|^\./||') |
| 145 | export ROOT_DIR |
| 146 | OUTPUT_DIR=$(grep output_dir config.toml | cut -d'"' -f 2 | sed 's|^\./||') |
| 147 | export OUTPUT_DIR |
| 148 | echo " 根目录: $ROOT_DIR" |
| 149 | echo " 输出目录: $OUTPUT_DIR" |
| 150 | # sed -i "s|^$ROOT_DIR/|$OUTPUT_DIR/|g" "$DELETE_FILE" |
| 151 | # 删除对应的输出文件 |
| 152 | echo " 正在清理对应的输出文件..." |
| 153 | while read -r file; do |
| 154 | new_file="${file/$ROOT_DIR/$OUTPUT_DIR}" |
| 155 | echo " 删除: $new_file" |
| 156 | rm -rf "$new_file" || true |
| 157 | done < "$DELETE_FILE" |
| 158 | echo "✓ 已清理删除的文件" |
| 159 | |
| 160 | # 更新 git 索引 |
| 161 | echo "→ 步骤 3/6: 更新 git 索引" |
| 162 | git add . |
| 163 | # 记录新增和修改的文件 |
| 164 | echo " 正在检测新增的文件..." |
| 165 | git diff --cached --name-only --diff-filter=A docs/ | tee "$ADD_FILE" |
| 166 | echo " 正在检测修改的文件..." |
| 167 | git diff --cached --name-only --diff-filter=M docs/ | tee "$MODIFIED_FILE" |
| 168 | |
| 169 | echo "→ 步骤 4/6: 生成待翻译文件列表" |
| 170 | cat "$ADD_FILE" "$MODIFIED_FILE" | tee "$TRANSLATE_LIST" |
| 171 | |
| 172 | # 移除以 .png .jpg .jpeg .gif .svg 结尾的文件 |
| 173 | echo " 过滤图片文件..." |
| 174 | sed -i '/\.\(png\|jpg\|jpeg\|gif\|svg\)$/d' "$TRANSLATE_LIST" |
| 175 | echo "✓ 已生成待翻译文件列表: $TRANSLATE_LIST" |
| 176 | |
| 177 | echo "→ 步骤 5/6: 设置配置文件" |
| 178 | setup_config |
| 179 | |
| 180 | # # 翻译增量文件 |
| 181 | # if ! command -v aitr &> /dev/null; then |
| 182 | # echo "正在安装 aitr ..." |
| 183 | # curl -L https://fx4.cn/aitr | bash |
| 184 | # fi |
| 185 | |
| 186 | # 调用 aitr 进行翻译 |
| 187 | echo "→ 步骤 6/6: 执行翻译" |
| 188 | if command -v aitr &> /dev/null; then |
| 189 | echo " 正在调用 aitr 翻译文件..." |
| 190 | aitr --input "$TRANSLATE_LIST" --list --output translated |
| 191 | echo " 正在复制翻译结果到输出目录..." |
| 192 | cp -r "translated/${ROOT_DIR}/"* "${OUTPUT_DIR}"/ |
| 193 | echo "✓ 翻译完成" |
| 194 | else |
| 195 | echo "⚠️ aitr 未安装,跳过翻译步骤。" |
| 196 | fi |
| 197 | echo "======================================" |
| 198 | echo "✓ 增量更新完成" |
| 199 | echo "======================================" |
| 200 | } |
| 201 | |
| 202 | # 部署 docs_zh 到 docsite |
| 203 | deploy_docs_zh() { |
| 204 | echo "======================================" |
| 205 | echo "📋 开始部署 docs_zh 到 docsite" |
| 206 | echo "======================================" |
| 207 | if [[ ! -d docsite ]]; then |
| 208 | if [[ -z "$UPSTREAM_URL" ]]; then |
| 209 | echo "❌ UPSTREAM_URL 未设置,无法克隆文档站点。" >&2 |
| 210 | exit 1 |
| 211 | fi |
| 212 | echo "→ docsite 目录不存在,正在克隆..." |
| 213 | git clone "$UPSTREAM_URL" docsite |
| 214 | echo "✓ 克隆完成" |
| 215 | fi |
| 216 | |
| 217 | if [[ -d "translated/docs" ]]; then |
| 218 | echo "→ 检测到 translated/docs,正在覆盖 docs_zh..." |
| 219 | cp -r translated/docs/* docs_zh/ |
| 220 | echo "✓ 已覆盖 docs_zh" |
| 221 | fi |
| 222 | |
| 223 | if [[ ! -d docs_zh ]]; then |
| 224 | echo "❌ docs_zh 目录不存在,无法部署。" >&2 |
| 225 | exit 1 |
| 226 | fi |
| 227 | |
| 228 | echo "→ 正在部署 docs_zh 到 docsite..." |
| 229 | if [[ -d docsite/content ]]; then |
| 230 | cp -r docs_zh/* ./docsite/content/ |
| 231 | echo "✓ 已部署到 docsite/content/" |
| 232 | elif [[ -d docsite/docs ]]; then |
| 233 | cp -r docs_zh/* ./docsite/docs/ |
| 234 | echo "✓ 已部署到 docsite/docs/" |
| 235 | else |
| 236 | echo "❌ docsite 目录下没有 content 或 docs 目录,无法部署。" >&2 |
| 237 | exit 1 |
| 238 | fi |
| 239 | echo "======================================" |
| 240 | echo "✓ 文档部署完成" |
| 241 | echo "======================================" |
| 242 | } |
| 243 | |
| 244 | # 更新项目信息 |
| 245 | update_info() { |
| 246 | local repo_url="${1:-}" |
| 247 | local title="${2:-}" |
| 248 | |
| 249 | if [[ -z "$repo_url" ]]; then |
| 250 | echo "❌ 缺少参数: repo_url" >&2 |
| 251 | echo "示例: $0 --init https://github.com/user/repo.git [\"我的文档\"]" |
| 252 | exit 1 |
| 253 | fi |
| 254 | |
| 255 | echo "======================================" |
| 256 | echo "📝 开始更新项目信息" |
| 257 | echo "======================================" |
| 258 | echo "→ 仓库地址: $repo_url" |
| 259 | if [[ -n "$title" ]]; then |
| 260 | echo "→ 文档标题: $title 中文文档" |
| 261 | fi |
| 262 | |
| 263 | if [[ -f README.md ]]; then |
| 264 | sed -i "s#https://github.com/xxx/docs.git#$repo_url#g" README.md |
| 265 | if [[ -n "$title" ]]; then |
| 266 | sed -i "s/^# 中文文档/# $title 中文文档/g" README.md |
| 267 | fi |
| 268 | echo "✓ README.md 已更新" |
| 269 | else |
| 270 | echo "⚠️ README.md 不存在,跳过" |
| 271 | fi |
| 272 | |
| 273 | # 同步更新 docs.yml |
| 274 | if [[ -f .github/workflows/docs.yml ]]; then |
| 275 | sed -i "s#https://github.com/xxx/docs.git#$repo_url#g" .github/workflows/docs.yml |
| 276 | echo "✓ .github/workflows/docs.yml 已更新" |
| 277 | fi |
| 278 | |
| 279 | echo "======================================" |
| 280 | echo "✓ 项目信息更新完成" |
| 281 | echo "======================================" |
| 282 | } |
| 283 | |
| 284 | # 调用翻译脚本 |
| 285 | translate() { |
| 286 | echo "======================================" |
| 287 | echo "🌐 开始执行翻译" |
| 288 | echo "======================================" |
| 289 | if ! command -v aitr &> /dev/null; then |
| 290 | echo "❌ aitr 未安装,请先安装 aitr。" >&2 |
| 291 | exit 1 |
| 292 | fi |
| 293 | echo "→ 正在调用 aitr 翻译工具..." |
| 294 | aitr |
| 295 | echo "======================================" |
| 296 | echo "✓ 翻译完成" |
| 297 | echo "======================================" |
| 298 | } |
| 299 | |
| 300 | usage() { |
| 301 | cat << EOF |
| 302 | 用法: $0 [选项] |
| 303 | |
| 304 | 选项: |
| 305 | -i --init 初始化项目信息 (替换仓库地址和标题) |
| 306 | -d --deploy 部署 docs_zh 到 docsite |
| 307 | -c --config 设置配置文件 |
| 308 | -s --sync 同步源文档 |
| 309 | -u --update 增量更新(同步+翻译) |
| 310 | -t --translate 执行翻译 |
| 311 | -b --branch 指定分支 (默认: main) |
| 312 | -U --upstream 指定上游 URL |
| 313 | -h --help 显示此帮助信息 |
| 314 | |
| 315 | 示例: |
| 316 | $0 --init https://github.com/user/repo.git "我的文档" |
| 317 | $0 --translate |
| 318 | $0 --update |
| 319 | $0 --sync --branch develop |
| 320 | $0 --update --upstream https://github.com/user/repo.git |
| 321 | EOF |
| 322 | } |
| 323 | |
| 324 | main() { |
| 325 | echo "======================================" |
| 326 | echo "🚀 部署脚本启动" |
| 327 | echo "======================================" |
| 328 | |
| 329 | if [[ $# -eq 0 ]]; then |
| 330 | echo "❌ 未指定任何操作" >&2 |
| 331 | usage |
| 332 | exit 1 |
| 333 | fi |
| 334 | |
| 335 | # 先解析所有参数 |
| 336 | local actions=() |
| 337 | local INIT_REPO="" |
| 338 | local INIT_TITLE="" |
| 339 | while [[ $# -gt 0 ]]; do |
| 340 | case $1 in |
| 341 | -i|--init) |
| 342 | INIT_REPO="$2" |
| 343 | # 检查 $3 是否存在且不是一个选项 (以 - 开头) |
| 344 | if [[ -n "${3:-}" && "${3:-}" != -* ]]; then |
| 345 | INIT_TITLE="$3" |
| 346 | shift 3 |
| 347 | else |
| 348 | shift 2 |
| 349 | fi |
| 350 | actions+=("init") |
| 351 | ;; |
| 352 | -b|--branch) |
| 353 | CUSTOM_BRANCH="$2" |
| 354 | shift 2 |
| 355 | ;; |
| 356 | -U|--upstream) |
| 357 | CUSTOM_UPSTREAM="$2" |
| 358 | shift 2 |
| 359 | ;; |
| 360 | -d|--deploy) |
| 361 | actions+=("deploy") |
| 362 | shift |
| 363 | ;; |
| 364 | -c|--config) |
| 365 | actions+=("config") |
| 366 | shift |
| 367 | ;; |
| 368 | -s|--sync) |
| 369 | actions+=("sync") |
| 370 | shift |
| 371 | ;; |
| 372 | -u|--update) |
| 373 | actions+=("update") |
| 374 | shift |
| 375 | ;; |
| 376 | -t|--translate) |
| 377 | actions+=("translate") |
| 378 | shift |
| 379 | ;; |
| 380 | --help|-h) |
| 381 | usage |
| 382 | exit 0 |
| 383 | ;; |
| 384 | *) |
| 385 | echo "❌ 未知参数: $1" >&2 |
| 386 | usage |
| 387 | exit 1 |
| 388 | ;; |
| 389 | esac |
| 390 | done |
| 391 | |
| 392 | # 应用自定义参数 |
| 393 | if [[ -n "$CUSTOM_BRANCH" ]]; then |
| 394 | DEFAULT_BRANCH="$CUSTOM_BRANCH" |
| 395 | fi |
| 396 | if [[ -n "$CUSTOM_UPSTREAM" ]]; then |
| 397 | UPSTREAM_URL="$CUSTOM_UPSTREAM" |
| 398 | fi |
| 399 | |
| 400 | echo "当前分支: $DEFAULT_BRANCH" |
| 401 | echo "Hugo 版本: $HUGO_VERSION" |
| 402 | if [[ -n "$UPSTREAM_URL" ]]; then |
| 403 | echo "Upstream URL: $UPSTREAM_URL" |
| 404 | else |
| 405 | echo "Upstream URL: (未设置)" |
| 406 | fi |
| 407 | echo "======================================" |
| 408 | |
| 409 | # 执行操作 |
| 410 | for action in "${actions[@]}"; do |
| 411 | case $action in |
| 412 | init) |
| 413 | echo "→ 执行操作: 初始化项目信息" |
| 414 | update_info "$INIT_REPO" "$INIT_TITLE" |
| 415 | ;; |
| 416 | deploy) |
| 417 | echo "→ 执行操作: 部署 docs_zh" |
| 418 | deploy_docs_zh |
| 419 | ;; |
| 420 | config) |
| 421 | echo "→ 执行操作: 设置配置文件" |
| 422 | setup_config |
| 423 | ;; |
| 424 | sync) |
| 425 | echo "→ 执行操作: 同步源文档" |
| 426 | sync_source |
| 427 | ;; |
| 428 | update) |
| 429 | echo "→ 执行操作: 增量更新" |
| 430 | update_incremental |
| 431 | ;; |
| 432 | translate) |
| 433 | echo "→ 执行操作: 执行翻译" |
| 434 | translate |
| 435 | ;; |
| 436 | esac |
| 437 | done |
| 438 | |
| 439 | echo "======================================" |
| 440 | echo "✓ 部署脚本执行完成" |
| 441 | echo "======================================" |
| 442 | } |
| 443 | |
| 444 | main "$@" |
docs.yml
· 2.6 KiB · YAML
Sin formato
name: Documentation
on:
push:
branches:
- docs
paths:
- 'docs_zh/**'
- '.github/workflows/docs.yml'
workflow_dispatch:
permissions:
contents: write
pages: write
id-token: write
concurrency:
group: pages
cancel-in-progress: false
defaults:
run:
shell: bash
jobs:
build:
env:
TZ: Asia/Shanghai
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Fetch Source
run: |
git clone https://github.com/xxx/docs.git docsite
cd docsite
git checkout $(cat ../commit.txt)
- name: Copy Chinese Docs
run: cp -r docs_zh/* ./docsite/docs/
- name: Build Site
working-directory: ./docsite
run: |
echo "Building documentation..."
- name: Copy CNAME
run: cp CNAME docsite/public/
- name: Upload build artifacts
uses: actions/upload-artifact@v6
with:
name: docs-site
path: docsite/public
retention-days: 1
deploy-github-pages:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
needs: build
runs-on: ubuntu-latest
steps:
# - uses: actions/configure-pages@v5
- name: Download build artifacts
uses: actions/download-artifact@v7
with:
name: docs-site
path: ./public
- name: Deploy to Branch
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public
# - uses: actions/upload-pages-artifact@v4
# with:
# path: docsite/public
# - uses: actions/deploy-pages@v4
# id: deployment
deploy-ssh-server:
needs: build
runs-on: ubuntu-latest
steps:
- name: Download build artifacts
uses: actions/download-artifact@v7
with:
name: docs-site
path: ./public
- name: Compress to tar.xz
run: |
tar -cJf docs-site.tar.xz public/
- name: Set up SSH
uses: LuisEnMarroquin/[email protected]
with:
ORIGIN: ${{ secrets.SSH_HOSTNAME }}
SSHKEY: ${{ secrets.SSH_KEY }}
NAME: webserver
PORT: ${{ secrets.SSH_PORT }}
USER: ${{ secrets.SSH_USERNAME }}
- run: echo "" >> ~/.ssh/access # fix
- name: Deploy the website
env:
WEBROOT: /data/wwwroot/MYDOCSITE
run: |
ssh webserver "rm -rf $WEBROOT && mkdir -p $WEBROOT"
scp docs-site.tar.xz webserver:${WEBROOT}/
ssh webserver "cd $WEBROOT && tar -xJf docs-site.tar.xz --strip-components=1 && rm -f docs-site.tar.xz"
| 1 | name: Documentation |
| 2 | |
| 3 | on: |
| 4 | push: |
| 5 | branches: |
| 6 | - docs |
| 7 | paths: |
| 8 | - 'docs_zh/**' |
| 9 | - '.github/workflows/docs.yml' |
| 10 | workflow_dispatch: |
| 11 | |
| 12 | permissions: |
| 13 | contents: write |
| 14 | pages: write |
| 15 | id-token: write |
| 16 | |
| 17 | concurrency: |
| 18 | group: pages |
| 19 | cancel-in-progress: false |
| 20 | defaults: |
| 21 | run: |
| 22 | shell: bash |
| 23 | |
| 24 | jobs: |
| 25 | build: |
| 26 | env: |
| 27 | TZ: Asia/Shanghai |
| 28 | runs-on: ubuntu-latest |
| 29 | steps: |
| 30 | - uses: actions/checkout@v6 |
| 31 | - name: Fetch Source |
| 32 | run: | |
| 33 | git clone https://github.com/xxx/docs.git docsite |
| 34 | cd docsite |
| 35 | git checkout $(cat ../commit.txt) |
| 36 | - name: Copy Chinese Docs |
| 37 | run: cp -r docs_zh/* ./docsite/docs/ |
| 38 | - name: Build Site |
| 39 | working-directory: ./docsite |
| 40 | run: | |
| 41 | echo "Building documentation..." |
| 42 | - name: Copy CNAME |
| 43 | run: cp CNAME docsite/public/ |
| 44 | - name: Upload build artifacts |
| 45 | uses: actions/upload-artifact@v6 |
| 46 | with: |
| 47 | name: docs-site |
| 48 | path: docsite/public |
| 49 | retention-days: 1 |
| 50 | |
| 51 | deploy-github-pages: |
| 52 | environment: |
| 53 | name: github-pages |
| 54 | url: ${{ steps.deployment.outputs.page_url }} |
| 55 | needs: build |
| 56 | runs-on: ubuntu-latest |
| 57 | steps: |
| 58 | # - uses: actions/configure-pages@v5 |
| 59 | - name: Download build artifacts |
| 60 | uses: actions/download-artifact@v7 |
| 61 | with: |
| 62 | name: docs-site |
| 63 | path: ./public |
| 64 | - name: Deploy to Branch |
| 65 | uses: peaceiris/actions-gh-pages@v4 |
| 66 | with: |
| 67 | github_token: ${{ secrets.GITHUB_TOKEN }} |
| 68 | publish_dir: ./public |
| 69 | # - uses: actions/upload-pages-artifact@v4 |
| 70 | # with: |
| 71 | # path: docsite/public |
| 72 | # - uses: actions/deploy-pages@v4 |
| 73 | # id: deployment |
| 74 | |
| 75 | deploy-ssh-server: |
| 76 | needs: build |
| 77 | runs-on: ubuntu-latest |
| 78 | steps: |
| 79 | - name: Download build artifacts |
| 80 | uses: actions/download-artifact@v7 |
| 81 | with: |
| 82 | name: docs-site |
| 83 | path: ./public |
| 84 | - name: Compress to tar.xz |
| 85 | run: | |
| 86 | tar -cJf docs-site.tar.xz public/ |
| 87 | - name: Set up SSH |
| 88 | uses: LuisEnMarroquin/[email protected] |
| 89 | with: |
| 90 | ORIGIN: ${{ secrets.SSH_HOSTNAME }} |
| 91 | SSHKEY: ${{ secrets.SSH_KEY }} |
| 92 | NAME: webserver |
| 93 | PORT: ${{ secrets.SSH_PORT }} |
| 94 | USER: ${{ secrets.SSH_USERNAME }} |
| 95 | - run: echo "" >> ~/.ssh/access # fix |
| 96 | - name: Deploy the website |
| 97 | env: |
| 98 | WEBROOT: /data/wwwroot/MYDOCSITE |
| 99 | run: | |
| 100 | ssh webserver "rm -rf $WEBROOT && mkdir -p $WEBROOT" |
| 101 | scp docs-site.tar.xz webserver:${WEBROOT}/ |
| 102 | ssh webserver "cd $WEBROOT && tar -xJf docs-site.tar.xz --strip-components=1 && rm -f docs-site.tar.xz" |
init.sh
· 557 B · Bash
Sin formato
#!/usr/bin/env bash
if [[ -n "${DEBUG:-}" ]]; then
set -eux
else
set -euo pipefail
fi
BASE_URL="https://gist.asfd.cn/jetsung/gendocs/raw/HEAD"
download_files() {
curl -fsSL -O "${BASE_URL}/deploy.sh"
chmod +x deploy.sh
curl -fsSL -o README.md "${BASE_URL}/DOCS.md"
curl -fsSL -O "${BASE_URL}/config.example.toml"
curl -fsSL -O "${BASE_URL}/.gitignore"
curl -fsSL --create-dirs -o ".github/workflows/docs.yml" "${BASE_URL}/docs.yml"
}
main() {
download_files
if [[ ! -f CNAME ]]; then
echo "" > CNAME
fi
}
main "$@"
| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | if [[ -n "${DEBUG:-}" ]]; then |
| 4 | set -eux |
| 5 | else |
| 6 | set -euo pipefail |
| 7 | fi |
| 8 | |
| 9 | BASE_URL="https://gist.asfd.cn/jetsung/gendocs/raw/HEAD" |
| 10 | |
| 11 | download_files() { |
| 12 | curl -fsSL -O "${BASE_URL}/deploy.sh" |
| 13 | chmod +x deploy.sh |
| 14 | |
| 15 | curl -fsSL -o README.md "${BASE_URL}/DOCS.md" |
| 16 | |
| 17 | curl -fsSL -O "${BASE_URL}/config.example.toml" |
| 18 | |
| 19 | curl -fsSL -O "${BASE_URL}/.gitignore" |
| 20 | |
| 21 | curl -fsSL --create-dirs -o ".github/workflows/docs.yml" "${BASE_URL}/docs.yml" |
| 22 | } |
| 23 | |
| 24 | main() { |
| 25 | download_files |
| 26 | |
| 27 | if [[ ! -f CNAME ]]; then |
| 28 | echo "" > CNAME |
| 29 | fi |
| 30 | } |
| 31 | |
| 32 | main "$@" |
| 33 |