Diary of a Perpetual Student

Perpetual Student: A person who remains at university far beyond the normal period

GitHub Actions の workflow の lint を Github Actions で動かす

GitHub Actions で色々なコードの test や lint を動かしていると思います。

その際に workflow を yml で定義するのですが、この workflow ファイルはちゃんとチェックしてますか?

  • 「見よう見まねで yml を書いて push したけど、キー名が間違っていて動かなかった」
  • ::set-output などの deprecated な機能を今更使ってしまった」

こんな経験はありませんか?

Github Actions に関するその問題、Github Actions を使えば解決できます!

解決策

actionlint という Github Actions の workflow ファイルをチェックしてくれる lint ツールがあります。

github.com

こいつを Github Actions 上で動かすだけです。

動作させてみた例を以下に載せておきます。

github.com

deprecated な機能を利用して怒られた例

やり方

以下のように 2 つのファイルを用意して push しましょう。

.github/workflows/ci-actions.yml

on:
  push:
    paths:
      - .github/workflows/*.yml
      - .github/actionlint.yml
      - .github/actionlint-matcher.json

name: actionlint
jobs:
  actionlint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install actionlint
        shell: bash
        run: bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash)
      - name: Add problem matcher
        run: echo "::add-matcher::.github/actionlint-matcher.json"
      - name: run actionlint
        run: ./actionlint
        shell: bash

.github/actionlint-matcher.json

以下からコピーして配置してください。

github.com

{
  "problemMatcher": [
    {
      "owner": "actionlint",
      "pattern": [
        {
          "regexp": "^(?:\\x1b\\[\\d+m)?(.+?)(?:\\x1b\\[\\d+m)*:(?:\\x1b\\[\\d+m)*(\\d+)(?:\\x1b\\[\\d+m)*:(?:\\x1b\\[\\d+m)*(\\d+)(?:\\x1b\\[\\d+m)*: (?:\\x1b\\[\\d+m)*(.+?)(?:\\x1b\\[\\d+m)* \\[(.+?)\\]$",
          "file": 1,
          "line": 2,
          "column": 3,
          "message": 4,
          "code": 5
        }
      ]
    }
  ]
}

解説

この workflow は以下のような流れで実行されます。

  1. github から actionlint を install するコマンドを実行
  2. actionlint の出力から annotation (この行がエラーだよという表示)を作るためのテンプレートを設定する
  3. actionlint を実行する

上級 Tips

手元では検出されないエラーが GHA 上で動かすと検出される

actionlint は手元でもインストール実行できるコマンドで、VSCode の拡張として使っている人たちもいるでしょう。このとき、手元環境だとエラーが検出されないのに、Github Actions 上で動かすとエラーが表示されることがあります。

この原因は、shellcheck というコマンドがインストールされているかどうかの差異にあります。

actionlint は、shellcheck がインストールされているなら、run: などで yml 上に書いているシェルスクリプトに対して shellcheck を適用した結果を教えてくれます。

shellcheck は Github Actions の ubuntu-latest runner にデフォルトでインストールされているのに対し、手元環境ではインストールしてない人もいるでしょう。

If you want to enable shellcheck integration, install shellcheck command. Note that shellcheck is pre-installed on Ubuntu worker.

actionlint/usage.md at main · rhysd/actionlint · GitHub より引用)

custom runner や self-hosted runner の名前がエラー扱いになる

基本 runs-on: ubuntu-latest として用意された runner を用いて動かすことが多いですが、大規模なプロジェクトでは custom runner や self-hosted runner を利用している場合もあるでしょう。

このとき、runs-on: hoge のように、デフォルトで用意されていない runner を指定すると、以下のように怒られます。

label "hoge" is unknown. available labels are "windows-latest", "windows-2022", ...

これをエラーとして検出しないようにするには、actionlint の設定ファイルを以下のように設置する必要があります。

.github/actionlint.yml

self-hosted-runner:
  labels:
    - hoge

cf.) actionlint/config.md at main · rhysd/actionlint · GitHub

まとめ

GitHub Actions のチェックが GitHub Actions でできました!!


余談

このエントリは社内でやっている技術勉強会「忘年LT大会」向けのものですが、せっかくなので public な場で書き留めておきました。

id:arthur-1ウロボロス的なものが好きで、はてなエンジニア Advent Calendar に「Advent Calendar の監視をする」というネタで投稿したのですが全くウケませんでした。今回もまた懲りずに同じ過ちを繰り返したわけであります。

blog.arthur1.dev