Update models and add tags #119
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Pull Request testing - Lint dbt models using SQLFluff | |
on: | |
pull_request: | |
paths: | |
- 'macros/**' | |
- 'models/**' | |
- 'integration_tests/**' | |
- 'dbt_project.yml' | |
- 'packages.yml' | |
- 'config.py' | |
workflow_dispatch: | |
# GitHub secrets | |
env: | |
DBT_PROFILES_DIR: /home/runner/work/${{ github.event.repository.name }}/${{ github.event.repository.name }}/integration_tests | |
GITHUB_SHA_OVERRIDE: ${{ github.event.pull_request.head.sha }} # We need the commit hash of the pull request branch's head, the GITHUB_SHA env var is always the base branch in a pull_request_target trigger | |
DBT_ENV_SECRET_BIGQUERY_TEST_SERVICE_ACCOUNT: ${{ secrets.DBT_ENV_SECRET_BIGQUERY_TEST_SERVICE_ACCOUNT }} | |
DBT_ENV_SECRET_BIGQUERY_TEST_STORAGE_PROJECT: ${{ secrets.DBT_ENV_SECRET_BIGQUERY_TEST_STORAGE_PROJECT }} | |
DBT_ENV_SECRET_BIGQUERY_TEST_EXECUTION_PROJECT: ${{ secrets.DBT_ENV_SECRET_BIGQUERY_TEST_EXECUTION_PROJECT }} | |
DBT_ENV_SECRET_BIGQUERY_TEST_LOCATION: ${{ secrets.DBT_ENV_SECRET_BIGQUERY_TEST_LOCATION }} | |
DBT_BQ_MONITORING_GCP_PROJECTS: ${{ secrets.DBT_BQ_MONITORING_GCP_PROJECTS }} | |
concurrency: | |
cancel-in-progress: true | |
group: pr-lint-models-${{ github.workflow }}-${{ github.ref }} | |
jobs: | |
sqlfluff-lint-models: | |
name: Lint dbt models using SQLFluff | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout branch | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.event.pull_request.head.sha }} # Check out the code of the PR | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.11' | |
- name: Install Python packages | |
run: python -m pip install dbt-bigquery~=1.10.0 sqlfluff-templater-dbt google-cloud-bigquery==3.31.0 | |
- name: Write keyfile if secret is defined | |
run: | | |
if [ -z "${{ secrets.DBT_ENV_SECRET_BIGQUERY_TEST_SERVICE_ACCOUNT }}" ]; then | |
echo "Error: DBT_ENV_SECRET_BIGQUERY_TEST_SERVICE_ACCOUNT is not defined." | |
else | |
echo ${{ secrets.DBT_ENV_SECRET_BIGQUERY_TEST_SERVICE_ACCOUNT }} | base64 -d > keyfile.json | |
fi | |
- name: Test database connection | |
run: dbt debug | |
- name: Install dbt packages | |
run: dbt deps | |
- name: Get changed files | |
id: get_file_changes | |
uses: trilom/file-changes-action@v1.2.4 | |
with: | |
output: ' ' | |
- name: Get new and changed .sql files in /models to lint | |
id: get_files_to_lint | |
shell: bash -l {0} | |
run: | | |
# Set the command in the $() brackets as an output to use in later steps | |
echo "::set-output name=lintees::$( | |
# Issue where grep regular expressions don't work as expected on the | |
# Github Actions shell, check dbt/models/ folder | |
echo \ | |
$(echo ${{ steps.get_file_changes.outputs.files_modified }} | | |
tr -s ' ' '\n' | | |
grep -E '^models.*[.]sql$' | | |
tr -s '\n' ' ') \ | |
$(echo ${{ steps.get_file_changes.outputs.files_added }} | | |
tr -s ' ' '\n' | | |
grep -E '^models.*[.]sql$' | | |
tr -s '\n' ' ') | |
)" | |
# ---- USEFUL FOR DEBUGGING profiles.yml and VPN config ---- | |
# - name: Expose workflow directory structure and CI/CD files | |
# # Show folder structue and that required files are present. | |
# # Mainly for sanity/debugging. | |
# run: | | |
# echo "Home directory (~):" | |
# echo ~ | |
# echo " " | |
# echo "Present working directory:" | |
# pwd | |
# echo " " | |
# echo "Present working directory contents:" | |
# ls -lh | |
# echo " " | |
# echo "Check /integration_tests folder contents for dummy profiles.yml" | |
# ls -lh ./integration_tests | |
# ---- USEFUL FOR DEBUGGING CONNECTION ISSUES ---- | |
# - name: Check dbt can connect to data warehouse and compile on its own. | |
# # Call dbt separately here to try and catch a more verbose version of | |
# # errors related to connection and profiles.yml issues since SQLFluff | |
# # returns very unclear errors when this occurs. | |
# shell: bash -l {0} | |
# run : | | |
# dbt compile | |
- name: Lint dbt models | |
if: steps.get_files_to_lint.outputs.lintees != '' | |
shell: bash -l {0} | |
run: | | |
sqlfluff lint --format github-annotation --annotation-level failure --nofail ${{ steps.get_files_to_lint.outputs.lintees }} > annotations.json | |
# This step forces the presence of an empty annotations file in the case | |
- name: Force pass because no SQL edited. | |
if: steps.get_files_to_lint.outputs.lintees == '' | |
shell: bash -l {0} | |
run: | | |
echo "[]" > annotations.json | |
- name: Output annotations for debugging | |
shell: bash -l {0} | |
run: | | |
cat annotations.json | |
- name: Annotate | |
uses: yuzutech/annotations-action@v0.5.0 | |
with: | |
repo-token: "${{ secrets.GITHUB_TOKEN }}" | |
title: "SQLFluff Lint" | |
input: "./annotations.json" |