Skip to content

Commit 9ef78d9

Browse files
committed
feat: prefer GitHub App auth for compliance workflows
1 parent a4f9123 commit 9ef78d9

8 files changed

Lines changed: 55 additions & 13 deletions

File tree

.github/workflows/cla-reusable.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ on:
5353
required: false
5454
default: I have read the ICLA and I hereby sign this agreement.
5555
type: string
56+
app-id:
57+
required: false
58+
default: ""
59+
type: string
5660

5761
jobs:
5862
cla:
@@ -132,10 +136,19 @@ jobs:
132136
core.setOutput("corporate_authorization_display_name", corporateAuthorizationDisplayName);
133137
core.setOutput("sign_comment", signComment);
134138
139+
- name: Create GitHub App token
140+
id: app_token
141+
if: ${{ inputs.app-id != '' && secrets.CLA_APP_PRIVATE_KEY != '' }}
142+
uses: actions/create-github-app-token@v2
143+
with:
144+
app-id: ${{ inputs.app-id }}
145+
private-key: ${{ secrets.CLA_APP_PRIVATE_KEY }}
146+
owner: ${{ github.repository_owner }}
147+
135148
- name: Persist signed contributors
136149
uses: actions/github-script@v8
137150
with:
138-
github-token: ${{ secrets.CLA_BOT_TOKEN != '' && secrets.CLA_BOT_TOKEN || secrets.GITHUB_TOKEN }}
151+
github-token: ${{ steps.app_token.outputs.token || secrets.CLA_BOT_TOKEN || secrets.GITHUB_TOKEN }}
139152
script: |
140153
const prNumber =
141154
context.payload.pull_request?.number ??
@@ -292,7 +305,7 @@ jobs:
292305
- name: Check and collect CLA signatures
293306
uses: contributor-assistant/github-action@v2.6.1
294307
env:
295-
GITHUB_TOKEN: ${{ secrets.CLA_BOT_TOKEN != '' && secrets.CLA_BOT_TOKEN || secrets.GITHUB_TOKEN }}
308+
GITHUB_TOKEN: ${{ steps.app_token.outputs.token || secrets.CLA_BOT_TOKEN || secrets.GITHUB_TOKEN }}
296309
with:
297310
path-to-document: https://github.com/${{ github.repository }}/blob/${{ inputs.default-branch }}/${{ inputs.icla-path }}
298311
path-to-signatures: ${{ inputs.signatures-path }}

.github/workflows/cla.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ jobs:
2727
issue-is-pr: ${{ github.event_name == 'issue_comment' && github.event.issue.pull_request != null }}
2828
comment-body: ${{ github.event.comment.body || '' }}
2929
default-branch: ${{ github.event.repository.default_branch }}
30+
app-id: ${{ vars.CLA_APP_ID }}
3031
compliance-profile: bsl-change-license-commercial

.github/workflows/pr-compliance-reusable.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,29 @@ on:
2424
required: false
2525
default: ""
2626
type: string
27+
app-id:
28+
required: false
29+
default: ""
30+
type: string
2731

2832
jobs:
2933
validate-pr-metadata:
3034
runs-on: ubuntu-latest
3135
steps:
36+
- name: Create GitHub App token
37+
id: app_token
38+
if: ${{ inputs.app-id != '' && secrets.CLA_APP_PRIVATE_KEY != '' }}
39+
uses: actions/create-github-app-token@v2
40+
with:
41+
app-id: ${{ inputs.app-id }}
42+
private-key: ${{ secrets.CLA_APP_PRIVATE_KEY }}
43+
owner: ${{ github.repository_owner }}
44+
3245
- name: Resolve compliance profile
3346
id: resolve_profile
3447
uses: actions/github-script@v8
3548
with:
36-
github-token: ${{ secrets.CLA_BOT_TOKEN != '' && secrets.CLA_BOT_TOKEN || secrets.GITHUB_TOKEN }}
49+
github-token: ${{ steps.app_token.outputs.token || secrets.CLA_BOT_TOKEN || secrets.GITHUB_TOKEN }}
3750
script: |
3851
const profiles = {
3952
"bsl-change-license-commercial": {
@@ -82,7 +95,7 @@ jobs:
8295
- name: Validate PR declarations
8396
uses: actions/github-script@v8
8497
with:
85-
github-token: ${{ secrets.CLA_BOT_TOKEN != '' && secrets.CLA_BOT_TOKEN || secrets.GITHUB_TOKEN }}
98+
github-token: ${{ steps.app_token.outputs.token || secrets.CLA_BOT_TOKEN || secrets.GITHUB_TOKEN }}
8699
script: |
87100
const prNumber = Number(${{ inputs.pr-number }});
88101
const body = ${{ toJson(inputs.pr-body) }};

.github/workflows/pr-compliance.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ jobs:
2222
pr-body: ${{ github.event.pull_request.body }}
2323
pr-author-login: ${{ github.event.pull_request.user.login }}
2424
default-branch: ${{ github.event.repository.default_branch }}
25+
app-id: ${{ vars.CLA_APP_ID }}
2526
compliance-profile: bsl-change-license-commercial

CONTRIBUTING.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
为了让 CLA 流程正常工作,仓库维护者还需要完成以下配置:
2929

3030
1. 在 GitHub 仓库设置中启用 Actions。
31-
2. 如果组织允许把 Workflow permissions 设为 `Read and write`,可以直接启用;如果组织层强制只读,则需要额外设置仓库 secret `CLA_BOT_TOKEN`,其值应为具有 `repo``workflow` 范围的 token。
31+
2. 推荐配置 GitHub App:
32+
- repository variable `CLA_APP_ID`
33+
- repository secret `CLA_APP_PRIVATE_KEY`
34+
如果暂时还没切 GitHub App,可临时保留仓库 secret `CLA_BOT_TOKEN` 作为兼容兜底。
3235
3. 在默认分支保护规则里把 `CLA``PR Compliance` 两个检查都加入必过状态。
3336
4. 额外创建一个未受保护的 `cla-signatures` 分支,专门存储 `.github/cla/signatures.json`
3437
5. 不要手动创建 `.github/cla/signatures.json`,首次有人签署时工作流会自动创建。
@@ -48,6 +51,7 @@ git switch main
4851
## 说明
4952

5053
- 当前 CLA 流程基于 `contributor-assistant/github-action`
54+
- PR 评论与 `cla-signatures` 写入会优先使用 GitHub App 身份。
5155
- 默认只有 bot 账号在 `allowlist` 中自动豁免,维护者和普通开发者都需要至少完成一次真实签署。
5256
- 机器人评论签署即代表你确认接受 [`docs/legal/ICLA.md`](./docs/legal/ICLA.md) 中的条款。
5357
- 企业贡献需要额外的 [`docs/legal/CCLA.md`](./docs/legal/CCLA.md) 或等效书面授权。

docs/organization-rollout.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ scripts/github/rollout-org-compliance.sh \
4141
--org golutra \
4242
--central-workflow-repo golutra/platform-workflows \
4343
--compliance-profile bsl-change-license-commercial \
44-
--workflow-ref v1.1.1 \
44+
--workflow-ref 0.1.0 \
4545
--execute
4646
```
4747

@@ -78,12 +78,13 @@ caller workflow 会统一变成只传 profile 的形式:
7878
```yaml
7979
jobs:
8080
cla:
81-
uses: golutra/platform-workflows/.github/workflows/cla-reusable.yml@v1.1.1
81+
uses: golutra/platform-workflows/.github/workflows/cla-reusable.yml@0.1.0
8282
with:
8383
event-name: ${{ github.event_name }}
8484
issue-is-pr: ${{ github.event_name == 'issue_comment' && github.event.issue.pull_request != null }}
8585
comment-body: ${{ github.event.comment.body || '' }}
8686
default-branch: ${{ github.event.repository.default_branch }}
87+
app-id: ${{ vars.CLA_APP_ID }}
8788
compliance-profile: bsl-change-license-commercial
8889
```
8990
@@ -127,9 +128,11 @@ jobs:
127128

128129
如果组织层把 `GITHUB_TOKEN` 默认权限限制为只读,还需要在每个业务仓库设置一个具有 `repo` 和 `workflow` 范围的仓库 secret:
129130

130-
- `CLA_BOT_TOKEN`
131+
- repository variable `CLA_APP_ID`
132+
- repository secret `CLA_APP_PRIVATE_KEY`
133+
- 可选:仓库 secret `CLA_BOT_TOKEN` 作为兼容兜底
131134

132-
中央 reusable workflows 会优先使用这个 secret 来:
135+
中央 reusable workflows 会优先使用 GitHub App token;如果没有配置成功,再回退到 `CLA_BOT_TOKEN`,最后才回退到 `GITHUB_TOKEN`。它们会用这个 token 来:
133136

134137
- 在 PR 下评论签署提示与合规检查结果
135138
- 把签署记录写入 `cla-signatures` 分支

docs/repository-compliance-onboarding.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@
1111
- `.github/workflows/cla.yml`
1212
- `.github/workflows/pr-compliance.yml`
1313
2. 为目标仓库创建未受保护的 `cla-signatures` 分支。
14-
3. 如果组织层限制 `GITHUB_TOKEN` 为只读,则在目标仓库创建 `CLA_BOT_TOKEN` secret。
15-
4. 在默认分支保护规则中把 `CLA``PR Compliance` 设为 required checks。
16-
5. 发起一个真实测试 PR,验证:
14+
3. 为目标仓库配置 GitHub App 凭据:
15+
- repository variable `CLA_APP_ID`
16+
- repository secret `CLA_APP_PRIVATE_KEY`
17+
4. 如需兼容旧流程,可临时保留仓库 secret `CLA_BOT_TOKEN` 作为回退。
18+
5. 在默认分支保护规则中把 `CLA``PR Compliance` 设为 required checks。
19+
6. 发起一个真实测试 PR,验证:
1720
- `PR Compliance` 自动触发
1821
- `CLA` 自动触发
1922
- 评论签署文本后,签名记录被写入 `cla-signatures`
23+
- 评论作者与账本写入使用 GitHub App 身份
2024
- 两个检查都能通过
2125

2226
## 常见失败点
2327

2428
- 没有 `cla-signatures` 分支,导致签署记录无法写入。
25-
- 组织层把 workflow token 限制为只读,但仓库没有设置 `CLA_BOT_TOKEN`
29+
- workflow 调用里没有传 `app-id`,或仓库没有配置 `CLA_APP_ID` / `CLA_APP_PRIVATE_KEY`
30+
- 组织层把 workflow token 限制为只读,同时仓库里既没有 GitHub App 私钥,也没有 `CLA_BOT_TOKEN` 兜底。
2631
- 默认分支没有 required checks,导致工作流虽然跑了,但不能真正阻塞合并。
2732
- PR 没有按模板填写,导致 `PR Compliance` 一直失败。
2833

scripts/github/rollout-org-compliance.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ jobs:
334334
issue-is-pr: \${{ github.event_name == 'issue_comment' && github.event.issue.pull_request != null }}
335335
comment-body: \${{ github.event.comment.body || '' }}
336336
default-branch: \${{ github.event.repository.default_branch }}
337+
app-id: \${{ vars.CLA_APP_ID }}
337338
compliance-profile: ${compliance_profile}
338339
EOF
339340
}
@@ -369,6 +370,7 @@ jobs:
369370
pr-body: \${{ github.event.pull_request.body }}
370371
pr-author-login: \${{ github.event.pull_request.user.login }}
371372
default-branch: \${{ github.event.repository.default_branch }}
373+
app-id: \${{ vars.CLA_APP_ID }}
372374
compliance-profile: ${compliance_profile}
373375
EOF
374376
}

0 commit comments

Comments
 (0)