Skip to content

Commit 07524bb

Browse files
authored
Merge pull request #80 from dbt-labs/feature/non-zero-return-code-on-sync
2 parents ebaf392 + 78e46d7 commit 07524bb

9 files changed

Lines changed: 35 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ With this package's approach, people don't need to learn another tool and can co
3131
### Installation
3232

3333
- Create a Python virtual environment and activate it
34-
- Run `pip install dbt-jobs-as-code` (or `pip install dbt-jobs-as-code~=0.6.0` to install a specific release and its patches)
34+
- Run `pip install dbt-jobs-as-code` (or `pip install dbt-jobs-as-code~=0.7` to install a specific release and its patches)
3535

3636
The CLI is now available as `dbt-jobs-as-code`
3737

example_cicd/prod_plan_on_pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
python-version: "3.12.x"
2222

2323
- name: Install dbt-jobs-as-code
24-
run: pip install dbt-jobs-as-code~=0.6.0
24+
run: pip install dbt-jobs-as-code~=0.7.0
2525

2626
- name: Run dbt-jobs-as-code
2727
run: dbt-jobs-as-code plan jobs/jobs.yml --vars-yml jobs/vars_prod.yml --limit-projects-envs-to-yml

example_cicd/prod_sync_on_merge.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
python-version: "3.12.x"
2525

2626
- name: Install dbt-jobs-as-code
27-
run: pip install dbt-jobs-as-code~=0.6.0
27+
run: pip install dbt-jobs-as-code~=0.7.0
2828

2929
- name: Run dbt-jobs-as-code
3030
run: dbt-jobs-as-code sync jobs/jobs.yml --vars-yml jobs/vars_prod.yml --limit-projects-envs-to-yml

example_cicd/qa_plan_on_pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
python-version: "3.12.x"
2222

2323
- name: Install dbt-jobs-as-code
24-
run: pip install dbt-jobs-as-code~=0.6.0
24+
run: pip install dbt-jobs-as-code~=0.7.0
2525

2626
- name: Run dbt-jobs-as-code
2727
run: dbt-jobs-as-code plan jobs/jobs.yml --vars-yml jobs/vars_qa.yml --limit-projects-envs-to-yml

example_cicd/qa_sync_on_merge.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
python-version: "3.12.x"
2525

2626
- name: Install dbt-jobs-as-code
27-
run: pip install dbt-jobs-as-code~=0.6.0
27+
run: pip install dbt-jobs-as-code~=0.7.0
2828

2929
- name: Run dbt-jobs-as-code
3030
run: dbt-jobs-as-code sync jobs/jobs.yml --vars-yml jobs/vars_qa.yml --limit-projects-envs-to-yml

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "dbt-jobs-as-code"
3-
version = "0.6.1"
3+
version = "0.7.0"
44
description = "A CLI to allow defining dbt Cloud jobs as code"
55
authors = ["dbt Labs <info@dbtlabs.com>"]
66
license = "Apache License 2.0"

src/changeset/change_set.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pydantic import BaseModel, RootModel
99
from rich.table import Table
1010

11-
from src.client import DBTCloud
11+
from src.client import DBTCloud, DBTCloudException
1212
from src.loader.load import load_job_configuration
1313
from src.schemas import check_env_var_same, check_job_mapping_same
1414
from src.schemas.job import JobDefinition
@@ -35,10 +35,11 @@ def apply(self):
3535
self.sync_function(**self.parameters)
3636

3737

38-
class ChangeSet(RootModel):
38+
class ChangeSet(BaseModel):
3939
"""Store the set of changes to be displayed or applied."""
4040

4141
root: List[Change] = []
42+
apply_success: bool = True
4243

4344
def __iter__(self):
4445
return iter(self.root)
@@ -77,7 +78,10 @@ def __len__(self):
7778

7879
def apply(self):
7980
for change in self.root:
80-
change.apply()
81+
try:
82+
change.apply()
83+
except DBTCloudException:
84+
self.apply_success = False
8185

8286

8387
# Don't bear type this function as we do some odd things in tests

src/client/__init__.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
VERSION = "dev"
1919

2020

21+
class DBTCloudException(Exception):
22+
pass
23+
24+
25+
class DBTCloudParamsException(Exception):
26+
pass
27+
28+
2129
class DBTCloud:
2230
"""A minimalistic API client for fetching dbt Cloud data."""
2331

@@ -56,10 +64,10 @@ def _clear_env_var_cache(self, job_definition_id: Optional[int]) -> None:
5664
def _check_for_creds(self):
5765
"""Confirm the presence of credentials"""
5866
if not self._api_key:
59-
raise Exception("An API key is required to get dbt Cloud jobs.")
67+
raise DBTCloudParamsException("An API key is required to get dbt Cloud jobs.")
6068

6169
if not self.account_id:
62-
raise Exception("An account_id is required to get dbt Cloud jobs.")
70+
raise DBTCloudParamsException("An account_id is required to get dbt Cloud jobs.")
6371

6472
def build_mapping_job_identifier_job_id(
6573
self, cloud_jobs: Optional[List[JobDefinition]] = None
@@ -89,6 +97,7 @@ def update_job(self, job: JobDefinition) -> JobDefinition:
8997

9098
if response.status_code >= 400:
9199
logger.error(response.json())
100+
raise DBTCloudException(f"Error updating job {job.name}")
92101
else:
93102
logger.success("Job updated successfully.")
94103

@@ -108,6 +117,7 @@ def create_job(self, job: JobDefinition) -> Optional[JobDefinition]:
108117

109118
if response.status_code >= 400:
110119
logger.error(response.json())
120+
raise DBTCloudException(f"Error creating job {job.name}")
111121
return None
112122
else:
113123
logger.success("Job created successfully.")
@@ -127,6 +137,7 @@ def delete_job(self, job: JobDefinition) -> None:
127137

128138
if response.status_code >= 400:
129139
logger.error(response.json())
140+
raise DBTCloudException(f"Error deleting job {job.name}")
130141
else:
131142
logger.success("Job deleted successfully.")
132143

@@ -142,7 +153,7 @@ def get_job(self, job_id: int) -> Optional[JobDefinition]:
142153
)
143154
if response.status_code > 200:
144155
logger.error(f"Issue getting the job {job_id}")
145-
return None
156+
raise DBTCloudException(f"Error getting the job {job_id}")
146157
return JobDefinition(**response.json()["data"])
147158

148159
def get_job_missing_fields(self, job_id: int) -> Optional[JobMissingFields]:
@@ -157,7 +168,7 @@ def get_job_missing_fields(self, job_id: int) -> Optional[JobMissingFields]:
157168
)
158169
if response.status_code > 200:
159170
logger.error(f"Issue getting the job {job_id}")
160-
return None
171+
raise DBTCloudException(f"Error getting the job {job_id}")
161172
return JobMissingFields(**response.json()["data"])
162173

163174
def get_jobs(
@@ -285,6 +296,7 @@ def create_env_var(
285296

286297
if response.status_code >= 400:
287298
logger.error(response.json())
299+
raise DBTCloudException(f"Error creating the env var {env_var.name}")
288300

289301
# If the new environment variables has a job_definition_id, then clear the EnvVar cache.
290302
self._clear_env_var_cache(job_definition_id=env_var.job_definition_id)
@@ -329,6 +341,7 @@ def update_env_var(
329341

330342
if response.status_code >= 400:
331343
logger.error(response.json())
344+
raise DBTCloudException(f"Error updating the env var {custom_env_var.name}")
332345

333346
self._clear_env_var_cache(job_definition_id=payload.job_definition_id)
334347

@@ -348,6 +361,7 @@ def delete_env_var(self, project_id: int, env_var_id: int) -> None:
348361

349362
if response.status_code >= 400:
350363
logger.error(response.json())
364+
raise DBTCloudException(f"Error deleting the env var {env_var_id}")
351365

352366
logger.success("Env Var Job Overwrite deleted successfully.")
353367

src/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ def sync(
108108
console.log(change_set.to_table())
109109
change_set.apply()
110110

111+
if not change_set.apply_success:
112+
logger.error("-- SYNC -- There were some errors during the sync. Check the logs.")
113+
sys.exit(1)
114+
111115

112116
@cli.command()
113117
@option_disable_ssl_verification

0 commit comments

Comments
 (0)