【Github】Github Actions ~ SQL Linter ~

■ はじめに

https://dk521123.hatenablog.com/entry/2024/02/28/225002
https://dk521123.hatenablog.com/entry/2024/03/01/163922

で、SQL Linter を扱ったが
今回は、これらを、Github Actions で実行させる。

目次

【1】ドキュメント
 1)SQLFluff 
 2)sqlfmt
【2】サンプル
 例1:SQLFluff での修正およびGithubコミット
 例2:sqlfmt でフォーマットチェック
【3】Tips
 1)continue-on-error
 2)自動コミット

【1】ドキュメント

1)SQLFluff

https://docs.sqlfluff.com/en/stable/production.html

2)sqlfmt

https://docs.sqlfmt.com/integrations/github-actions

【2】サンプル

例1:SQLFluff での修正およびGithubコミット

.github/workflows/demo_linter.yml

name: Run SQL linter

on:
  workflow_dispatch:

jobs:
  sample-job:
    runs-on: ubuntu-latest
    steps:
      - name: Run checkout
        uses: actions/checkout@v4
      - name: Setup Python
        uses: 'actions/setup-python@v2' 
        with:
            python-version: '3.10'
      - name: install SQLFluff
        run: 'pip install sqlfluff'
      - name: format
        run: |
          for file in 'sqls/*.sql' 
          do
            sqlfluff fix $file --rules L001,L002,L003,L004,L005,L006,L010,L011,L012,L017,L018,L019,L022,L023 --dialect snowflake
          done
      # ★「【3】Tips」の「2)自動コミット」を参照
      - name: commit
        uses: stefanzweifel/git-auto-commit-action@v3.0.0
        with:
          commit_message: run sqlfluff

sql/test.sql

-- This is a test SQL.
                    SELECT
    * FROM    
    sample_table AS t1
;

[Github Workflow実行後] sql/test.sql

-- This is a test SQL.
SELECT
    * FROM
    sample_table AS t1
;

例2:sqlfmt でフォーマットチェック

run_demo_sqlfmt.yaml

name: Run SQL linter with sqlfmt

on:
  workflow_dispatch:

jobs:
  sample-job:
    runs-on: ubuntu-latest
    steps:
      - name: Run checkout
        uses: actions/checkout@v4
      - name: Setup Python
        uses: 'actions/setup-python@v2' 
        with:
            python-version: '3.10'
      - name: install sqlfmt
        run: "pip install 'shandy-sqlfmt[jinjafmt]'"
      - name: format
        run: |
          sqlfmt --diff sqls

【3】Tips

1)continue-on-error

* エラーでも処理を継続したい場合、
 「continue-on-error: true」を指定する
 => 詳細は、以下の関連記事を参照のこと

Github Actions ~ エラー処理 / continue-on-error ~
https://dk521123.hatenablog.com/entry/2024/01/01/232057

2)自動コミット

* 以下の「stefanzweifel/git-auto-commit-actionアクション」で、
 変更したファイルを自動コミットできる

https://github.com/stefanzweifel/git-auto-commit-action
https://github.com/marketplace/actions/git-auto-commit

参考文献

https://qiita.com/k_0120/items/a25de096352c95dd70d6
https://techblog.kazaneya.com/20230417-sqlfluff-dummy-bigquery/

関連記事

Github ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/07/18/234652
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 ~ エラー処理 / continue-on-error ~
https://dk521123.hatenablog.com/entry/2024/01/01/232057
Github Actions ~ workflow_run / workflow_call ~
https://dk521123.hatenablog.com/entry/2024/02/14/232546
Github Actions ~ pull_request / pull_request_target ~
https://dk521123.hatenablog.com/entry/2024/04/10/152101
Github Actions ~ Pythonを使うには ~
https://dk521123.hatenablog.com/entry/2024/02/04/011205
Github Actions ~ サンプル集 ~
https://dk521123.hatenablog.com/entry/2023/12/31/231438
Github Actions ~ Python関連 ~
https://dk521123.hatenablog.com/entry/2022/06/21/143624
SQL Linter ~ SQLFluff ~
https://dk521123.hatenablog.com/entry/2024/02/28/225002
SQL Linter ~ sqlfmt ~
https://dk521123.hatenablog.com/entry/2024/03/01/163922
GitHub CLI ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/02/17/233836
パッケージ管理 ~ pip ~
https://dk521123.hatenablog.com/entry/2021/07/02/000000
オフライン環境下で pip install するには
https://dk521123.hatenablog.com/entry/2021/07/10/164833
lnコマンド / update-alternativesコマンド
https://dk521123.hatenablog.com/entry/2024/02/25/233428