run-gemini-cli GitHub Action

repository·main·Indexed 24 days ago

https://github.com/google-github-actions/run-gemini-cli

A GitHub Action that integrates Gemini AI capabilities into GitHub workflows for automated PR reviews, issue triaging, and conversational coding assistance. It supports various authentication methods including Gemini API keys and Workload Identity Federation, and provides pre-built workflows for issue triage and PR reviews. The action can be customized via a GEMINI.md file for project-specific context and configured through specific action inputs and repository secrets.

Tokens
6.8K
Snippets
12
Records
37
Agent score
82%

What's inside run-gemini-cli

  1. Overview of run-gemini-cli

    main

    run-gemini-cli is a GitHub Action that integrates Gemini into your development workflow via the Gemini CLI. It functions as both an autonomous agent for routine coding tasks and an on-demand collaborator.

    You can use it to perform pull request reviews, triage issues, and perform code analysis or modifications by interacting with it conversationally (e.g., using @gemini-cli in comments) directly within your GitHub repositories.

  2. Authentication methods for run-gemini-cli

    main

    The action requires authentication for both Google services and the GitHub API.

    Google Authentication

    Choose one of the following:

    1. Gemini API Key: Simplest method for projects without Google Cloud integration.
    2. Workload Identity Federation: Most secure method for authenticating to Google Cloud services.

    GitHub Authentication

    Choose one of the following:

    1. Default GITHUB_TOKEN: Uses the standard token provided by the workflow.
    2. Custom GitHub App (Recommended): Most secure and flexible method for authentication.
  3. Structure of the evaluation framework

    main

    The evaluation directory is organized as follows:

    • test-rig.ts: A utility for setting up temporary environments for the CLI.
    • *.eval.ts: Individual benchmark files for specific workflows (e.g., issue-triage.eval.ts, pr-review.eval.ts, issue-fixer.eval.ts, gemini-assistant.eval.ts, gemini-scheduled-triage.eval.ts).
    • data/*.jsonl: Gold-standard datasets used for each workflow benchmark.
    • vitest.config.ts: Configuration for the Vitest runner.
  4. Key features of run-gemini-cli

    main

    The action provides several core capabilities:

    • Automation: Trigger workflows based on GitHub events (like opening an issue) or on a schedule (like nightly runs).
    • On-demand Collaboration: Trigger specific workflows by mentioning the Gemini CLI in issue or pull request comments (e.g., @gemini-cli /review).
    • Extensible with Tools: Uses Gemini's tool-calling capabilities to interact with other command-line tools, such as the GitHub CLI (gh).
    • Customizable: You can provide project-specific context and instructions by adding a GEMINI.md file to your repository.
  5. Configure secure authentication with Workload Identity Federation

    main
    For the most secure authentication to Google Cloud, use Workload Identity Federation. This keyless authentication method is recommended because it eliminates the need to manage long-lived service account keys. Refer to the project's authentication.md for detailed setup instructions.
  6. Best practices for securing and configuring workflows

    main

    To maintain security and reliability when using run-gemini-cli in automated workflows, follow these recommendations:

    Repository Security

    • Implement branch and tag protection.
    • Restrict pull request approvers.

    Workflow Configuration

    • Use Workload Identity Federation for secure authentication to Google Cloud.
    • Manage secrets effectively (e.g., using GitHub Secrets).
    • Pin action versions to specific tags or SHAs to prevent unexpected changes from upstream updates.

    Monitoring and Auditing

    • Regularly review action logs.
    • Enable OpenTelemetry for deeper insights into performance and behavior.
  7. Authenticate with GitHub using the default GITHUB_TOKEN

    main

    For simple use cases, the action can use the default GITHUB_TOKEN automatically provided by GitHub for each workflow run. The action falls back to this method if APP_ID and APP_PRIVATE_KEY secrets are not configured.

    Limitations:

    • Limited Permissions: The token has restricted permissions. You may need to explicitly grant permissions in your workflow file (e.g., contents: 'read', issues: 'write', pull-requests: 'write').
    • Job-Scoped: Access is limited to the current repository and expires when the job finishes.
    permissions:
      contents: 'read'
      issues: 'write'
      pull-requests: 'write'
  8. Enable OpenTelemetry observability for Gemini CLI

    main

    You can configure the Gemini CLI Action to send telemetry data (metrics, logs, and traces) to a Google Cloud project. This allows you to monitor performance and debug workflows using Google Cloud's native observability tools.

    To enable this, you must provide a settings JSON object in your GitHub Actions workflow with the telemetry configuration set to enabled: true and target: "gcp".

    - uses: 'google-github-actions/run-gemini-cli@v0'
      with:
        gcp_workload_identity_provider: '${{ vars.GCP_WIF_PROVIDER }}'
        gcp_project_id: '${{ vars.GOOGLE_CLOUD_PROJECT }}'
        gemini_api_key: '${{ secrets.GEMINI_API_KEY }}'
        # Enable telemetry in settings
        settings: |- 
          {
            "telemetry": {
              "enabled": true,
              "target": "gcp"
            }
          }
  9. Authenticate with Gemini Code Assist via Workload Identity Federation

    main

    If you have a Gemini Code Assist subscription, use this configuration to authenticate via Workload Identity Federation.

    1. Run the Setup Script

    Run ./scripts/setup_workload_identity.sh as described in the Google Cloud authentication guide.

    2. Configure GitHub Variables

    Add these variables to your repository:

    Variable NameDescription
    GCP_WIF_PROVIDERThe resource name of the Workload Identity Provider
    GOOGLE_CLOUD_PROJECTYour Google Cloud project ID
    GOOGLE_CLOUD_LOCATIONYour Google Cloud project Location
    SERVICE_ACCOUNT_EMAILThe email of the service account for Code Assist
    GOOGLE_GENAI_USE_GCASet to true to authenticate using Gemini Code Assist

    3. Workflow Example

    - uses: 'google-github-actions/run-gemini-cli@v0'
      with:
        gcp_workload_identity_provider: '${{ vars.GCP_WIF_PROVIDER }}'
        gcp_service_account: '${{ vars.SERVICE_ACCOUNT_EMAIL }}'
        gcp_project_id: '${{ vars.GOOGLE_CLOUD_PROJECT }}'
        gcp_location: '${{ vars.GOOGLE_CLOUD_LOCATION }}'
        use_gemini_code_assist: '${{ vars.GOOGLE_GENAI_USE_GCA }}'
        prompt: |-
          Explain this code
  10. Authenticate with GitHub using a GitHub App (Recommended)

    main

    For better security and fine-grained control, use a custom GitHub App. This allows you to follow the principle of least privilege by granting only the specific permissions the action needs.

    Step 1: Create a New GitHub App

    1. Go to GitHub Settings > Developer settings > GitHub Apps > New GitHub App.
    2. Name: Provide a unique name.
    3. Homepage URL: Provide your org website or repo URL.
    4. Webhooks: Uncheck Active (not required).
    5. Repository Permissions: Grant the following (or fewer if your workflow allows):
      • Contents: Read & write
      • Issues: Read & write
      • Pull requests: Read & write
    6. Click Create GitHub App.

    Step 2: Generate Credentials

    1. On the app settings page, click Generate a private key.
    2. Save the .pem file securely.
    3. Note the App ID at the top of the settings page.

    Step 3: Install the App

    1. Select Install App from the left sidebar in the app settings.
    2. Choose your organization/account.
    3. Select Only select repositories and pick the target repository.
    4. Click Install.

    Step 4: Configure Repository Variables and Secrets

    In your repository's Settings > Secrets and variables > Actions:

    1. Variables Tab: Create a new repository variable named APP_ID with your App ID.
    2. Secrets Tab: Create a new repository secret named APP_PRIVATE_KEY containing the full contents of the .pem file.