{"meta":{"title":"ワークフローでの GitHub CLI の使用","intro":"GitHub CLI ワークフローでは、GitHub Actions を使用してスクリプトを作成できます。","product":"GitHub Actions","breadcrumbs":[{"href":"/ja/enterprise-cloud@latest/actions","title":"GitHub Actions"},{"href":"/ja/enterprise-cloud@latest/actions/how-tos","title":"方法"},{"href":"/ja/enterprise-cloud@latest/actions/how-tos/write-workflows","title":"ワークフローを書き込む"},{"href":"/ja/enterprise-cloud@latest/actions/how-tos/write-workflows/choose-what-workflows-do","title":"ワークフロー動作を選択する"},{"href":"/ja/enterprise-cloud@latest/actions/how-tos/write-workflows/choose-what-workflows-do/use-github-cli","title":"GitHub CLI の使用"}],"documentType":"article"},"body":"# ワークフローでの GitHub CLI の使用\n\nGitHub CLI ワークフローでは、GitHub Actions を使用してスクリプトを作成できます。\n\n> \\[!NOTE]\n> GitHub CLI の詳細については、「[GitHub CLI について](/ja/enterprise-cloud@latest/github-cli/github-cli/about-github-cli)」を参照してください。\n\nGitHub CLI は、GitHub でホストされるすべてのランナーにプレインストールされます。 GitHub CLI を使う各ステップで、`GH_TOKEN` という環境変数に必要なスコープを持つトークンを設定する必要があります。\n\n任意の GitHub CLI コマンドを実行できます。 たとえば、このワークフローでは、`gh issue comment` サブコマンドを使って、issue が開かれるときにコメントを追加します。\n\n```yaml copy\nname: Comment when opened\non:\n  issues:\n    types:\n      - opened\njobs:\n  comment:\n    runs-on: ubuntu-latest\n    steps:\n      - run: gh issue comment $ISSUE --body \"Thank you for opening this issue!\"\n        env:\n          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n          ISSUE: ${{ github.event.issue.html_url }}\n```\n\nGitHub CLI を使って API 呼び出しを実行することもできます。 たとえば、このワークフローでは、最初に `gh api` サブコマンドを使って GraphQL API のクエリを実行し、結果を解析します。 次に、後のステップでアクセスできる環境変数に結果を格納します。 2 番目のステップでは、`gh issue create` サブコマンドを使って、最初のステップの情報を含む issue を作成します。\n\n```yaml copy\nname: Report remaining open issues\non:\n  schedule:\n    # Daily at 8:20 UTC\n    - cron: '20 8 * * *'\njobs:\n  track_pr:\n    runs-on: ubuntu-latest\n    steps:\n      - run: |\n          numOpenIssues=\"$(gh api graphql -F owner=$OWNER -F name=$REPO -f query='\n            query($name: String!, $owner: String!) {\n              repository(owner: $owner, name: $name) {\n                issues(states:OPEN){\n                  totalCount\n                }\n              }\n            }\n          ' --jq '.data.repository.issues.totalCount')\"\n\n          echo 'NUM_OPEN_ISSUES='$numOpenIssues >> $GITHUB_ENV\n        env:\n          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n          OWNER: ${{ github.repository_owner }}\n          REPO: ${{ github.event.repository.name }}\n      - run: |\n          gh issue create --title \"Issue report\" --body \"$NUM_OPEN_ISSUES issues remaining\" --repo $GITHUB_REPOSITORY\n        env:\n          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n```"}