Skip to content

Commit

Permalink
add templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Sung Won Chung committed Feb 5, 2024
1 parent f7ed853 commit 03f80b4
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/dbt_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: dbt deploy

on:
push:
branches:
- main

jobs:
run:
runs-on: ubuntu-20.04

steps:
- name: checkout
uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: "3.11"

- name: install requirements
run: pip install -q -r requirements.txt

- name: install datafold-sdk
run: pip install -q datafold-sdk

- name: dbt deps
run: dbt deps

- name: Grab production manifest from S3
run: |
aws s3 cp s3://datafold-dbt-prod-manifest/manifest.json ./manifest.json
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-west-2

- name: dbt build
run: dbt build --select state:modified+ --defer --state ./ --exclude config.materialized:snapshot --full-refresh --profiles-dir ./
env:
SNOWFLAKE_ACCOUNT: ${{ secrets.SNOWFLAKE_ACCOUNT }}
SNOWFLAKE_USER: ${{ secrets.SNOWFLAKE_USER }}
SNOWFLAKE_PASSWORD: ${{ secrets.SNOWFLAKE_PASSWORD }}
SNOWFLAKE_ROLE: ${{ secrets.SNOWFLAKE_ROLE }}
SNOWFLAKE_SCHEMA: "${{ 'ANALYTICS' }}"

- name: submit artifacts to datafold
run: |
set -ex
datafold dbt upload --ci-config-id 26 --run-type ${DATAFOLD_RUN_TYPE} --commit-sha ${GIT_SHA}
env:
DATAFOLD_APIKEY: ${{ secrets.DATAFOLD_APIKEY }}
DATAFOLD_RUN_TYPE: "${{ 'production' }}"
GIT_SHA: "${{ github.sha }}"

- name: Upload manifest to S3
run: |
aws s3 cp target/manifest.json s3://datafold-dbt-prod-manifest/manifest.json
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-west-2
66 changes: 66 additions & 0 deletions .github/workflows/dbt_staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: dbt staging

on:
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review
push:
branches:
- "!main"

jobs:
run:
runs-on: ubuntu-20.04
if: ${{ !github.event.pull_request.draft }}
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
steps:
- name: checkout
uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: "3.11"

- name: install requirements
run: pip install -q -r requirements.txt

- name: install datafold-sdk
run: pip install -q datafold-sdk

- name: dbt deps
run: dbt deps

- name: Find Current Pull Request
uses: jwalton/[email protected]
id: findPR

- name: Grab production manifest from S3
run: |
aws s3 cp s3://datafold-dbt-prod-manifest/manifest.json ./manifest.json
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-west-2

- name: dbt build
run: dbt build --select state:modified+ --defer --state ./ --exclude config.materialized:snapshot --profiles-dir ./
env:
SNOWFLAKE_ACCOUNT: ${{ secrets.SNOWFLAKE_ACCOUNT }}
SNOWFLAKE_USER: ${{ secrets.SNOWFLAKE_USER }}
SNOWFLAKE_PASSWORD: ${{ secrets.SNOWFLAKE_PASSWORD }}
SNOWFLAKE_ROLE: ${{ secrets.SNOWFLAKE_ROLE }}
SNOWFLAKE_SCHEMA: "${{ format('{0}_{1}', 'PR_NUM', steps.findPr.outputs.pr) }}"

- name: submit artifacts to datafold
run: |
set -ex
datafold dbt upload --ci-config-id 26 --run-type ${DATAFOLD_RUN_TYPE} --commit-sha ${GIT_SHA}
env:
DATAFOLD_APIKEY: ${{ secrets.DATAFOLD_APIKEY }}
DATAFOLD_RUN_TYPE: "${{ 'pull_request' }}"
GIT_SHA: "${{ github.event.pull_request.head.sha }}"
34 changes: 34 additions & 0 deletions .github/workflows/staging_teardown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: staging teardown on PR close

on:
pull_request:
types:
- closed

jobs:
run:
runs-on: ubuntu-20.04

steps:
- name: checkout
uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: "3.11"

- name: install requirements
run: pip install -q -r requirements.txt

- name: dbt deps
run: dbt deps

- name: drop PR schemas
run: dbt run-operation drop_pr_staging_schemas --args "{'PR_number'":" '${PR_NUM}' }" --profiles-dir ./
env:
SNOWFLAKE_ACCOUNT: ${{ secrets.SNOWFLAKE_ACCOUNT }}
SNOWFLAKE_USER: ${{ secrets.SNOWFLAKE_USER }}
SNOWFLAKE_PASSWORD: ${{ secrets.SNOWFLAKE_PASSWORD }}
SNOWFLAKE_ROLE: ${{ secrets.SNOWFLAKE_ROLE }}
SNOWFLAKE_SCHEMA: ""
PR_NUM: ${{ github.event.number }}
27 changes: 27 additions & 0 deletions macros/drop_pr_staging_schemas.sql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{%- macro drop_pr_staging_schemas(PR_number) %}

{% set pr_cleanup_query %}
with pr_staging_schemas as (
select schema_name
from analytics.information_schema.schemata
where
schema_name like 'PR_NUM_'||{{ PR_number }}||'%'
)

select
'drop schema analytics.'||schema_name||';' as drop_command
from pr_staging_schemas
{% endset %}

{% do log(pr_cleanup_query, info=TRUE) %}

{% set drop_commands = run_query(pr_cleanup_query).columns[0].values() %}

{% if drop_commands %}
{% for drop_command in drop_commands %}
{% do log(drop_command, True) %} {% do run_query(drop_command) %}
{% endfor %}
{% else %} {% do log('No schemas to drop.', True) %}
{% endif %}

{%- endmacro -%}

0 comments on commit 03f80b4

Please sign in to comment.