Skip to content

Commit 4e0b906

Browse files
authored
feat(ci): add GitHub Actions workflow to notify parent repository on updates
1 parent 1429de3 commit 4e0b906

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

.github/workflows/push.yml

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# ═══════════════════════════════════════════════════════════════════════════════
2+
# 组件仓库 GitHub Actions 配置模板
3+
# ═══════════════════════════════════════════════════════════════════════════════
4+
#
5+
# 此文件用于子仓库,当子仓库有更新时通知主仓库进行 subtree pull 同步。
6+
#
7+
# 【使用步骤】
8+
# ─────────────────────────────────────────────────────────────────────────────
9+
# 1. 将此文件复制到子仓库的 .github/workflows/ 目录:
10+
# cp scripts/push.yml <子仓库>/.github/workflows/push.yml
11+
#
12+
# 2. 在子仓库中配置 Secret:
13+
# GitHub 仓库 → Settings → Secrets → Actions → New repository secret
14+
# 名称: PARENT_REPO_TOKEN
15+
# 值: 具有主仓库 repo 权限的 Personal Access Token
16+
#
17+
# 3. 修改下方 env 块中的一个变量(标注了「需要修改」的行):
18+
# PARENT_REPO - 主仓库路径,例如 rcore-os/tgoskits
19+
# (subtree 目录由主仓库自动从 git 历史中推断,无需手动指定)
20+
#
21+
# 【Token 权限要求】
22+
# ─────────────────────────────────────────────────────────────────────────────
23+
# PARENT_REPO_TOKEN 需要 Classic Personal Access Token,权限包括:
24+
# - repo (Full control of private repositories)
25+
#
26+
# - Fine-grained token: Contents (Read and Write)
27+
#
28+
# 【触发条件】
29+
# ─────────────────────────────────────────────────────────────────────────────
30+
# - 自动触发:推送到 dev 或 main 分支时
31+
# - 手动触发:Actions → Notify Parent Repository → Run workflow
32+
#
33+
# 【工作流程】
34+
# ─────────────────────────────────────────────────────────────────────────────
35+
# 子仓库 push → 触发此工作流 → 调用主仓库 API → 主仓库 subtree pull
36+
#
37+
# 【注意事项】
38+
# ─────────────────────────────────────────────────────────────────────────────
39+
# - 主仓库需要配置接收 repository_dispatch 事件的同步工作流
40+
# - 如果不需要子仓库到主仓库的同步,可以不使用此文件
41+
#
42+
# ═══════════════════════════════════════════════════════════════════════════════
43+
44+
name: Notify Parent Repository
45+
46+
# 当有新的推送时触发
47+
on:
48+
push:
49+
branches:
50+
- main
51+
- master
52+
workflow_dispatch:
53+
54+
jobs:
55+
notify:
56+
runs-on: ubuntu-latest
57+
steps:
58+
- name: Get repository info
59+
id: repo
60+
env:
61+
GH_REPO_NAME: ${{ github.event.repository.name }}
62+
GH_REF_NAME: ${{ github.ref_name }}
63+
GH_SERVER_URL: ${{ github.server_url }}
64+
GH_REPOSITORY: ${{ github.repository }}
65+
run: |
66+
# 直接使用 GitHub Actions 内置变量,通过 env 传入避免 shell 注入
67+
COMPONENT="$GH_REPO_NAME"
68+
BRANCH="$GH_REF_NAME"
69+
# 构造标准 HTTPS URL,供主仓库按 URL 精确匹配 repos.list
70+
REPO_URL="${GH_SERVER_URL}/${GH_REPOSITORY}"
71+
72+
echo "component=${COMPONENT}" >> $GITHUB_OUTPUT
73+
echo "branch=${BRANCH}" >> $GITHUB_OUTPUT
74+
echo "repo_url=${REPO_URL}" >> $GITHUB_OUTPUT
75+
76+
echo "Component: ${COMPONENT}"
77+
echo "Branch: ${BRANCH}"
78+
echo "Repo URL: ${REPO_URL}"
79+
80+
- name: Notify parent repository
81+
env:
82+
# ── 需要修改 ──────────────────────────────────────────────────────────
83+
PARENT_REPO: "rcore-os/tgoskits" # 主仓库路径
84+
# ── 无需修改 ──────────────────────────────────────────────────────────
85+
DISPATCH_TOKEN: ${{ secrets.PARENT_REPO_TOKEN }}
86+
# 将用户可控内容通过 env 传入,避免直接插值到 shell 脚本
87+
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
88+
GIT_ACTOR: ${{ github.actor }}
89+
GIT_SHA: ${{ github.sha }}
90+
STEP_COMPONENT: ${{ steps.repo.outputs.component }}
91+
STEP_BRANCH: ${{ steps.repo.outputs.branch }}
92+
STEP_REPO_URL: ${{ steps.repo.outputs.repo_url }}
93+
run: |
94+
COMPONENT="$STEP_COMPONENT"
95+
BRANCH="$STEP_BRANCH"
96+
REPO_URL="$STEP_REPO_URL"
97+
98+
echo "Notifying parent repository about update in ${COMPONENT}:${BRANCH}"
99+
100+
# 使用 jq 安全构建 JSON,避免 commit message 中任何特殊字符导致注入
101+
PAYLOAD=$(jq -n \
102+
--arg component "$COMPONENT" \
103+
--arg branch "$BRANCH" \
104+
--arg repo_url "$REPO_URL" \
105+
--arg commit "$GIT_SHA" \
106+
--arg message "$COMMIT_MESSAGE" \
107+
--arg author "$GIT_ACTOR" \
108+
'{
109+
event_type: "subtree-update",
110+
client_payload: {
111+
component: $component,
112+
branch: $branch,
113+
repo_url: $repo_url,
114+
commit: $commit,
115+
message: $message,
116+
author: $author
117+
}
118+
}')
119+
120+
curl --fail --show-error -X POST \
121+
-H "Accept: application/vnd.github.v3+json" \
122+
-H "Authorization: token ${DISPATCH_TOKEN}" \
123+
https://api.github.com/repos/${PARENT_REPO}/dispatches \
124+
-d "$PAYLOAD"
125+
126+
echo "Notification sent successfully"
127+
128+
- name: Create summary
129+
env:
130+
STEP_COMPONENT: ${{ steps.repo.outputs.component }}
131+
STEP_BRANCH: ${{ steps.repo.outputs.branch }}
132+
STEP_REPO_URL: ${{ steps.repo.outputs.repo_url }}
133+
GIT_SHA: ${{ github.sha }}
134+
GIT_ACTOR: ${{ github.actor }}
135+
run: |
136+
COMPONENT="$STEP_COMPONENT"
137+
BRANCH="$STEP_BRANCH"
138+
REPO_URL="$STEP_REPO_URL"
139+
140+
echo "## Notification Summary" >> $GITHUB_STEP_SUMMARY
141+
echo "" >> $GITHUB_STEP_SUMMARY
142+
echo "- **Component**: ${COMPONENT}" >> $GITHUB_STEP_SUMMARY
143+
echo "- **Branch**: ${BRANCH}" >> $GITHUB_STEP_SUMMARY
144+
echo "- **Repo URL**: ${REPO_URL}" >> $GITHUB_STEP_SUMMARY
145+
echo "- **Commit**: \`${GIT_SHA}\`" >> $GITHUB_STEP_SUMMARY
146+
echo "- **Author**: ${GIT_ACTOR}" >> $GITHUB_STEP_SUMMARY
147+
echo "- **Status**: ✅ Notification sent" >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)