|
1 | | -# GitHub Action 集成 |
| 1 | +# GitHub Action 同步 |
2 | 2 |
|
3 | 3 | ## 说明 |
4 | 4 |
|
5 | | -这个仓库用于提供组织的 Github Action 模板,并且在模板变化后同步到其他仓库 |
6 | | - |
7 | | -## 目录结构 |
| 5 | +这个Action用于同步模板文件到组织的所有仓库中,支持拆分配置文件,以实现增量同步。 |
| 6 | + |
| 7 | +## 输入 |
| 8 | + |
| 9 | +```yaml |
| 10 | +inputs: |
| 11 | + app_id: |
| 12 | + description: "github app id" |
| 13 | + required: true |
| 14 | + installation_id: |
| 15 | + description: "github app installation id" |
| 16 | + required: true |
| 17 | + private_key: |
| 18 | + description: "github app private key" |
| 19 | + required: true |
| 20 | + files: |
| 21 | + description: "config files" |
| 22 | + required: true |
| 23 | + message: |
| 24 | + description: "commit message" |
| 25 | + required: false |
| 26 | + default: "chore: Sync by .github" |
| 27 | +``` |
8 | 28 |
|
9 | | -```txt |
10 | | -├── .github |
11 | | -│ └── workflows |
12 | | -│ ├── teams-check.yml 自动触发的Action,用于在发起的PR 或push 中, 配置文件或Action模板变动时,检查yaml 的合法性 |
13 | | -│ ├── teams-update.yml 自动触发的Action,用于在配置文件或Action模板变动时,自动修改Teams 成员 |
14 | | -│ ├── jenkins-bridge.yml 可重用的Action文件,在check.yml中引用 |
15 | | -│ └── sync.yml 自动触发的Action,用于在配置文件或Action模板变动时,自动同步到其他仓库中 |
16 | | -├── go.mod |
17 | | -├── go.sum |
18 | | -├── main.go |
19 | | -├── README.md |
20 | | -├── teams.yaml 配置Teams |
21 | | -├── repos 同步配置,目录下文件发生变动,读取变动的配置,并触发该配置的变动 |
22 | | -│ └── peeweep-test |
23 | | -│ └── test-action.json |
24 | | -└── workflow-templates Action模板,在GitHub添加Action时可选择,修改目录下文件,会触发所有配置同步 |
25 | | - ├── check.properties.json |
26 | | - └── check.yml |
| 29 | +## 例子 |
| 30 | +
|
| 31 | +```yaml |
| 32 | +name: Sync |
| 33 | +on: |
| 34 | + push: |
| 35 | + paths: |
| 36 | + - ".github/workflows/sync.yml" |
| 37 | + - "repos/**" |
| 38 | + - "workflow-templates/**" |
| 39 | + workflow_dispatch: |
| 40 | + inputs: |
| 41 | + dry_run: |
| 42 | + description: "dry run" |
| 43 | + required: false |
| 44 | +jobs: |
| 45 | + sync: |
| 46 | + runs-on: ubuntu-latest |
| 47 | + steps: |
| 48 | + - uses: actions/checkout@v2 |
| 49 | + with: |
| 50 | + fetch-depth: 0 |
| 51 | + # 获取变动的配置文件 |
| 52 | + - name: Get changed configs |
| 53 | + id: changed-configs |
| 54 | + uses: tj-actions/changed-files@v16 |
| 55 | + with: |
| 56 | + separator: " " |
| 57 | + files: repos/** |
| 58 | + # 根据变动的配置文件,增量同步 |
| 59 | + - name: Sync changed configs |
| 60 | + uses: myml/action-sync@main |
| 61 | + if: steps.changed-configs.outputs.any_changed == 'true' |
| 62 | + with: |
| 63 | + app_id: 164400 |
| 64 | + installation_id: 22221748 |
| 65 | + private_key: ${{ secrets.APP_PRIVATE_KEY }} |
| 66 | + files: "${{ steps.changed-configs.outputs.all_changed_files }}" |
| 67 | + message: "chore: Sync by peeweep-test/.github" |
| 68 | + # 判断是否更改了模板文件或工作流配置 |
| 69 | + - name: Get changed files |
| 70 | + id: changed-files |
| 71 | + uses: tj-actions/changed-files@v16 |
| 72 | + with: |
| 73 | + files: | |
| 74 | + workflow-templates/** |
| 75 | + .github/workflows/sync.yml |
| 76 | + # 如果更改了模板文件,则使用所有配置文件,全量同步 |
| 77 | + - name: Get all configs |
| 78 | + id: all-configs |
| 79 | + if: steps.changed-files.outputs.any_changed == 'true' |
| 80 | + run: | |
| 81 | + all_configs=`find repos -type f | xargs` |
| 82 | + echo all configs $all_configs |
| 83 | + echo "::set-output name=ALL_CONFIGS::$all_configs" |
| 84 | + - name: Sync changed files |
| 85 | + uses: myml/action-sync@main |
| 86 | + if: steps.changed-files.outputs.any_changed == 'true' |
| 87 | + with: |
| 88 | + app_id: 164400 |
| 89 | + installation_id: 22221748 |
| 90 | + private_key: ${{ secrets.APP_PRIVATE_KEY }} |
| 91 | + files: "${{ steps.all-configs.outputs.ALL_CONFIGS }}" |
| 92 | + message: "chore: Sync by peeweep-test/.github" |
27 | 93 | ``` |
28 | 94 |
|
29 | 95 | ## 同步配置 |
|
36 | 102 | "src": "workflow-templates/check.yml", |
37 | 103 | "dest": "peeweep-test/test-action/.github/workflows/check.yml", |
38 | 104 | // 可选项,默认同步文件到所有分支 |
39 | | - "brache": ["main"] |
| 105 | + "branches": ["main"] |
40 | 106 | } |
41 | 107 | ] |
42 | 108 | ``` |
43 | 109 |
|
44 | 110 | 以上配置将 workflow-templates/check.yml 同步到 peeweep-test/test-action 仓库的 .github/workflows 目录下。 |
45 | 111 |
|
46 | | -虽然配置可写在 repos 目录下任意位置,但为了便于维护,建议以仓库路径为文件名 |
| 112 | +虽然配置可写在任意位置,但为了便于维护,建议以仓库路径为文件名 |
47 | 113 |
|
48 | 114 | 例如同步到 peeweep-test/test-action 仓库的配置文件建议放置到 repos/peeweep-test/test-action.json |
49 | | - |
50 | | -## Teams 配置 |
51 | | - |
52 | | -``` |
53 | | -teams: |
54 | | - admin: # team 名 |
55 | | - members: # team 成员 |
56 | | - - peeweep |
57 | | - dtkcore: |
58 | | - members: |
59 | | - - peeweep |
60 | | - reviewer: |
61 | | - members: |
62 | | - - myml |
63 | | - - peeweep |
64 | | -``` |
65 | | - |
66 | | -成员在team 中的权限默认为 member |
67 | | - |
68 | | -如果成员同时是组织的maintainer, 会自动升级为maintainer |
69 | | - |
70 | | -## TODO |
71 | | - |
72 | | -- [x] 同步流程手动触发会无权限同步文件 |
|
0 commit comments