|
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 | +``` |
0 commit comments