Skip to main content

Scheduling issue creation

You can use GitHub Actions to create an issue on a regular basis for things like daily meetings or quarterly reviews.

Introduction

This tutorial demonstrates how to use the GitHub CLI to create an issue on a regular basis. For example, you can create an issue each week to use as the agenda for a team meeting. For more information about GitHub CLI, see Using GitHub CLI in workflows.

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: Weekly Team Sync
    on:
      schedule:
        - cron: 20 07 * * 1
    
    jobs:
      create_issue:
        name: Create team sync issue
        runs-on: ubuntu-latest
        permissions:
          issues: write
        steps:
          - name: Create team sync issue
            run: |
              if [[ $CLOSE_PREVIOUS == true ]]; then
                previous_issue_number=$(gh issue list \
                  --label "$LABELS" \
                  --json number \
                  --jq '.[0].number')
                if [[ -n $previous_issue_number ]]; then
                  gh issue close "$previous_issue_number"
                  gh issue unpin "$previous_issue_number"
                fi
              fi
              new_issue_url=$(gh issue create \
                --title "$TITLE" \
                --assignee "$ASSIGNEES" \
                --label "$LABELS" \
                --body "$BODY")
              if [[ $PINNED == true ]]; then
                gh issue pin "$new_issue_url"
              fi
            env:
              GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              GH_REPO: ${{ github.repository }}
              TITLE: Team sync
              ASSIGNEES: monalisa,doctocat,hubot
              LABELS: weekly sync,docs-team
              BODY: |
                ### Agenda
    
                - [ ] Start the recording
                - [ ] Check-ins
                - [ ] Discussion points
                - [ ] Post the recording
    
                ### Discussion Points
                Add things to discuss below
    
                - [Work this week](https://github.com/orgs/github/projects/3)
              PINNED: false
              CLOSE_PREVIOUS: false
    
  4. Customize the parameters in your workflow file:

    • Change the value for on.schedule to dictate when you want this workflow to run. In the example above, the workflow will run every Monday at 7:20 UTC. For more information about scheduled workflows, see 触发工作流的事件.
    • Change the value for ASSIGNEES to the list of GitHub usernames that you want to assign to the issue.
    • Change the value for LABELS to the list of labels that you want to apply to the issue.
    • Change the value for TITLE to the title that you want the issue to have.
    • Change the value for BODY to the text that you want in the issue body. The | character allows you to use a multi-line value for this parameter.
    • If you want to pin this issue in your repository, set PINNED to true. For more information about pinned issues, see 将议题固定到仓库.
    • If you want to close the previous issue generated by this workflow each time a new issue is created, set CLOSE_PREVIOUS to true. The workflow will close the most recent issue that has the labels defined in the labels field. To avoid closing the wrong issue, use a unique label or combination of labels.
  5. 将工作流程文件提交到仓库的默认分支。 有关详细信息,请参阅“创建新文件”。

Expected results

Based on the schedule parameter (for example, every Monday at 7:20 UTC), your workflow will create a new issue with the assignees, labels, title, and body that you specified. If you set PINNED to true, the workflow will pin the issue to your repository. If you set CLOSE_PREVIOUS to true, the workflow will close the most recent issue with matching labels.

注意

schedule 事件在 GitHub Actions 工作流运行期间负载过高时可能会延迟。 高负载时间包括每小时的开始时间。 如果负载足够高,可能会删除一些排队作业。 为了降低延迟的可能性,将您的工作流程安排在不同时间运行。

You can view the history of your workflow runs to see this workflow run periodically. For more information, see Viewing workflow run history.

Next steps