github/command Action

repository·main·Indexed 41 days ago

https://github.com/github/command

An IssueOps tool for GitHub Actions that allows developers to trigger workflows by commenting on Issues or Pull Requests. It handles command detection, user authorization via allowlists, and UI feedback through emoji reactions. The Action provides a `continue` output to gate subsequent workflow steps based on permissions, CI status, and PR review requirements.

Tokens
3.4K
Snippets
7
Records
15
Agent score
49%

What's inside github/command

  1. How github/command works

    main

    The github/command Action automates the lifecycle of an IssueOps command. When a workflow step using this Action runs, it performs the following:

    1. Detection: Scans the comment body for the specified command phrase (e.g., .ping).
    2. Reaction: Automatically reacts to the comment to provide immediate feedback that the command is being processed.
    3. Authorization: Verifies if the user who commented has the required permissions and is in the allowlist if configured.
    4. Validation: Checks CI status and PR reviews (if running in a pull_request context).
    5. Output: Sets the continue output variable. If the command is valid and authorized, steps.<id>.outputs.continue is set to 'true'.
  2. Use the `continue` output to control workflow execution

    main

    When using github/command, the Action provides two primary outputs for flow control:

    1. triggered: A string ("true" or "false") indicating if the specific command string was found in the comment.
    2. continue: A string ("true" or empty) that acts as the final gate.

    Crucial: You should use the continue output to determine if your subsequent workflow steps should run. Even if triggered is true, the workflow might not proceed if permissions, allowlists, or other constraints (like skip_ci or skip_reviews) are not met. Always use a conditional check on ${{ steps.command.outputs.continue == 'true' }} before executing custom logic.

  3. Pass parameters to commands using a separator

    main

    The command Action allows you to pass unstructured parameters to your command by using a separator. By default, the separator is |. Any text following this separator in the command string is captured and saved as a GitHub Actions output named params.

    You can use this to pass configuration values (like LOG_LEVEL=debug) or lists of identifiers (like server1 server2) to subsequent workflow steps. It is the responsibility of the user to parse the params string in later steps.

    .restart | LOG_LEVEL=debug CPU_CORES=4
  4. Quickstart with github/command

    main

    To get started with github/command, use it within a GitHub Actions workflow triggered by issue_comment events. This allows you to execute custom logic by commenting on Issues or Pull Requests.

    Basic Usage

    Use the default configuration to detect a specific command phrase:

    - name: command
      id: command
      uses: github/command@vX.X.X
      with:
        command: .lint

    Advanced Usage

    Customize the command syntax, reaction type, allowed contexts (e.g., pull_request, issue), required permissions, and user allowlists:

    - name: command
      id: command
      uses: github/command@vX.X.X
      with:
        command: .restart
        reaction: "eyes"
        allowed_contexts: "pull_request,issue"
        permissions: "write,admin"
        allowlist: monalisa
  5. Access command parameters via the `params` output

    main

    The text captured after the param_separator is made available via the params output. You can access this in subsequent steps using the standard GitHub Actions syntax: ${{ steps.<step_id>.outputs.params }}.

    - name: command
      id: command
      uses: github/command@vX.X.X
      with:
        command: .restart
        param_separator: "|"
    
    - name: example
      if: steps.command.outputs.continue == 'true'
      run: |
        echo "params: ${{ steps.command.outputs.params }}"
  6. Configure IssueOps command triggers and permissions

    main

    The github/command Action allows you to implement IssueOps by triggering workflows based on specific comment strings (e.g., .lint). You can control which users can trigger commands using allowlist and define the required GitHub permissions via permissions.

    To allow commands to be invoked from both Pull Requests and Issues, set allowed_contexts to "pull_request,issue". By default, it only allows pull_request.

    Security Note: If you use GitHub org teams in your allowlist, you must provide a GitHub Personal Access Token (PAT) with read:org scopes via the allowlist_pat input so the Action can verify team membership.

    - uses: github/command@vX.X.X
      id: command
      with:
        command: ".lint"
        allowlist: monalisa,octocat,octo-awesome-org/octo-awesome-team
        allowlist_pat: ${{ secrets.ALLOWLIST_PAT }}
  7. Complete IssueOps Workflow Example

    main

    This example demonstrates a full workflow that triggers on a new issue comment, checks for the .ping command, and conditionally runs a subsequent step only if the command was valid and authorized.

    Key Requirements:

    1. Trigger: Must use on: issue_comment with types: [created].
    2. Permissions: Requires pull-requests: write and issues: write to add reactions to comments.
    3. Gating: Use the output ${{ steps.command.outputs.continue == 'true' }} to control subsequent steps.
    name: "command demo"
    
    on:
      issue_comment:
        types: [created]
    
    permissions:
      pull-requests: write
      issues: write
      checks: read
      contents: read
    
    jobs:
      demo:
        runs-on: ubuntu-latest
        steps:
          - uses: github/command@vX.X.X
            id: command
            with:
              command: ".ping"
              allowed_contexts: issue,pull_request
    
          - name: ping
            if: ${{ steps.command.outputs.continue == 'true' }}
            run: echo "I am going to ping some cool website now!"
  8. Configure the github/command Action inputs

    main

    The github/command Action is used to implement IssueOps by reacting to specific commands within issue or pull request comments. To use it, you must provide the following inputs in your workflow file:

    Required Inputs

    • command: The specific command string to look for in the comment body.
    • github_token: A GitHub token with permissions to read comments and add reactions.

    Optional Inputs

    • param_separator: A string used to separate parameters within the command.
    • reaction: The emoji reaction to add to the triggering comment.
    • allow_forks: Boolean. Whether to allow commands from fork repositories.
    • skip_ci: Boolean. Whether to skip CI checks.
    • allow_drafts: Boolean. Whether to allow commands on draft pull requests.
    • skip_reviews: Boolean. Whether to skip review checks.
    • fork_review_bypass: Boolean. Whether to bypass review requirements for forks.
  9. Reference the `github/command` outputs

    main

    The following outputs are produced by the Action:

    OutputDescription
    triggeredString "true" if the trigger was found, otherwise "false".
    continue⭐ String "true" if the workflow should proceed, otherwise empty.
    comment_bodyThe body of the comment.
    actorThe GitHub handle of the user who invoked the command.
    paramsRaw parameters passed in the command (parsed via param_separator).
    comment_idThe ID of the triggering comment.
    issue_numberThe issue/PR number.
    initial_reaction_idReaction ID for the initial reaction on the trigger.
    forkString "true" if the PR is a fork, otherwise "false".
    fork_refThe true ref of the fork.
    fork_labelThe API label field for the fork.
    fork_checkoutConsole command to checkout the fork locally.
    fork_full_nameFull name of the fork (org/repo).
    shaThe commit SHA (if in a PR context).
    refThe ref (if in a PR context).
    base_refThe base ref the PR is merging into.
  10. Reference the `github/command` input options

    main

    The following inputs are available for configuring the github/command Action:

    InputRequired?DefaultDescription
    commandtrue-The string to look for in comments (e.g., ".lint").
    github_tokentrue${{ github.token }}Token used to create an authenticated client.
    statustrue${{ job.status }}The status of the GitHub Action.
    reactiontrueeyesEmoji reaction to add when a trigger is detected.
    success_reactiontrue+1Reaction to add if execution was successful.
    failure_reactiontrue-1Reaction to add if execution failed.
    allowed_contextstruepull_requestComma separated list of contexts (pull_request, issue).
    permissionstrue"write,admin"Required GitHub permissions for the actor.
    allow_draftstrue"false"Allow command on draft pull requests.
    allow_forkstrue"false"Allow command on forked pull requests.
    skip_citrue"false"Require passing CI checks before running.
    skip_reviewstrue"false"Require reviews before running.
    param_separatortrue"|"Separator for parsing parameters in comments.
    allowlistfalse"false"Comma separated list of usernames or teams.
    allowlist_patfalse"false"PAT with read:org scope (required for org teams).
    skip_completingtrue"false"Skip the process of completing the Action.
    fork_review_bypasstrue"false"Allow forks to bypass review requirements.
  11. Configure github/command inputs

    main

    The following inputs are available for the github/command Action:

    InputDescription
    commandThe trigger phrase to look for in the comment (e.g., .lint).
    reactionThe emoji reaction to add to the comment (e.g., eyes).
    allowed_contextsComma-separated list of contexts where the command is valid (e.g., pull_request,issue).
    permissionsThe GitHub permissions required to run the command (e.g., write,admin).
    allowlistA list of users authorized to run the command.