Skip to main content

Adding labels to issues

You can use GitHub Actions to automatically label issues.

Introduction

This tutorial demonstrates how to use the GitHub CLI in a workflow to label newly opened or reopened issues. For example, you can add the triage label every time an issue is opened or reopened. Then, you can see all issues that need to be triaged by filtering for issues with the triage label.

The GitHub CLI allows you to easily use the GitHub API in a workflow.

In the tutorial, you will first make a workflow file that uses the GitHub CLI. Then, you will customize the workflow to suit your needs.

Creating the workflow

  1. 选择要应用此项目管理工作流程的仓库。 您可以使用您有写入权限的现有仓库,或者创建一个新的仓库。 有关创建仓库的详细信息,请参阅 创建新仓库

  2. 在存储库中,创建一个名为 .github/workflows/YOUR_WORKFLOW.yml 的文件,将 YOUR_WORKFLOW 替换为你选择的名称。 这是一个工作流程文件。 有关在 GitHub 上创建新文件的详细信息,请参阅 创建新文件

  3. Copy the following YAML contents into your workflow file.

    YAML
    name: Label issues
    on:
      issues:
        types:
          - reopened
          - opened
    jobs:
      label_issues:
        runs-on: ubuntu-latest
        permissions:
          issues: write
        steps:
          - run: gh issue edit "$NUMBER" --add-label "$LABELS"
            env:
              GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              GH_REPO: ${{ github.repository }}
              NUMBER: ${{ github.event.issue.number }}
              LABELS: triage
    
  4. Customize the env values in your workflow file:

    • The GH_TOKEN, GH_REPO, and NUMBER values are automatically set using the github and secrets contexts. You do not need to change these.
    • Change the value for LABELS to the list of labels that you want to add to the issue. The label(s) must exist for your repository. Separate multiple labels with commas. For example, help wanted,good first issue. For more information about labels, see 管理标签.
  5. 将工作流程文件提交到仓库的默认分支。 有关详细信息,请参阅“创建新文件”。

Testing the workflow

Every time an issue in your repository is opened or reopened, this workflow will add the labels that you specified to the issue.

Test out your workflow by creating an issue in your repository.

  1. Create an issue in your repository. For more information, see 创建议题.
  2. To see the workflow run that was triggered by creating the issue, view the history of your workflow runs. For more information, see Viewing workflow run history.
  3. When the workflow completes, the issue that you created should have the specified labels added.

Next steps