【Github】Github Actions ~ TOML Linter ~

■ はじめに

https://dk521123.hatenablog.com/entry/2024/08/08/202314
https://dk521123.hatenablog.com/entry/2024/08/09/141801

の続き。

今回は、TOMLファイルをGithub Actionsでチェックする方法について
調べてみた。

目次

【1】自作する
 1)サンプル
 2)動作確認
【2】補足:TOML Linter の Third-Party Github Action

【1】自作する

* TOML Linter の Third-Party Github Actionを調べたが
 現状(2024/08/10現在)使えるものがなさそう(※)なので
 文法チェックする簡易的なものを自作する、、、

※詳細は、「【2】補足:TOML Linter の Third-Party Github Action」参照

1)サンプル

.github/workflows/demo-validate-toml.yml

name: ValidateToml

on:
  push:
    paths:
      # TOML ファイル (.toml) をプッシュするたびに実行
      - '**.toml'
jobs:
  validate-toml:
    runs-on: ubuntu-latest

    steps:
      - name: "Checkout"
        uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12' 
      - name: Validate TOML file
        run: |
          for file in '**.toml'
          do
            echo "- target_file: ${file}"
            python .github/validate_toml.py $file
          done

.github/validate_toml.py

import tomllib
import sys

# e.g. python3 validate_toml.py test.toml
input_toml = sys.argv[1]

with open(input_toml,'rb') as file:
  output = tomllib.load(file)
  print(output)

2)動作確認

[OK] test.toml

# This is a comment
title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00

[[table_array]]
key1 = ["value1", "value2", 'value3']

出力結果:正常終了

Run for file in '**.toml'
- target_file: **.toml
{'title': 'TOML Example', 'owner': {'name': 'Tom Preston-Werner', 'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=57600)))}, 'table_array': [{'key1': ['value1', 'value2', 'value3']}]}

[NG] test.toml

# This is a comment
title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00

[[table_array]]
key1 = ["value1", "value2" 'value3'] # カンマ忘れで文法エラー

出力結果:エラー

Run for file in '**.toml'
- target_file: **.toml
Traceback (most recent call last):
  File "/home/runner/work/github-actions-demo/github-actions-demo/.github/validate_toml.py", line 8, in <module>
    output = tomllib.load(file)
             ^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/tomllib/_parser.py", line 66, in load
    return loads(s, parse_float=parse_float)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/tomllib/_parser.py", line 102, in loads
    pos = key_value_rule(src, pos, out, header, parse_float)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/tomllib/_parser.py", line 326, in key_value_rule
    pos, key, value = parse_key_value_pair(src, pos, parse_float)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/tomllib/_parser.py", line 369, in parse_key_value_pair
    pos, value = parse_value(src, pos, parse_float)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/tomllib/_parser.py", line 616, in parse_value
    return parse_array(src, pos, parse_float)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/tomllib/_parser.py", line 428, in parse_array
    raise suffixed_err(src, pos, "Unclosed array")
tomllib.TOMLDecodeError: Unclosed array (at line 9, column 28)
Error: Process completed with exit code 1.

【2】補足:TOML Linter の Third-Party Github Action

Third-Party Github Action 「gh-action-toml-linter」を
利用することを考えたが、
以下「1)エラーメッセージ」が表示されたので、
この方法は諦める。
(使い方、間違っている可能性はあるが)

yisonPylkita/gh-action-toml-linter
https://github.com/yisonPylkita/gh-action-toml-linter

      - name: Validate TOML file
        uses: yisonPylkita/gh-action-toml-linter@0.1.9
        with:
          # Execute lint check on a specific file or folder.
          file: ./

1)エラーメッセージ

Scanning all TOML files at /github/workspace
Check file ./test.toml
Error loading shared library libgcc_s.so.1: No such file or directory (needed by ./taplo)
Error relocating ./taplo: _Unwind_Resume: symbol not found
Error relocating ./taplo: _Unwind_FindEnclosingFunction: symbol not found
Error relocating ./taplo: _Unwind_Backtrace: symbol not found
Error relocating ./taplo: _Unwind_GetRegionStart: symbol not found
Error relocating ./taplo: _Unwind_GetTextRelBase: symbol not found
Error relocating ./taplo: _Unwind_RaiseException: symbol not found
Error relocating ./taplo: _Unwind_GetIPInfo: symbol not found
Error relocating ./taplo: _Unwind_GetLanguageSpecificData: symbol not found
Error relocating ./taplo: _Unwind_GetIP: symbol not found
Error relocating ./taplo: _Unwind_GetCFA: symbol not found
Error relocating ./taplo: _Unwind_GetDataRelBase: symbol not found
Error relocating ./taplo: _Unwind_SetGR: symbol not found
Error relocating ./taplo: _Unwind_DeleteException: symbol not found
Error relocating ./taplo: _Unwind_SetIP: symbol not found
Error: {TOML file not formatted}

関連記事

Github Actions ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/11/04/142835
Github Actions ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2022/06/16/151443
Github Actions ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/12/22/195715
Github Actions ~ 差分ファイルを抽出する ~
https://dk521123.hatenablog.com/entry/2024/08/11/123351
Github Actions ~ Pythonを使うには ~
https://dk521123.hatenablog.com/entry/2024/02/04/011205
Github Actions ~ Python関連 ~
https://dk521123.hatenablog.com/entry/2022/06/21/143624
Github Actions ~ SQL Linter ~
https://dk521123.hatenablog.com/entry/2024/03/04/180308
Github Actions ~ Scala Linter ~
https://dk521123.hatenablog.com/entry/2024/04/02/002828
Github Actions ~ JSON Linter ~
https://dk521123.hatenablog.com/entry/2024/08/08/202314
Github Actions ~ YAML Linter ~
https://dk521123.hatenablog.com/entry/2024/08/09/141801
Python ~ 基本編 / コマンドライン引数 ~
https://dk521123.hatenablog.com/entry/2019/10/11/223651
Python ~ TOML ~
https://dk521123.hatenablog.com/entry/2024/01/27/000110
reviewdog ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2024/04/18/161200