Last active
January 11, 2024 03:22
-
-
Save juangesino/d0af5df24b70755acb40da7e29f78026 to your computer and use it in GitHub Desktop.
Running dbt using Gitlab CI/CD
This file contains 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
.dbt_run: &dbt_jobs | |
image: python:3.8.1-slim-buster | |
before_script: | |
- apt-get update | |
- apt-get dist-upgrade -y | |
- apt-get install -y --no-install-recommends git make ca-certificates libpq-dev libicu-dev pkg-config gcc build-essential | |
- pip install dbt-postgres==1.0.0 | |
- export CI_PROFILE_TARGET="--profiles-dir . --target ${TARGET_NAME}" | |
- echo $CI_PROFILE_TARGET |
This file contains 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
.deploy: | |
<<: *dbt_jobs | |
script: | |
- dbt deps $CI_PROFILE_TARGET | |
- dbt build $CI_PROFILE_TARGET |
This file contains 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
deploy-production: | |
extends: .deploy | |
stage: deploy-production | |
variables: | |
TARGET_NAME: postgres | |
when: manual |
This file contains 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
stages: | |
- deploy-production | |
.dbt_run: &dbt_jobs | |
image: python:3.8.1-slim-buster | |
before_script: | |
- apt-get update | |
- apt-get dist-upgrade -y | |
- apt-get install -y --no-install-recommends git make ca-certificates libpq-dev libicu-dev pkg-config gcc build-essential | |
- pip install dbt-postgres==1.0.0 | |
- export CI_PROFILE_TARGET="--profiles-dir . --target ${TARGET_NAME}" | |
- echo $CI_PROFILE_TARGET | |
.deploy: | |
<<: *dbt_jobs | |
script: | |
- dbt deps $CI_PROFILE_TARGET | |
- dbt build $CI_PROFILE_TARGET | |
deploy-production: | |
extends: .deploy | |
stage: deploy-production | |
variables: | |
TARGET_NAME: postgres | |
when: manual |
This file contains 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
# Running dbt using Gitlab CI/CD |
This file contains 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
config: | |
send_anonymous_usage_stats: False | |
use_colors: True | |
jaffle_shop: | |
target: postgres | |
outputs: | |
postgres: | |
type: postgres | |
host: "{{ env_var('DB_HOST') }}" | |
user: "{{ env_var('DB_USER') }}" | |
pass: "{{ env_var('DB_PASSWORD') }}" | |
port: "{{ env_var('DB_PORT') | int }}" | |
dbname: "{{ env_var('DB_NAME') }}" | |
schema: public | |
threads: 4 |
This file contains 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
import requests | |
GITLAB_PIPELINE_DBT = "XXXXXXXXXXXXXXXX" | |
PROJECT_ID = "XXXXXXXXX" | |
PIPELINE_ENDPOINT = f"https://gitlab.com/api/v4/projects/{PROJECT_ID}/trigger/pipeline" | |
BRANCH_NAME = "main" | |
payload = { | |
"token": GITLAB_PIPELINE_DBT, | |
"ref": branch, | |
"variables[EXAMPLE_VARIABLE]": "your_value_here", | |
} | |
response = requests.request("POST", PIPELINE_ENDPOINT, data=payload) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment