Skip to content

Commit 27e71f3

Browse files
DeanChensjcopybara-github
authored andcommitted
chore: Update Gemini Actions workflows for enhanced security and community alignment
- Restrict invoke and review triggers purely to explicit user comments. - Enforce strict author association verification (OWNER, MEMBER, COLLABORATOR). - Enforce strict targeting assertion to ensure pull requests act on the main branch. - Synchronize prompt constraints and GitHub action tools with the community catalog. - Refine action API key options to uniformly target secrets.GOOGLE_API_KEY. Co-authored-by: Shangjie Chen <deanchen@google.com> PiperOrigin-RevId: 915654346
1 parent 88ebd42 commit 27e71f3

3 files changed

Lines changed: 44 additions & 61 deletions

File tree

.github/workflows/gemini-dispatch.yml

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ on:
77
pull_request_review:
88
types:
99
- 'submitted'
10-
pull_request:
11-
types:
12-
- 'opened'
13-
- 'ready_for_review'
14-
issues:
15-
types:
16-
- 'opened'
17-
- 'reopened'
1810
issue_comment:
1911
types:
2012
- 'created'
@@ -44,19 +36,11 @@ jobs:
4436
env | grep '^DEBUG_'
4537
4638
dispatch:
47-
# For PRs: only if not from a fork
48-
# For issues: only on open/reopen
49-
# For comments: only if user types @gemini-cli and is OWNER/MEMBER/COLLABORATOR
39+
# Only trigger if user types @gemini-cli and author association is OWNER, MEMBER, or COLLABORATOR
5040
if: |-
51-
(
52-
github.event_name == 'pull_request' &&
53-
github.event.pull_request.head.repo.fork == false &&
54-
github.event.pull_request.draft == false
55-
) || (
56-
github.event.sender.type == 'User' &&
57-
startsWith(github.event.comment.body || github.event.review.body || github.event.issue.body, '@gemini-cli') &&
58-
contains(fromJSON('["OWNER", "MEMBER", "COLLABORATOR"]'), github.event.comment.author_association || github.event.review.author_association || github.event.issue.author_association)
59-
)
41+
github.event.sender.type == 'User' &&
42+
startsWith(github.event.comment.body || github.event.review.body, '@gemini-cli') &&
43+
contains(fromJSON('["OWNER", "MEMBER", "COLLABORATOR"]'), github.event.comment.author_association || github.event.review.author_association)
6044
runs-on: 'ubuntu-latest'
6145
permissions:
6246
contents: 'read'
@@ -82,22 +66,43 @@ jobs:
8266

8367
- name: 'Extract command'
8468
id: 'extract_command'
85-
uses: 'actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea' # ratchet:actions/github-script@v7
69+
uses: 'actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd' # ratchet:actions/github-script@v8.0.0
8670
env:
87-
EVENT_TYPE: '${{ github.event_name }}.${{ github.event.action }}'
88-
REQUEST: '${{ github.event.comment.body || github.event.review.body || github.event.issue.body }}'
71+
REQUEST: '${{ github.event.comment.body || github.event.review.body }}'
72+
IS_PR: '${{ !!(github.event.pull_request || github.event.issue.pull_request) }}'
8973
with:
9074
script: |
91-
const eventType = process.env.EVENT_TYPE;
9275
const request = process.env.REQUEST;
76+
const isPr = process.env.IS_PR === 'true';
9377
core.setOutput('request', request);
9478
95-
if (eventType === 'pull_request.opened' || eventType === 'pull_request.ready_for_review') {
96-
core.setOutput('command', 'review');
97-
} else if (request.startsWith("@gemini-cli /review")) {
98-
core.setOutput('command', 'review');
99-
const additionalContext = request.replace(/^@gemini-cli \/review/, '').trim();
100-
core.setOutput('additional_context', additionalContext);
79+
// Ensure request is on a PR targeting the main branch
80+
let baseRef = '';
81+
if (context.eventName === 'pull_request_review' || context.eventName === 'pull_request_review_comment') {
82+
baseRef = context.payload.pull_request.base.ref;
83+
} else if (context.eventName === 'issue_comment' && context.payload.issue.pull_request) {
84+
const pr = await github.rest.pulls.get({
85+
owner: context.repo.owner,
86+
repo: context.repo.repo,
87+
pull_number: context.payload.issue.number
88+
});
89+
baseRef = pr.data.base.ref;
90+
}
91+
92+
if (isPr && baseRef !== 'main') {
93+
console.log(`Skipping: PR targets '${baseRef}', but only 'main' is allowed.`);
94+
core.setOutput('command', 'fallthrough');
95+
return;
96+
}
97+
98+
if (request.startsWith("@gemini-cli /review")) {
99+
if (isPr) {
100+
core.setOutput('command', 'review');
101+
const additionalContext = request.replace(/^@gemini-cli \/review/, '').trim();
102+
core.setOutput('additional_context', additionalContext);
103+
} else {
104+
core.setOutput('command', 'fallthrough');
105+
}
101106
} else if (request.startsWith("@gemini-cli")) {
102107
const additionalContext = request.replace(/^@gemini-cli/, '').trim();
103108
core.setOutput('command', 'invoke');

.github/workflows/gemini-invoke.yml

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ jobs:
5252
ISSUE_NUMBER: '${{ github.event.pull_request.number || github.event.issue.number }}'
5353
REPOSITORY: '${{ github.repository }}'
5454
ADDITIONAL_CONTEXT: '${{ inputs.additional_context }}'
55+
# Required to allow the Gemini CLI to process files in the ephemeral GitHub Actions runner
56+
GEMINI_CLI_TRUST_WORKSPACE: 'true'
5557
with:
5658
gcp_location: '${{ vars.GOOGLE_CLOUD_LOCATION }}'
5759
gcp_project_id: '${{ vars.GOOGLE_CLOUD_PROJECT }}'
5860
gcp_service_account: '${{ vars.SERVICE_ACCOUNT_EMAIL }}'
5961
gcp_workload_identity_provider: '${{ vars.GCP_WIF_PROVIDER }}'
60-
gemini_api_key: '${{ secrets.GEMINI_API_KEY }}'
62+
gemini_api_key: '${{ secrets.GOOGLE_API_KEY }}'
6163
gemini_cli_version: '${{ vars.GEMINI_CLI_VERSION }}'
6264
gemini_debug: '${{ fromJSON(vars.GEMINI_DEBUG || vars.ACTIONS_STEP_DEBUG || false) }}'
6365
gemini_model: '${{ vars.GEMINI_MODEL }}'
@@ -91,32 +93,12 @@ jobs:
9193
"GITHUB_PERSONAL_ACCESS_TOKEN",
9294
"ghcr.io/github/github-mcp-server:v0.27.0"
9395
],
94-
"includeTools": [
95-
"add_issue_comment",
96-
"issue_read",
97-
"list_issues",
98-
"search_issues",
99-
"pull_request_read",
100-
"list_pull_requests",
101-
"search_pull_requests",
102-
"get_commit",
103-
"get_file_contents",
104-
"list_commits",
105-
"search_code"
106-
],
10796
"env": {
10897
"GITHUB_PERSONAL_ACCESS_TOKEN": "${{ steps.mint_identity_token.outputs.token || secrets.GITHUB_TOKEN || github.token }}"
10998
}
11099
}
111-
},
112-
"tools": {
113-
"core": [
114-
"run_shell_command(cat)",
115-
"run_shell_command(echo)",
116-
"run_shell_command(grep)",
117-
"run_shell_command(head)",
118-
"run_shell_command(tail)"
119-
]
120100
}
121101
}
122-
prompt: '/gemini-invoke'
102+
prompt: |-
103+
/gemini-invoke
104+
[IMPORTANT] Do not generate execution plans and do not ask for approval (such as suggesting `@gemini-cli /approve`). Perform the requested task or answer the question directly and immediately.

.github/workflows/gemini-review.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ jobs:
5151
PULL_REQUEST_NUMBER: '${{ github.event.pull_request.number || github.event.issue.number }}'
5252
REPOSITORY: '${{ github.repository }}'
5353
ADDITIONAL_CONTEXT: '${{ inputs.additional_context }}'
54-
GEMINI_API_KEY: '${{ secrets.GEMINI_API_KEY }}'
54+
GEMINI_API_KEY: '${{ secrets.GOOGLE_API_KEY }}'
55+
# Required to allow the Gemini CLI to process files in the ephemeral GitHub Actions runner
5556
GEMINI_CLI_TRUST_WORKSPACE: 'true'
5657
with:
5758
gcp_location: '${{ vars.GOOGLE_CLOUD_LOCATION }}'
5859
gcp_project_id: '${{ vars.GOOGLE_CLOUD_PROJECT }}'
5960
gcp_service_account: '${{ vars.SERVICE_ACCOUNT_EMAIL }}'
6061
gcp_workload_identity_provider: '${{ vars.GCP_WIF_PROVIDER }}'
61-
gemini_api_key: '${{ secrets.GEMINI_API_KEY }}'
62+
gemini_api_key: '${{ secrets.GOOGLE_API_KEY }}'
6263
gemini_cli_version: '${{ vars.GEMINI_CLI_VERSION }}'
6364
gemini_debug: '${{ fromJSON(vars.GEMINI_DEBUG || vars.ACTIONS_STEP_DEBUG || false) }}'
6465
gemini_model: '${{ vars.GEMINI_MODEL }}'
@@ -90,11 +91,6 @@ jobs:
9091
"GITHUB_PERSONAL_ACCESS_TOKEN",
9192
"ghcr.io/github/github-mcp-server:v0.27.0"
9293
],
93-
"includeTools": [
94-
"pull_request_read",
95-
"add_comment_to_pending_review",
96-
"pull_request_review_write"
97-
],
9894
"env": {
9995
"GITHUB_PERSONAL_ACCESS_TOKEN": "${{ steps.mint_identity_token.outputs.token || secrets.GITHUB_TOKEN || github.token }}"
10096
}

0 commit comments

Comments
 (0)