Skip to content

Commit 209920c

Browse files
committed
auto sync #3
1 parent 13a063a commit 209920c

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed

.github/workflows/sync.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# refusing to allow a GitHub App to create or update workflow .github/workflows/XXXX.yml without workflows permission
2+
# 遇到这个错误,需要检查三点:
3+
# 1. 仓库设置 Workflow permissions 允许写权限
4+
# 2. 使用的 TOKEN 具有 workflows 权限,如果是组织仓库还需要检查 TOKEN 的作用域
5+
# 3. actions/checkout 也需要使用 workflows 权限的 TOKEN
6+
# -------------------------------------------------------------------
7+
# https://github.com/Soltus
8+
9+
name: Test
10+
11+
on:
12+
workflow_call:
13+
inputs:
14+
target_sync_branch:
15+
description: "目标(下游)同步分支,默认为 github.ref ,根据实际情况修改"
16+
type: string
17+
default: ${{ github.ref }}
18+
upstream_sync_repo:
19+
description: "上游仓库,默认为 github.repository ,根据实际情况修改"
20+
type: string
21+
default: ${{ github.repository }}
22+
upstream_sync_branch:
23+
description: "上游同步分支,必须指定"
24+
required: true
25+
type: string
26+
sync_test_mode: # Adds a boolean option that appears during manual workflow run for easy test mode config
27+
description: 'Fork Sync Test Mode'
28+
type: boolean
29+
default: false
30+
31+
jobs:
32+
sync_latest_from_upstream:
33+
runs-on: ubuntu-latest
34+
name: Sync latest commits from upstream repo
35+
36+
steps:
37+
# REQUIRED step
38+
# Step 1: run a standard checkout action, provided by github
39+
- name: Checkout target repo
40+
uses: actions/checkout@v4
41+
with:
42+
fetch-depth: 0
43+
token: ${{ secrets.TOEKN_FOREVER_FULL_WRITE }} # 使用具有完全写入权限的 TOKEN(必须包含 workflows )
44+
# REQUIRED if your upstream repo is private (see wiki), 启用会造成 check-branch-exists 失败
45+
# persist-credentials: false
46+
47+
- name: Set up Git
48+
run: |
49+
git config --global user.name 'GitHub Actions'
50+
git config --global user.email '[email protected]'
51+
52+
- name: Check if target_sync_branch exists in the remote
53+
id: check-branch-exists
54+
run: |
55+
# grep -qE 使用了 -E 参数来启用扩展的正则表达式, 并且使用了 $ 符号来确保匹配的字符串以 main 结尾, 达到精准匹配的目的
56+
if git ls-remote --heads origin | grep -qE 'refs/heads/${{ inputs.target_sync_branch }}$'; then
57+
echo "branch_exists=true" >> $GITHUB_OUTPUT
58+
echo "Remote branch ${{ inputs.target_sync_branch }} exists."
59+
git checkout ${{ inputs.target_sync_branch }}
60+
else
61+
echo "branch_exists=false" >> $GITHUB_OUTPUT
62+
echo "Remote branch ${{ inputs.target_sync_branch }} does not exist."
63+
git remote add upstream https://github.com/${{ inputs.upstream_sync_repo }}.git
64+
# 创建一个临时分支来fetch远程分支的内容
65+
git fetch upstream ${{ inputs.upstream_sync_branch }}:temp_${{ inputs.upstream_sync_branch }}
66+
# 基于临时分支创建新的本地分支
67+
git checkout -b ${{ inputs.target_sync_branch }} temp_${{ inputs.upstream_sync_branch }}
68+
# 删除临时分支
69+
git branch -d temp_${{ inputs.upstream_sync_branch }}
70+
# 推送新分支到远程仓库
71+
git push origin ${{ inputs.target_sync_branch }}
72+
fi
73+
74+
# REQUIRED step
75+
# Step 2: run the sync action
76+
- name: Sync upstream changes
77+
id: sync
78+
if: steps.check-branch-exists.outputs.branch_exists == 'true'
79+
uses: aormsby/[email protected]
80+
with:
81+
target_sync_branch: ${{ inputs.target_sync_branch }}
82+
target_repo_token: ${{ secrets.TOEKN_FOREVER_FULL_WRITE }} # 可能涉及 workflows 的修改,需要对应权限
83+
upstream_sync_branch: ${{ inputs.upstream_sync_branch }}
84+
upstream_sync_repo: ${{ inputs.upstream_sync_repo }}
85+
test_mode: ${{ inputs.sync_test_mode }}
86+
87+
# Step 3: Display a sample message based on the sync output var 'has_new_commits'
88+
- name: New commits found
89+
if: steps.sync.outputs.has_new_commits == 'true'
90+
run: echo "New commits were found to sync."
91+
92+
- name: No new commits
93+
if: steps.sync.outputs.has_new_commits == 'false'
94+
run: echo "There were no new commits."
95+
96+
- name: Show value of 'has_new_commits'
97+
run: echo ${{ steps.sync.outputs.has_new_commits }}

.github/workflows/test.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,4 @@ jobs:
99
uses: ./.github/workflows/validation-proxy.yml
1010
secrets: inherit
1111
with:
12-
name: "test"
13-
sync:
14-
uses: prototype-test/dist-repo-use-branch_test/.github/workflows/test.yml@HEAD
15-
secrets: inherit
16-
needs: build
12+
name: "test"

.github/workflows/validation-proxy.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ jobs:
3434
cd_path: "${{ inputs.name }}/ci"
3535
cd_branch: "${{ needs.setup.outputs.cd_branch_prefix }}${{ inputs.name }}"
3636
dist_path: "${{ inputs.name }}/src"
37-
dist_branch: "${{ needs.setup.outputs.dist_branch_prefix }}${{ inputs.name }}"
37+
dist_branch: "${{ needs.setup.outputs.dist_branch_prefix }}${{ inputs.name }}"
38+
sync:
39+
uses: ./.github/workflows/sync.yml
40+
secrets: inherit
41+
needs: [setup, build]
42+
with:
43+
upstream_sync_branch: "${{ needs.setup.outputs.dist_branch_prefix }}${{ inputs.name }}"
44+
upstream_sync_repo: ${{ needs.setup.outputs.repository }}

0 commit comments

Comments
 (0)