この記事は、GitHub Actions Advent Calendar 2022 の9日目の記事です。

GitHub Actions Advent Calendar 2022
https://qiita.com/advent-calendar/2022/github-actions

GitHubActionsに限らず、
CIでビルド・自動テストが成功しているが標準エラー出力にWarningが出ている時、
なるべく、それらのWarningを解消してからマージすべきです。
が、Statusが成功になっており、気づかずにマージしてしまいがちなので、
このエントリでは、標準エラー出力に何か出ていたら失敗させる方法を紹介します。

実施方法

結論から書くと、次のようなWorkflowで実施できます。

.github/workflows/ci.yaml

name: ci
on:
  push:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: build
      run: |
        echo "warning" >&2 # ビルドや自動テストの処理に置き換える        
      shell: bash -c "bash -e {0} 2> >(tee stderr.log >&2)"
    - name: check stderr of build
      run: test ! -s stderr.log

処理の説明

build stepでは、
標準エラー出力をファイルstderr.logに出力させるため、
shellを置き換えています。

shellの置き換える方法は、次を参照してください。

jobs.<job_id>.steps[*].shell | GitHub Actions | GitHub Docs
https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsshell

check stepでは、
stderr.logが空でない場合に、失敗するようにチェックしています。

標準エラー出力が空ではなく、
warningという文字が含まれている場合などに限定したい場合は、
! grep warning stderr.log のようにすると良いと思います。

# ちなみに、1つのstepで対応しようと、
# build stepのshellを、
# bash -c "bash -e {0} 2> >(tee stderr.log >&2) && test ! -s stderr.log"
# みたいに買いても上手くいかないです。
# おそらく、teeでの出力のファイル複製が完了する前にtestが動いてしまうことがあるため

以上。