LoginSignup
1
0

More than 1 year has passed since last update.

GitHub ActionsでNGを検出した際にPRにコメントをする方法

Posted at

なぜするのか

GitHub Actionsでのチェックで失敗した場合に、Actionsのコンソールまで見に行くのが大変なので
PRを見るだけでわかるようにするため。

やり方

  • permissionspull-requests: write設定をする
  • PRにコメントするためPRの番号を取得する
  • ghコマンドを利用するためGITHUB_TOKENを設定する
  • コメントしたい内容をファイルに出力する
  • ghコマンドでコメントをする
gh-comment.yaml
name: main

on:
  pull_request:
    types: [ opened, synchronize ]

jobs:
  main:
    runs-on: ubuntu-22.04
    permissions:
      contents: read
      pull-requests: write
    timeout-minutes: 5
    steps:
      - name: CommentTest
        env:
          PR_NUMBER: ${{ github.event.number }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          echo "PRへのコメント内容をファイルに出力する" > /tmp/comment
          gh pr comment "${PR_NUMBER}" -F /tmp/comment

実行例

Screenshot_1241.png

実際のActionsに組み込んでみる

設定yaml

textlintなどで失敗すると、そこで処理が終了してします。
そのため||trueとしておき、その行は正常終了させる。
そして、ファイル出力があった場合には内容を出力して異常終了とする。

main.yaml(抜粋)
      - name: textlint
        env:
          PR_NUMBER: ${{ github.event.number }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git diff origin/main HEAD |grep "^+++" |awk '{print $2}'|sed "s|b/||"| grep -e \.md$ -e \.md\"$ |xargs npx textlint > /tmp/comment || true
          if [ -s /tmp/comment ]; then
            gh pr comment "${PR_NUMBER}" -F /tmp/comment
            exit 1
          fi

エラーがあった際

Screenshot_1243.png

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0