Skip to content

Commit 5f8b066

Browse files
committed
docs sample
1 parent 7a1d41d commit 5f8b066

21 files changed

+935
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title: 'Using Environment Variables in Components'
3+
sidebar_position: 100
4+
---
5+
6+
7+
import Preview from '@site/docs/partials/\_Preview.md';
8+
9+
<Preview />
10+
11+
12+
It's common to want to configure components depending on the environment in which they are run. `dg` and components make it easy to configure this behavior.
13+
14+
15+
## Setting up a components project with dbt
16+
17+
We start with an empty Dagster components project. Let's begin by pulling down a sample dbt project from GitHub:
18+
19+
<CodeExample path="docs_snippets/docs_snippets/guides/dg/using-env/1-jaffle-clone.txt" language="bash" />
20+
21+
Next, we'll install the `dagster-components[dbt]` package:
22+
23+
<CliInvocationExample contents="uv add 'dagster-components[dbt]' dbt-duckdb" />
24+
25+
To confirm that the `dagster_components.dbt_project` component type is now available, run `dg list component-type`:
26+
27+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/2-dg-list-component-types.txt" />
28+
29+
Next, we'll scaffold a new dbt component to represent the dbt project as assets:
30+
31+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/3-dg-scaffold-jdbt.txt" />
32+
33+
We can use `dg check yaml` to validate that the component is configured correctly:
34+
35+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/4-dg-component-check.txt" />
36+
37+
We can then generate the manifest, and take a look at the assets we've configured:
38+
39+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/5-dbt-parse.txt" />
40+
41+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/6-dg-list-defs.txt" />
42+
43+
## Configuring the component to use environment variables
44+
We'd like our asset keys to reflect the database and schema in which the tables are materialized. We can configure our `component.yaml` to do this, relying on environment variables to vary the schema based on our environment:
45+
46+
<CodeExample path="docs_snippets/docs_snippets/guides/dg/using-env/7-project-jdbt.yaml" language="yaml" />
47+
48+
If we run `dg check yaml`, we see that we need to encode our environment dependency in the component file:
49+
50+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/8-dg-component-check.txt" />
51+
52+
If we pass the `--fix-env-requirements` flag to `dg check yaml`, the component file will automatically be updated to include the environment variable:
53+
54+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/9-dg-component-check.txt" />
55+
56+
Now, we get an error that the environment variable is not set. We'll add it to our `.env` file:
57+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/10-inject-env.txt" />
58+
59+
Let's run `dg check yaml` once more, and we see that the component file is now valid:
60+
61+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/11-dg-component-check-fixed.txt" />
62+
63+
We can employ `dg env list` to see that our env var has been configured, and is being used by our dbt component:
64+
65+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/12-dg-env-list.txt" />
66+
67+
And we can see that the assets have been configured to use the proper schema:
68+
69+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/13-dg-list-defs.txt" />
70+
71+
## Using environment variables with Dagster Plus
72+
73+
We can use environment variables to configure secrets for Dagster Plus. Let's say we want to now deploy our dbt project to our production environment.
74+
75+
First, we'll authenticate with Dagster Plus:
76+
77+
<CliInvocationExample contents="dg plus login" />
78+
79+
If we run `dg env list`, we now see additional columns showing whether the environment variable is set for each location:
80+
81+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/14-dg-env-list.txt" />
82+
83+
We can use `dg plus env add` to configure the environment variable in Dagster Plus. First, we can push up our local environment variable to the `dev` scope, so that
84+
other developers can use it in their local environments:
85+
86+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/15-dg-plus-env-add.txt" />
87+
88+
If we run `dg env list` again, we see that the environment variable is now set for the `dev` scope:
89+
90+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/16-dg-env-list.txt" />
91+
92+
Finally, we can push up the environment variable to the `full` and `branch` scopes:
93+
94+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/17-dg-plus-env-add.txt" />
95+
96+
If we run `dg env list` again, we see that the environment variable is now set for all scopes:
97+
98+
<CliInvocationExample path="docs_snippets/docs_snippets/guides/dg/using-env/18-dg-env-list.txt" />
99+
100+
We can go ahead and deploy our project to production:
101+
102+
<CliInvocationExample contents="dg deploy" />

examples/docs_snippets/docs_snippets/guides/dg/using-env/10-dg-env-list.txt

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
echo 'DBT_SCHEMA=jaffle_shop' >> .env
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dg check yaml
2+
3+
All components validated successfully.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dg check yaml
2+
3+
All components validated successfully.

examples/docs_snippets/docs_snippets/guides/dg/using-env/12-dg-env-list.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
dg env list
22

3-
DagsterPlusCliConfig(url=None, organization=None, default_deployment=None, user_token=None, agent_timeout=None)
43
┏━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
54
┃ Env Var ┃ Local Value ┃ Components ┃
65
┡━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━┩

0 commit comments

Comments
 (0)