Skip to content

Commit c49e994

Browse files
authored
Merge pull request #134 from dbt-labs/import-with-template
Add ability to define field templates when importing jobs
2 parents 731eb8f + 7805d8e commit c49e994

19 files changed

Lines changed: 382 additions & 31 deletions

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ repos:
55
hooks:
66
# Run the linter.
77
- id: ruff
8-
args: [ --select, I, --select, B, --fix ]
8+
args: [ --fix ]
99
# Run the formatter.
1010
- id: ruff-format

docs/advanced_config/glob_config_files.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ Those patterns are also called "Unix style pathname pattern expansion", and in a
99
For example, to run the `plan` command on all the files stored in subdirectoris under the `jobs` directory, you can use the following command:
1010

1111
```bash
12-
dbt-jobs-as-code validate --config-file jobs/**/*.yml
13-
```
12+
dbt-jobs-as-code plan "jobs/**/*.yml" # (1)!
13+
```
14+
15+
1. Depending on your shell you might have to quote the pattern or not. For example, for `zsh` quoting is required as otherwise the shell will try to expand the pattern before passing it to the command.

docs/advanced_config/index.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
More advanced features of the tools that can be combined to match more complex requirements in regards to dbt Cloud jobs management.
1+
# Advanced configuration
22

3-
- [YAML Templating](templating.md)
4-
- [glob config files](glob_config_files.md)
5-
- [YAML anchors](yaml_anchors.md)
6-
- [Advanced jobs importing](jobs_importing.md)
3+
More advanced features of `dbt-jobs-as-code` can be combined to match complex requirements in regards to dbt Cloud jobs management.
4+
5+
- [YAML Templating](templating.md) - for using the same YAML file to update different dbt Cloud projects and/or environments
6+
- [glob config files](glob_config_files.md) - for using glob patterns to match config files at once
7+
- [YAML anchors](yaml_anchors.md) - to reuse the same parameters in different jobs
8+
- [Advanced jobs importing](jobs_importing.md) - for importing jobs from dbt Cloud to a YAML file
Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,98 @@
1-
# WIP
1+
# Advanced job importing
2+
3+
## Generating jobs with templated fields with `import-jobs`
4+
5+
`plan` and `sync` commands allow adding Jinja variables to the jobs in order to use the same YAML file for different environments (see [YAML templating](templating.md)).
6+
7+
While it is possible to import the jobs from dbt Cloud using the `import-jobs` command and to modify the outputted YAML by hand to add variables, there is also the ability to use a YAML file to specify variables for the different jobs.
8+
9+
By providing the `--templated-fields` parameter, it is possible to use a YAML file to specify the variables for specific fields of the jobs.
10+
11+
For example, the following YAML file:
12+
13+
```yaml title="templ.yml"
14+
environment_id: {{ environment_id }}
15+
deferring_environment_id: {{ deferring_environment_id }}
16+
triggers.schedule: {{ env_name == 'prod' }}
17+
```
18+
19+
will be used to set the `project_id`, `environment_id`, `deferring_environment_id` and `triggers.schedule` fields for all the jobs in the generated YAML file and can be called with
20+
21+
```bash
22+
dbt-jobs-as-code import-jobs --account-id 1234 --project-id 3213 --environment-id 423432 --templated-fields templ.yml --managed-only
23+
```
24+
25+
The outputted YAML will look like the following:
26+
27+
```yaml title="jobs.yml"
28+
# yaml-language-server: $schema=https://raw.githubusercontent.com/dbt-labs/dbt-jobs-as-code/main/src/dbt_jobs_as_code/schemas/load_job_schema.json
29+
30+
jobs:
31+
my-id:
32+
account_id: 1234
33+
project_id: 3213
34+
environment_id: {{ environment_id }}
35+
dbt_version:
36+
name: My job name created from the UI
37+
settings:
38+
threads: 10
39+
target_name: default
40+
execution:
41+
timeout_seconds: 100
42+
deferring_job_definition_id:
43+
deferring_environment_id: {{ deferring_environment_id }}
44+
run_generate_sources: true
45+
execute_steps:
46+
- dbt build
47+
generate_docs: true
48+
schedule:
49+
cron: 0 1,5 * * 0,1,2,3,4,5
50+
triggers:
51+
github_webhook: false
52+
git_provider_webhook: false
53+
schedule: {{ env_name == 'prod' }}
54+
on_merge: false
55+
description: ''
56+
run_compare_changes: false
57+
compare_changes_flags: --select state:modified
58+
job_type: other
59+
triggers_on_draft_pr: false
60+
job_completion_trigger_condition:
61+
custom_environment_variables: []
62+
```
63+
64+
65+
## Automatically promote jobs between environments
66+
67+
The import command from above can also be used to automatically promote jobs between environments. In that case, as part of a CI/CD process, or on a schedule, the following command can be used to generate the YAML content and save it to a file:
68+
69+
```bash
70+
dbt-jobs-as-code import-jobs --account-id 1234 --project-id 3213 --environment-id 423432 --templated-fields templ.yml --managed-only > jobs.yml
71+
```
72+
73+
It would then be possible to automate the creation of PRs whenever the `jobs.yml` file is updated, meaning that some jobs would have been updated in the dbt Cloud UI. The GitHub action [Create Pull Request](https://github.com/marketplace/actions/create-pull-request) could be used to implement this flow.
74+
75+
76+
Then, the `jobs.yml` file can be used to import the jobs in a different environment with the following command, like described in [YAML templating](templating.md) and in the [typical flows](../typical_flows.md) page :
77+
78+
```bash
79+
dbt-jobs-as-code plan jobs.yml -v qa_vars.yml --limit-projects-envs-to-yml
80+
```
81+
82+
With `qa_vars.yml` being the YAML file containing the variables for the QA/staging environment.
83+
84+
```yaml title="qa_vars.yml"
85+
env_name: "qa"
86+
environment_id: 456
87+
deferring_environment_id: # (1)!
88+
```
89+
90+
1. The `deferring_environment_id` here is set to the null value, we could also set it to a specific ID
91+
92+
And `prod_vars.yml` being the YAML file containing the variables for the Prod environment.
93+
94+
```yaml title="prod_vars.yml"
95+
env_name: "prod"
96+
environment_id: 789
97+
deferring_environment_id:
98+
```

docs/advanced_config/templating.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ To add templated values to your jobs YAML file:
1010

1111
The file called in `--vars-yml` needs to be a valid YAML file like the following:
1212

13-
```yaml
13+
```yaml title="vars_qa.yml"
1414
project_id: 123
1515
environment_id: 456
1616
```
@@ -102,7 +102,7 @@ Templating also allows people to version control those YAML files and to have di
102102
deferring_environment_id: 789
103103
```
104104

105-
``` yaml title="vars_prd.yml"
105+
``` yaml title="vars_prod.yml"
106106
env_type: prod
107107
project_id: 12
108108
environment_id: 231231

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
To see the details of all changes, head to the GitHub repo
33

4+
### 1.4
5+
6+
- Add `--templated-fields` to `import-jobs` to add Jinja variables to the generated YAML file. This can be useful to allow users to maintain jobs in the dbt Cloud UI and set a process to automatically promote those to other environments.
7+
48
### 1.3
59

610
- Add this docs site

docs/typical_flows.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Typical flows (WIP)
1+
# Typical flows
22

33
This page descibes how `dbt-jobs-as-code` can be used in different scenarios, what commands to run in what order, what parameters to use, what CI actions to create etc...
44

@@ -282,8 +282,28 @@ sequenceDiagram
282282

283283
---
284284

285-
## Adanced flows for automation of jobs promotion
285+
## Advanced flows
286286

287-
### **WIP -- Not implemented yet** // Replicate all jobs from one environment to another (ongoing managed jobs)
287+
### Automated promotion of jobs created in the UI to other environments
288288

289-
WIP
289+
In this mechanism, we use `dbt-jobs-as-code` to create a CI/CD pipeline to automatically promote jobs created in the dbt Cloud UI to other environments. It would synchronize jobs and allow creating new ones, deleting old ones and modifying existing ones.
290+
291+
The key part of the process is [the advanced ability to import jobs](advanced_config/jobs_importing.md) while setting Jinja variables that will be rendered differently for each environment.
292+
293+
```mermaid
294+
sequenceDiagram
295+
actor D as dbt Developer
296+
participant C as dbt Cloud
297+
participant G as git repo
298+
actor U as User
299+
300+
D -->>C: Maintain dbt Cloud jobs in the UI in dev,<br> adding [[..]] to the jobs that<br> need to be promoted/replicated
301+
U -->>G: Create and commit the YAML vars files for each env
302+
U -->>G: Create and commit the YAML vars files for the templated values
303+
U -->>G: Create a pipeline to automatically run `dbt-jbos-as-code<br>on a schedule or triggered by somone
304+
G ->> C: run `import-jobs` with `--templated-fields templ.yml`<br>,`--managed-only` and `-e` to specify the env
305+
C ->> G: get yaml file
306+
Note over G: Check difference with existing file<br> and auto-create PR if it is different
307+
Note right of G: When the PR is created, we follow the<br> typical flow of GH actions<br> with plan/sync on PR creation/merge
308+
309+
```

mkdocs.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ nav:
88
- Home: index.md
99
- Getting Started: getting_started.md
1010
- Advanced configuration:
11-
- Index: advanced_config/index.md
11+
- advanced_config/index.md
1212
- YAML Templating: advanced_config/templating.md
1313
- glob config files: advanced_config/glob_config_files.md
1414
- Using YAML anchors: advanced_config/yaml_anchors.md
@@ -54,7 +54,10 @@ theme:
5454
- content.action.edit
5555
- navigation.tabs
5656
- toc.integrate # not sure, let's see
57+
- toc.follow
5758
- content.code.copy
59+
- content.code.annotate
60+
- navigation.indexes
5861

5962
markdown_extensions:
6063
- mkdocs-click

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ markers = [
7171

7272
[tool.ruff]
7373
line-length = 99
74+
75+
[tool.ruff.lint]
76+
select = ["E", "F", "I"]
77+
ignore = ["E501"] # line-length, need to fix those by hands first
78+
typing-modules = ["beartype.typing"]

src/dbt_jobs_as_code/client/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from dbt_jobs_as_code.schemas.job import JobDefinition, JobMissingFields
1414

1515
if os.getenv("DBT_JOB_ID", "") == "":
16-
VERSION = f'v{version("dbt-jobs-as-code")}'
16+
VERSION = f"v{version('dbt-jobs-as-code')}"
1717
else:
1818
VERSION = "dev"
1919

@@ -147,7 +147,7 @@ def get_job(self, job_id: int) -> JobDefinition:
147147
self._check_for_creds()
148148

149149
response = self._session.get(
150-
url=(f"{self.base_url}/api/v2/accounts/" f"{self.account_id}/jobs/{job_id}/"),
150+
url=(f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/{job_id}/"),
151151
headers=self._headers,
152152
verify=self._verify,
153153
)
@@ -162,7 +162,7 @@ def get_job_missing_fields(self, job_id: int) -> Optional[JobMissingFields]:
162162
self._check_for_creds()
163163

164164
response = self._session.get(
165-
url=(f"{self.base_url}/api/v2/accounts/" f"{self.account_id}/jobs/{job_id}/"),
165+
url=(f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/{job_id}/"),
166166
headers=self._headers,
167167
verify=self._verify,
168168
)

0 commit comments

Comments
 (0)