|
| 1 | +--- |
| 2 | +title: Automating tasks with Copilot CLI and GitHub Actions |
| 3 | +shortTitle: Automate with Actions |
| 4 | +intro: Integrate {% data variables.copilot.copilot_cli %} into your {% data variables.product.prodname_actions %} workflows. |
| 5 | +product: '{% data reusables.gated-features.copilot-cli %}' |
| 6 | +versions: |
| 7 | + feature: copilot |
| 8 | +topics: |
| 9 | + - Copilot |
| 10 | + - CLI |
| 11 | +contentType: how-tos |
| 12 | +category: |
| 13 | + - Build with Copilot CLI |
| 14 | + - Author and optimize with Copilot |
| 15 | +--- |
| 16 | + |
| 17 | +You can run {% data variables.copilot.copilot_cli %} in a {% data variables.product.prodname_actions %} workflow to automate AI-powered tasks as part of your CI/CD process. For example, you can summarize recent repository activity, generate reports, or scaffold project content. {% data variables.copilot.copilot_cli %} runs on the Actions runner like any other CLI tool, so you can install it during a job and invoke it from workflow steps. |
| 18 | + |
| 19 | +## Understanding the workflow |
| 20 | + |
| 21 | +You can define a job in a {% data variables.product.prodname_actions %} workflow that: installs {% data variables.copilot.copilot_cli_short %} on the runner, authenticates it, runs it in programmatic mode, and then handles the results. Programmatic mode is designed for scripts and automation and lets you pass a prompt non-interactively. |
| 22 | + |
| 23 | +Workflows can follow this pattern: |
| 24 | +1. **Trigger**: Start the workflow on a schedule, in response to repository events, or manually. |
| 25 | +1. **Setup**: Checkout code, set up environment. |
| 26 | +1. **Install**: Install {% data variables.copilot.copilot_cli %} on the runner. |
| 27 | +1. **Authenticate**: Ensure the CLI has the necessary permissions to access the repository and make changes. |
| 28 | +1. **Run {% data variables.copilot.copilot_cli_short %}**: Invoke {% data variables.copilot.copilot_cli_short %} with a prompt describing the task you want to automate. |
| 29 | + |
| 30 | +The following workflow generates a daily summary of repository changes and prints the summary to the workflow logs. |
| 31 | + |
| 32 | +```yaml copy |
| 33 | +name: Daily summary |
| 34 | +on: |
| 35 | + workflow_dispatch: |
| 36 | + # Daily at 8:25 UTC |
| 37 | + schedule: |
| 38 | + - cron: '25 8 * * *' |
| 39 | +permissions: |
| 40 | + contents: read |
| 41 | +jobs: |
| 42 | + daily-summary: |
| 43 | + runs-on: ubuntu-latest |
| 44 | + steps: |
| 45 | + - name: Checkout |
| 46 | + uses: {% data reusables.actions.action-checkout %} |
| 47 | + |
| 48 | + - name: Set up Node.js environment |
| 49 | + uses: {% data reusables.actions.action-setup-node %} |
| 50 | + |
| 51 | + - name: Install Copilot CLI |
| 52 | + run: npm install -g @github/copilot |
| 53 | + |
| 54 | + - name: Run Copilot CLI |
| 55 | + env: |
| 56 | + {% raw %}COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}{% endraw %} |
| 57 | + run: | |
| 58 | + TODAY=$(date +%Y-%m-%d) |
| 59 | + copilot -p "Review the git log for this repository and write a bullet point summary of all code changes that were made today ($TODAY). Include a brief description of what was changed." |
| 60 | +``` |
| 61 | +
|
| 62 | +## Trigger |
| 63 | +
|
| 64 | +In this example, the workflow runs on a daily schedule and can also be triggered manually. |
| 65 | +
|
| 66 | +`The workflow_dispatch` trigger lets you run the workflow manually from the Actions tab, which is useful when testing changes to your prompt or workflow configuration. |
| 67 | + |
| 68 | +The `schedule` trigger runs the workflow automatically at a specified time using cron syntax. |
| 69 | + |
| 70 | +```yaml copy |
| 71 | +on: |
| 72 | + # Allows manual triggering of this workflow. |
| 73 | + workflow_dispatch: |
| 74 | + # Daily at 8:30 UTC |
| 75 | + schedule: |
| 76 | + - cron: '30 8 * * *' |
| 77 | +``` |
| 78 | + |
| 79 | +## Setup |
| 80 | + |
| 81 | +Set up the job so {% data variables.copilot.copilot_cli_short %} can access your repository and run on the runner. This allows {% data variables.copilot.copilot_cli_short %} to analyze the repository context, when generating the daily summary. |
| 82 | + |
| 83 | +The `permissions` block defines the scope granted to the built-in `GITHUB_TOKEN`. Because this workflow reads repository data and prints a summary to the logs, it requires `contents: read`. |
| 84 | + |
| 85 | +```yaml copy |
| 86 | +permissions: |
| 87 | + contents: read |
| 88 | +jobs: |
| 89 | + daily-summary: |
| 90 | + runs-on: ubuntu-latest |
| 91 | + steps: |
| 92 | + - name: Checkout |
| 93 | + uses: {% data reusables.actions.action-checkout %} |
| 94 | +``` |
| 95 | + |
| 96 | +## Install |
| 97 | + |
| 98 | +Install {% data variables.copilot.copilot_cli_short %} on the runner so your workflow can invoke it as a command. You can install {% data variables.copilot.copilot_cli %} using any supported installation method. For a full list of installation options, see [AUTOTITLE](/copilot/how-tos/copilot-cli/set-up-copilot-cli/install-copilot-cli). |
| 99 | + |
| 100 | +In this example, the workflow installs {% data variables.copilot.copilot_cli %} globally with npm. |
| 101 | + |
| 102 | +```yaml copy |
| 103 | +- name: Set up Node.js environment |
| 104 | + uses: {% data reusables.actions.action-setup-node %} |
| 105 | +
|
| 106 | +- name: Install Copilot CLI |
| 107 | + run: npm install -g @github/copilot |
| 108 | +``` |
| 109 | + |
| 110 | +## Authenticate |
| 111 | + |
| 112 | +To authenticate {% data variables.copilot.copilot_cli_short %} in a workflow, create a {% data variables.product.pat_v2 %} (PAT) with the **Copilot Requests** permission. Store the PAT as a repository secret, then pass it to the CLI using an environment variable. For more information on creating a PAT for the CLI, see [Authenticating with a {% data variables.product.pat_generic %}](/copilot/how-tos/copilot-cli/set-up-copilot-cli/install-copilot-cli#authenticating-with-a-personal-access-token). |
| 113 | + |
| 114 | +{% data variables.copilot.copilot_cli_short %} supports multiple authentication environment variables. In this example, the workflow uses `COPILOT_GITHUB_TOKEN`, which is specific to {% data variables.copilot.copilot_cli_short %} and avoids confusion for the built-in `GITHUB_TOKEN` environment variable. |
| 115 | + |
| 116 | +```yaml copy |
| 117 | +- name: Run Copilot CLI |
| 118 | + env: |
| 119 | + {% raw %}COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}{% endraw %} |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +## Run {% data variables.copilot.copilot_cli_short %} |
| 124 | + |
| 125 | +Run {% data variables.copilot.copilot_cli_short %} in programmatic mode when you want to use it in automation. |
| 126 | +`copilot -p PROMPT` executes a prompt programmatically and exits when the command completes. |
| 127 | + |
| 128 | +In this workflow, {% data variables.copilot.copilot_cli_short %} references the repository content that is available in the job workspace. The command prints its response to standard output and the summary appears in the workflow logs. |
| 129 | + |
| 130 | +```yaml copy |
| 131 | +- name: Run Copilot CLI |
| 132 | + env: |
| 133 | + {% raw %}COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}{% endraw %} |
| 134 | + run: | |
| 135 | + TODAY=$(date +%Y-%m-%d) |
| 136 | + copilot -p "Review the git log for this repository and write a bullet point summary of all code changes that were made today ($TODAY). Include a brief description of what was changed." |
| 137 | +``` |
| 138 | + |
| 139 | +## Next steps |
| 140 | + |
| 141 | +After you confirm the workflow prints a summary to the logs, you can adapt the same pattern to other automation tasks. Start by changing the prompt you pass to `copilot -p PROMPT`, then decide what you want to do with the output. |
| 142 | + |
| 143 | +* Write the summary to a file so later steps can use it as input. |
| 144 | +* Post the summary as a comment on an issue or a message in a team chat. |
| 145 | +* Summarize requests and output a draft changelog. |
| 146 | + |
| 147 | +## Further reading |
| 148 | + |
| 149 | +* [AUTOTITLE](/copilot/reference/cli-command-reference) |
| 150 | +* [AUTOTITLE](/copilot/reference/cli-plugin-reference) |
| 151 | + |
0 commit comments