🐈

sqruff を GitHub Actions で動かす

に公開

SQL用の Linter/Formatter として sqruff [1] があります. CLIツールなので開発環境で使えますが, Linter/Formatter は自動化してこそです. 特にプロダクトコードではないSQLでは変更頻度も少なく, 実行するのを忘れがちです. そこで GitHub Actions を使って自動化しましょう.

サンプルコードは以下.

https://github.com/toms74209200/sqruff-github-actions-example

sqruff の使い方は割愛します. Lint 用のコマンドとして sqruff lint が, format 用のコマンドとして sqruff fix があります. 今回はこれらを自動化します. sqruff の元となっている SQLFluff[2] でもほとんど同じようにできます.

Format

まず format から実装します. ワークフローとしては format 結果が正しいか検証するのではなく, format を実行して適用されたファイルをコミットします.

.github/workflows/job-format-sql.yml
name: Job - Format SQL

on:
  workflow_call:

jobs:
  format:
    runs-on: ubuntu-slim
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v5
        with:
          ref: ${{ github.head_ref }}
      - uses: quarylabs/install-sqruff-cli-action@7805329baf7cf340e849e254cddc782f08f2d36c # master
      - name: Apply format
        continue-on-error: true
        run: sqruff fix . --format github-annotation-native
      - name: Check for changes
        id: diff
        run: git diff --quiet || echo "changed=true" >> $GITHUB_OUTPUT
        shell: bash
      - name: Commit changes
        if: steps.diff.outputs.changed == 'true'
        run: |
          git config --global user.name 'github-actions[bot]'
          git config --global user.email 'github-actions[bot]@users.noreply.github.com'
          git add *.sql
          git commit -m "Apply format SQL files"
          git push
        shell: bash

https://github.com/toms74209200/sqruff-github-actions-example/blob/24dd945b4ebd76cfea70517df9ca0b9f5786b92a/.github/workflows/job-format-sql.yml

セットアップ

GitHub Actions で sqruff を使うため sqruff 公式から quarylabs/install-sqruff-cli-action[3] という action が出ています. タグ管理されていないのでコミットハッシュをそのまま使っています... sqruff 自体がシングルバイナリなので直接インストールするのとあまり変わらない気もします...

Format 適用

sqruff fix で format します. ディレクトリは再帰的に検査されるようです. 出力形式として github-annotation-native を指定すると, 以下のように GitHub Actions の Annotations に追加されます. format 違反は修正されるため, 修正されたものが表示されます.

GitHub Actions のワークフローサマリー. Annotations に format 違反が出力される
GitHub Actions のワークフローサマリー. Annotations に format 違反が出力される.

GitHub Actions の Job 詳細. 標準出力がハイライトされている
GitHub Actions の Job 詳細. 標準出力がハイライトされている.

実はこの format コマンドで lint も行われており, lint 違反と format 違反が出力されることになります. そのため format を行う場合は lint も行う意味はありません.

ここで違反があると戻り値がエラー(0以外)となるため continue-on-error を有効にして後続の処理が行われるようにします[4].

変更チェック

git diff --quiet で戻り値によって変更の有無を確認します. GITHUB_OUTPUT に入れて次のステップで条件分岐します. shell の test コマンドを使えば 1ステップにまとめられますが, GitHub Actions ではこっちの方が実行の有無がわかりやすくなります.

- name: Check for changes
  id: diff
  run: git diff --quiet || echo "changed=true" >> $GITHUB_OUTPUT
  shell: bash
- name: Commit changes
  if: steps.diff.outputs.changed == 'true'

変更コミット&プッシュ

Format 結果をコミットします. 実行元のブランチにコミットするようにチェックアウトしておく必要があります.

- uses: actions/checkout@v5
  with:
    ref: ${{ github.head_ref }}
- name: Commit changes
  if: steps.diff.outputs.changed == 'true'
  run: |
    git config --global user.name 'github-actions[bot]'
    git config --global user.email 'github-actions[bot]@users.noreply.github.com'
    git add *.sql
    git commit -m "Apply format SQL files"
    git push
  shell: bash

Lint

Lint は format よりも簡単です.

.github/workflows/job-lint-sql.yml
name: Job - Lint SQL

on:
  workflow_call:

jobs:
  lint:
    runs-on: ubuntu-slim
    steps:
      - uses: actions/checkout@v5
        with:
          ref: ${{ github.head_ref }}
      - uses: quarylabs/install-sqruff-cli-action@7805329baf7cf340e849e254cddc782f08f2d36c # master
      - name: Run linter
        run: sqruff lint . --format github-annotation-native

https://github.com/toms74209200/sqruff-github-actions-example/blob/24dd945b4ebd76cfea70517df9ca0b9f5786b92a/.github/workflows/job-lint-sql.yml

sqruff lint でも github-annotation-native を指定できます.

GitHub Actions のワークフローサマリー. Annotations に format 違反が出力される
GitHub Actions のワークフローサマリー. Annotations に lint 違反が出力される.

GitHub Actions の Job 詳細. 標準出力がハイライトされている
GitHub Actions の Job 詳細. 標準出力がハイライトされている.


これで sqruff を自動化できました. Linter/formatter は開発の途中から導入するのは容易ではありません. 開発初期から導入し自動化していきましょう.

脚注
  1. quarylabs/sqruff: Fast SQL formatter/linter https://github.com/quarylabs/sqruff ↩︎

  2. SQLFluff https://www.sqlfluff.com/ ↩︎

  3. quarylabs/install-sqruff-cli-action: GitHub Action to install sqruff ↩︎

  4. https://docs.github.com/ja/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idstepscontinue-on-error ↩︎

Discussion