若要详细了解 GitHub CLI,请参阅“关于 GitHub CLI”。
GitHub CLI 预安装在所有 GitHub 托管的运行程序上。 对于使用 GitHub CLI 的每个步骤,必须将调用 GITHUB_TOKEN
的环境变量设置为具有所需范围的令牌。
可以执行任何 GitHub CLI 命令。 例如,此工作流使用 gh issue comment
子命令在打开问题时添加注释。
name: Comment when opened on: issues: types: - opened jobs: comment: runs-on: ubuntu-latest steps: - run: gh issue comment $ISSUE --body "Thank you for opening this issue!" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ISSUE: ${{ github.event.issue.html_url }}
name: Comment when opened
on:
issues:
types:
- opened
jobs:
comment:
runs-on: ubuntu-latest
steps:
- run: gh issue comment $ISSUE --body "Thank you for opening this issue!"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE: ${{ github.event.issue.html_url }}
还可以通过 GitHub CLI 执行 API 调用。 例如,此工作流首先使用 gh api
子命令查询 GraphQL API 并分析结果。 然后,它将结果存储在可在后续步骤中访问的环境变量中。 在第二步中,它使用 gh issue create
子命令创建包含第一步中信息的问题。
name: Report remaining open issues on: schedule: # Daily at 8:20 UTC - cron: '20 8 * * *' jobs: track_pr: runs-on: ubuntu-latest steps: - run: | numOpenIssues="$(gh api graphql -F owner=$OWNER -F name=$REPO -f query=' query($name: String!, $owner: String!) { repository(owner: $owner, name: $name) { issues(states:OPEN){ totalCount } } } ' --jq '.data.repository.issues.totalCount')" echo 'NUM_OPEN_ISSUES='$numOpenIssues >> $GITHUB_ENV env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} OWNER: ${{ github.repository_owner }} REPO: ${{ github.event.repository.name }} - run: | gh issue create --title "Issue report" --body "$NUM_OPEN_ISSUES issues remaining" --repo $GITHUB_REPOSITORY env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
name: Report remaining open issues
on:
schedule:
# Daily at 8:20 UTC
- cron: '20 8 * * *'
jobs:
track_pr:
runs-on: ubuntu-latest
steps:
- run: |
numOpenIssues="$(gh api graphql -F owner=$OWNER -F name=$REPO -f query='
query($name: String!, $owner: String!) {
repository(owner: $owner, name: $name) {
issues(states:OPEN){
totalCount
}
}
}
' --jq '.data.repository.issues.totalCount')"
echo 'NUM_OPEN_ISSUES='$numOpenIssues >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
- run: |
gh issue create --title "Issue report" --body "$NUM_OPEN_ISSUES issues remaining" --repo $GITHUB_REPOSITORY
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}