Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 17719a3

Browse files
hubwriterCopilotsarahsSiaraMist
authored
Copilot CLI: Add new map topic & articles about automation (#60002)
Co-authored-by: Copilot Autofix powered by AI <[email protected]> Co-authored-by: Sarah Schneider <[email protected]> Co-authored-by: Siara <[email protected]>
1 parent a6ad80c commit 17719a3

24 files changed

Lines changed: 677 additions & 193 deletions

content/copilot/concepts/agents/copilot-cli/about-copilot-cli.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ The `--deny-tool` and `--allow-tool` options require one of the following argume
240240

241241
* `'shell(COMMAND)'`
242242

243-
For example, `copilot --deny-tool 'shell(rm)'` prevents {% data variables.product.prodname_copilot_short %} from using any `rm` command.
243+
For example, `copilot --deny-tool='shell(rm)'` prevents {% data variables.product.prodname_copilot_short %} from using any `rm` command.
244244

245245
For `git` and `gh` commands, you can specify a particular first-level subcommand to allow or deny. For example:
246246

@@ -311,7 +311,7 @@ Each time you submit a prompt to {% data variables.product.prodname_copilot_shor
311311

312312
ACP (the Agent Client Protocol) is an open standard for interacting with AI agents. It allows you to use {% data variables.copilot.copilot_cli_short %} as an agent in any third-party tools, IDEs, or automation systems that support this protocol.
313313

314-
For more information, see [AUTOTITLE](/copilot/reference/acp-server).
314+
For more information, see [AUTOTITLE](/copilot/reference/copilot-cli-reference/acp-server).
315315

316316
{% data reusables.cli.feedback %}
317317

content/copilot/concepts/agents/copilot-cli/autopilot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ You will get the best results from autopilot mode if you enable all permissions.
6363

6464
## Comparing autopilot mode, `--allow-all`, and `--no-ask-user`
6565

66-
`--allow-all`, and its alias `--yolo`, are permissions-related options that you can pass to the `copilot` command when you start an interactive session. For a full list of available options, see [AUTOTITLE](/copilot/reference/cli-command-reference#command-line-options).
66+
`--allow-all`, and its alias `--yolo`, are permissions-related options that you can pass to the `copilot` command when you start an interactive session. For a full list of available options, see [AUTOTITLE](/copilot/reference/copilot-cli-reference/cli-command-reference#command-line-options).
6767

6868
The `--allow-all` and `--yolo` options allow the CLI agent to use all tools, paths, and URLs. You can also set these permissions during an interactive session, by using the `/allow-all` or `/yolo` slash commands.
6969

content/copilot/concepts/agents/copilot-cli/research.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ To share the report you can either save it to a file or create a {% data variabl
140140
ls -dtl ~/.copilot/session-state/*/ | head -10
141141
```
142142

143-
* **The research agent uses a specific model**: The research agent is hard-coded to use a particular AI model (see [AUTOTITLE](/copilot/reference/cli-command-reference#built-in-agents)). The model selection is not configurable via the `/model` command. The research agent always uses the defined model regardless of what model you've selected for your main session.
143+
* **The research agent uses a specific model**: The research agent is hard-coded to use a particular AI model (see [AUTOTITLE](/copilot/reference/copilot-cli-reference/cli-command-reference#built-in-agents)). The model selection is not configurable via the `/model` command. The research agent always uses the defined model regardless of what model you've selected for your main session.
144144

145145
* **Report quality varies by query type**: The agent classifies your query into three types and adapts its response accordingly:
146146

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
contentType: how-tos
9+
category:
10+
- Build with Copilot CLI
11+
- Author and optimize with Copilot
12+
redirect_from:
13+
- /copilot/how-tos/copilot-cli/automate-with-actions
14+
---
15+
16+
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.
17+
18+
## Using {% data variables.copilot.copilot_cli_short %} in an Actions workflow
19+
20+
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.
21+
22+
Workflows can follow this pattern:
23+
1. **Trigger**: Start the workflow on a schedule, in response to repository events, or manually.
24+
1. **Setup**: Checkout code, set up environment.
25+
1. **Install**: Install {% data variables.copilot.copilot_cli %} on the runner.
26+
1. **Authenticate**: Ensure the CLI has the necessary permissions to access the repository and make changes.
27+
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.
28+
29+
### Example workflow
30+
31+
The following workflow generates details of changes made today in the default branch of the repository and displays these details as the summary for the workflow run.
32+
33+
```yaml copy
34+
name: Daily summary
35+
on:
36+
workflow_dispatch:
37+
# Run this workflow daily at 5:30pm UTC
38+
schedule:
39+
- cron: '30 17 * * *'
40+
permissions:
41+
contents: read
42+
jobs:
43+
daily-summary:
44+
runs-on: ubuntu-latest
45+
steps:
46+
- name: Checkout
47+
uses: {% data reusables.actions.action-checkout %}
48+
with:
49+
fetch-depth: 0
50+
51+
- name: Set up Node.js environment
52+
uses: {% data reusables.actions.action-setup-node %}
53+
54+
- name: Install Copilot CLI
55+
run: npm install -g @github/copilot
56+
57+
- name: Run Copilot CLI
58+
env:
59+
{% raw %}COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}{% endraw %}
60+
run: |
61+
copilot -p "Review the git log for this repository and write a bullet point summary of all code changes that were made today, with links to the relevant commit on GitHub. Above the bullet list give a description (max 100 words) summarizing the changes made. Write the details to summary.md" --allow-tool='shell(git:*)' --allow-tool=write --no-ask-user
62+
cat summary.md >> "$GITHUB_STEP_SUMMARY"
63+
```
64+
65+
The following sections explain each part of this workflow.
66+
67+
## Trigger
68+
69+
In this example, the workflow runs on a daily schedule and can also be triggered manually.
70+
71+
The `workflow_dispatch` trigger lets you run the workflow manually from the **Actions** tab of your repository on {% data variables.product.github %}, which is useful when testing changes to your prompt or workflow configuration.
72+
73+
The `schedule` trigger runs the workflow automatically at a specified time using cron syntax.
74+
75+
```yaml copy
76+
on:
77+
# Allows manual triggering of this workflow
78+
workflow_dispatch:
79+
# Run this workflow daily at 11:55pm UTC
80+
schedule:
81+
- cron: '55 23 * * *'
82+
```
83+
84+
## Setup
85+
86+
Set up the job so {% data variables.copilot.copilot_cli_short %} can access your repository and run on the Actions runner. This allows {% data variables.copilot.copilot_cli_short %} to analyze the repository context, when generating the daily summary.
87+
88+
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`.
89+
90+
```yaml copy
91+
permissions:
92+
contents: read
93+
jobs:
94+
daily-summary:
95+
runs-on: ubuntu-latest
96+
steps:
97+
- name: Checkout
98+
uses: {% data reusables.actions.action-checkout %}
99+
with:
100+
fetch-depth: 0
101+
```
102+
103+
## Install
104+
105+
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).
106+
107+
In this example, the workflow installs {% data variables.copilot.copilot_cli %} globally with npm.
108+
109+
```yaml copy
110+
- name: Set up Node.js environment
111+
uses: {% data reusables.actions.action-setup-node %}
112+
113+
- name: Install Copilot CLI
114+
run: npm install -g @github/copilot
115+
```
116+
117+
## Authenticate
118+
119+
To allow {% data variables.copilot.copilot_cli_short %} to run on an Actions runner, you need to authenticate a {% data variables.product.github %} user account with a valid {% data variables.product.prodname_copilot_short %} license.
120+
121+
**Step 1: Create a {% data variables.product.pat_generic %} (PAT) with the "Copilot Requests" permission:**
122+
1. Go to your personal settings for creating a {% data variables.product.pat_v2 %}: [github.com/settings/personal-access-tokens/new](https://github.com/settings/personal-access-tokens/new?ref_product=copilot&ref_type=engagement&ref_style=text).
123+
1. Create a new PAT with the "Copilot Requests" permission.
124+
1. Copy the token value.
125+
126+
**Step 2: Store the PAT as an Actions repository secret:**
127+
1. In your repository, go to **Settings** > **Secrets and variables** > **Actions** and click **New repository secret**.
128+
1. Give the secret a name that you will use in the workflow. In this example we're using `PERSONAL_ACCESS_TOKEN` as the name of the secret.
129+
1. Paste the token value into the "Secret" field and click **Add secret**.
130+
131+
The workflow sets a special environment variable with the value of the repository secret. {% data variables.copilot.copilot_cli_short %} supports several special environment variables for authentication. In this example, the workflow uses `COPILOT_GITHUB_TOKEN`, which is specific to {% data variables.copilot.copilot_cli_short %} and allows you to set different permissions for {% data variables.product.prodname_copilot_short %} than you might use elsewhere with the built-in `GITHUB_TOKEN` environment variable.
132+
133+
```yaml copy
134+
- name: Run Copilot CLI
135+
env:
136+
{% raw %}COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}{% endraw %}
137+
```
138+
139+
## Run {% data variables.copilot.copilot_cli_short %}
140+
141+
Use `copilot -p PROMPT [OPTIONS]` to run the CLI programmatically and exit when the command completes.
142+
143+
The CLI prints its response to standard output, which is recorded in the log for the Actions workflow run. However, to make the details of changes easier to access, this example adds this information to the summary for the workflow run.
144+
145+
```yaml copy
146+
run: |
147+
copilot -p "Review the git log for this repository and write a bullet point summary of all code changes that were made today, with links to the relevant commit on GitHub. Above the bullet list give a description (max 100 words) summarizing the changes made. Write the details to summary.md" --allow-tool='shell(git:*)' --allow-tool=write --no-ask-user
148+
cat summary.md >> "$GITHUB_STEP_SUMMARY"
149+
```
150+
151+
This example uses several options after the CLI prompt:
152+
153+
* `--allow-tool='shell(git:*)'` allows {% data variables.product.prodname_copilot_short %} to run Git commands to analyze the repository history. This is necessary to generate the summary of recent changes.
154+
* `--allow-tool='write'` allows {% data variables.product.prodname_copilot_short %} to write the generated summary to a file on the runner.
155+
* `--no-ask-user` prevents the CLI from prompting for user input, which is important when running in an automated workflow where there is no user to respond to requests for additional input.
156+
157+
## Next steps
158+
159+
After you confirm the workflow generates a summary of changes, 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. For example, you could:
160+
161+
* Create a pull request to update a changelog file in the repository with the day's changes.
162+
* Email the summary to the repository maintainers.
163+
164+
## Further reading
165+
166+
* [AUTOTITLE](/copilot/reference/copilot-cli-reference/cli-command-reference)
167+
* [AUTOTITLE](/actions)
168+
* [AUTOTITLE](/copilot/how-tos/copilot-cli/automate-copilot-cli/run-cli-programmatically)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Automate with GitHub Copilot CLI
3+
shortTitle: Automate with Copilot CLI
4+
intro: 'Learn how to use {% data variables.copilot.copilot_cli_short %} in the terminal, in scripts, or in Actions workflows.'
5+
versions:
6+
feature: copilot
7+
contentType: how-tos
8+
children:
9+
- /quickstart
10+
- /run-cli-programmatically
11+
- /automate-with-actions
12+
---
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Quickstart for automating with {% data variables.copilot.copilot_cli %}
3+
shortTitle: Quickstart
4+
allowTitleToDifferFromFilename: true
5+
intro: "Build an automation with {% data variables.copilot.copilot_cli_short %} in minutes."
6+
versions:
7+
feature: copilot
8+
redirect_from:
9+
- /copilot/how-tos/copilot-cli/automate-copilot-cli/overview
10+
contentType: how-tos
11+
category:
12+
- Author and optimize with Copilot # Copilot discovery page
13+
- Build with Copilot CLI # Copilot CLI bespoke page
14+
- Quickstarts
15+
---
16+
17+
## Overview
18+
19+
You can use {% data variables.copilot.copilot_cli %} to programmatically run Copilot prompts. There are two main ways to do this:
20+
21+
* Run a {% data variables.copilot.copilot_cli_short %} prompt directly from your terminal.
22+
* Write a script or automation that leverages {% data variables.copilot.copilot_cli_short %}.
23+
24+
This guide will walk you through a simple use case for each option.
25+
26+
## Run a prompt from the command line
27+
28+
When you want to pass {% data variables.copilot.copilot_cli_short %} a prompt without initiating an interactive session, use the `-p` flag.
29+
30+
```shell copy
31+
copilot -p "Summarize what this file does: ./README.md"
32+
```
33+
34+
Any prompt you would type in an interactive session works with `-p`.
35+
36+
## Use {% data variables.copilot.copilot_cli_short %} in a script
37+
38+
The real power of programmatic mode comes from writing scripts to automate AI-powered tasks. Within a script, you can generate the prompt, or replace parts of a prompt with dynamic content, and then capture the output or pass it to another part of the script.
39+
40+
Let's create a script that finds all files larger than 10 MB in the current directory, uses {% data variables.copilot.copilot_cli_short %} to generate a brief description of each file, and then emails a summary report.
41+
42+
1. In your repository, create a new file called `find_large_files.sh` and add the following content.
43+
44+
```bash copy
45+
#!/bin/bash
46+
# Find files over 10 MB, use Copilot CLI to describe them, and email a summary
47+
48+
EMAIL_TO="[email protected]"
49+
SUBJECT="Large file found"
50+
BODY=""
51+
52+
while IFS= read -r -d '' file; do
53+
size=$(du -h "$file" | cut -f1)
54+
description=$(copilot -p "Describe this file briefly: $file" -s 2>/dev/null)
55+
BODY+="File: $file"$'\n'"Size: $size"$'\n'"Description: $description"$'\n\n'
56+
done < <(find . -type f -size +10M -print0)
57+
58+
if [ -z "$BODY" ]; then
59+
echo "No files over 10MB found."
60+
exit 0
61+
fi
62+
63+
echo -e "To: $EMAIL_TO\nSubject: $SUBJECT\n\n$BODY" | sendmail "$EMAIL_TO"
64+
echo "Email sent to $EMAIL_TO with large file details."
65+
```
66+
67+
1. Make the script executable.
68+
69+
```shell copy
70+
chmod +x find_large_files.sh
71+
```
72+
73+
1. Run the script.
74+
75+
```shell copy
76+
./find_large_files.sh
77+
```
78+
79+
This script leverages {% data variables.copilot.copilot_cli_short %} to generate descriptions of the files you are searching for, so you can quickly understand the contents of large files without opening them.
80+
81+
You can also automatically trigger these scripts in response to events, such as a new file being added to a directory, or on a schedule using cron jobs or CI/CD pipelines.
82+
83+
## Further reading
84+
85+
* [AUTOTITLE](/copilot/how-tos/copilot-cli/automate-copilot-cli/run-cli-programmatically)
86+
* [AUTOTITLE](/copilot/how-tos/copilot-cli/automate-copilot-cli/automate-with-actions)
87+
* [AUTOTITLE](/copilot/reference/copilot-cli-reference/cli-programmatic-reference)

0 commit comments

Comments
 (0)