Skip to content

Instantly share code, notes, and snippets.

@irvingpop
Created March 18, 2025 21:07
Show Gist options
  • Save irvingpop/e3863b2e43a1a93fd0cb6d0c1ddd5a71 to your computer and use it in GitHub Desktop.
Save irvingpop/e3863b2e43a1a93fd0cb6d0c1ddd5a71 to your computer and use it in GitHub Desktop.
# Original source: https://github.com/sqlfluff/sqlfluff-github-actions/blob/main/menu_of_workflows/surfline/sqlfluff_lint_dbt_models.yml
name: SQLFluff lint dbt data models
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
sqlfluff-lint-models:
name: Lint dbt models using SQLFluff
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ['ubuntu-latest']
python-version: ['3.12']
steps:
- name: Checkout branch
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Install Python packages or restore cache
run: pip install -r requirements.txt
- name: Get all changed model SQL files
id: changed-files-models
run: |
# Define array of files to exclude
EXCLUDE_FILES=(
"models/utils/date_spine.sql"
)
# Define include pattern for SQL files
INCLUDE_PATTERN="models/.*\.sql$"
if [ "${{ github.event_name }}" == "pull_request" ]; then
# For pull requests, compare with the base branch
git fetch origin ${{ github.base_ref }}
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}..HEAD | grep -E "$INCLUDE_PATTERN" || true)
else
# For pushes, compare with the previous commit
CHANGED_FILES=$(git diff --name-only HEAD^ HEAD | grep -E "$INCLUDE_PATTERN" || true)
fi
# Filter out excluded files
for EXCLUDE_FILE in "${EXCLUDE_FILES[@]}"; do
CHANGED_FILES=$(echo "$CHANGED_FILES" | grep -v "$EXCLUDE_FILE" || true)
done
# Set outputs
echo "all_changed_files=${CHANGED_FILES}" >> $GITHUB_OUTPUT
if [ -n "$CHANGED_FILES" ]; then
echo "any_changed=true" >> $GITHUB_OUTPUT
else
echo "any_changed=false" >> $GITHUB_OUTPUT
fi
- name: Lint dbt models
if: steps.changed-files-models.outputs.any_changed == 'true'
shell: bash -l {0}
run: |
sqlfluff lint --format github-annotation-native --annotation-level failure ${{ steps.changed-files-models.outputs.all_changed_files }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment