notebook

都内でWEB系エンジニアやってます。

GitHub ActionsのAnnotationリストを取得するCLI拡張を作った

背景、目的

少し前の話になるが、GitHub ActionsでNodeのバージョン12を使っているActionがあるとDeprecatedになるよというWarningがAnnotationへ出るようになった

しっかりメンテナンスしているリポジトリだったら気付いて対応って感じでよいが漏れているものとかもありそうだなーという感じがした

発見するためには、各ActionsのWeb画面でSummaryを見ればよいが、管理するリポジトリが増えたりするといちいち画面まで行って確認するのは時間が掛かる

ということで

GitHub ActionsのAnnotationのリストを取得するGitHub CLIの拡張を作ってみた

swfz/gh-annotations: list of annotations from the recently executed Workflow

github.com

動作イメージ

APIを結構たたくことになるので、リポジトリの各Workflowの最新の実行結果に対してAnnotationを見るようにしている

なので特定のWorkflowで少し前の実行で発生したAnnotationは取得できない

gh annotations -repo swfz/til | jq
[
  {
    "repository": "swfz/til",
    "workflow_name": "slack notification",
    "workflow_event": "workflow_run",
    "workflow_path": ".github/workflows/slack.yml",
    "workflow_url": "https://github.com/swfz/til/actions/runs/3697438928",
    "job_name": "main",
    "job_conclusion": "success",
    "annotation_level": "warning",
    "message": "Node.js 12 actions are deprecated. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/. Please update the following actions to use Node.js 16: swfz/slack-workflow-status@feature/follow-workflow-run"
  },
  {
    "repository": "swfz/til",
    "workflow_name": "ci",
    "workflow_event": "push",
    "workflow_path": ".github/workflows/ci.yml",
    "workflow_url": "https://github.com/swfz/til/actions/runs/3697430624",
    "job_name": "build",
    "job_conclusion": "failure",
    "annotation_level": "failure",
    "message": "Process completed with exit code 1."
  },
  {
    "repository": "swfz/til",
    "workflow_name": "lint content",
    "workflow_event": "push",
    "workflow_path": ".github/workflows/lint.yml",
    "workflow_url": "https://github.com/swfz/til/actions/runs/3694817234",
    "job_name": "run textlint",
    "job_conclusion": "failure",
    "annotation_level": "failure",
    "message": "Process completed with exit code 1."
  }
]

インストール

gh extension install swfz/gh-annotations

これはGitHub CLI Extensionだからって話だが上記コマンドだけで使えるようになる

操作方法

gh annotations | jq

現在いるディレクトリのリポジトリに対して実行する

gh annotations -repo swfz/sample | jq

と言った感じでリポジトリを指定することも可能

何も指定しない場合は自身がいるディレクトリを対象としている

冒頭でメンテナンスしているリポジトリの情報をすべて得たい場合は

リポジトリの一覧を取得してそれぞれに対してgh annotationsを実行してすべてのリポジトリのAnnotationを出力するなどもワンライナーで工夫すれば可能

gh repo list --limit 200 --json 'nameWithOwner' | jq -rc '.[]|.nameWithOwner' | xargs -i sh -lc "gh annotations -repo {}" | tee -a annotations.json

終わり

GitHub ActionsのAnnotationの結果を取得するGitHub CLI拡張を作った話でした

  • CIに組み込んで何かAnnotationが発生した場合にキャッチして通知する
  • 定期的に管理している全リポジトリの結果を出力してチェックする

などの用途に使えるかなと思っています

開発では、JSON出力以外にリストでの出力も実装しようと思ったが気力が切れてしまったのでいったんこの状態で記事を書くことにした

良ければ使ってみてください!

swfz/gh-annotations: list of annotations from the recently executed Workflow