{"meta":{"title":"Utilisation de l’interface CLI GitHub dans les flux de travail","intro":"Vous pouvez créer des scripts avec GitHub CLI dans les workflows GitHub Actions.","product":"GitHub Actions","breadcrumbs":[{"href":"/fr/actions","title":"GitHub Actions"},{"href":"/fr/actions/how-tos","title":"Guides pratiques"},{"href":"/fr/actions/how-tos/write-workflows","title":"Écrire des workflows"},{"href":"/fr/actions/how-tos/write-workflows/choose-what-workflows-do","title":"Choisir ce que font les workflows"},{"href":"/fr/actions/how-tos/write-workflows/choose-what-workflows-do/use-github-cli","title":"Utiliser l’interface CLI GitHub"}],"documentType":"article"},"body":"# Utilisation de l’interface CLI GitHub dans les flux de travail\n\nVous pouvez créer des scripts avec GitHub CLI dans les workflows GitHub Actions.\n\n> \\[!NOTE]\n> Pour plus d’informations sur GitHub CLI, consultez [À propos de l’interface CLI GitHub](/fr/github-cli/github-cli/about-github-cli).\n\nL’GitHub CLI est préinstallé sur tous les exécuteurs hébergés par GitHub. Pour chaque étape utilisant GitHub CLI, vous devez définir une variable d’environnement, appelée `GH_TOKEN`, qui correspond à un jeton avec les étendues requises.\n\nVous pouvez exécuter n’importe quelle commande de l’GitHub CLI. Par exemple, ce workflow utilise la sous-commande `gh issue comment` pour ajouter un commentaire lorsqu’un problème est ouvert.\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\nVous pouvez également exécuter des appels d’API via l’GitHub CLI. Par exemple, ce workflow utilise d’abord la sous-commande `gh api` pour interroger l’API GraphQL et analyser le résultat. Ensuite, il stocke le résultat dans une variable d’environnement auquel il peut accéder dans une étape ultérieure. Dans la deuxième étape, il utilise la sous-commande `gh issue create` pour créer un problème contenant les informations de la première étape.\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```"}