|
| 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) |
0 commit comments