Skip to content

Add CLI interface for managing Dagster Cloud variables#28

Closed
stevenayers wants to merge 1 commit intodagster-io:mainfrom
stevenayers:env-vars
Closed

Add CLI interface for managing Dagster Cloud variables#28
stevenayers wants to merge 1 commit intodagster-io:mainfrom
stevenayers:env-vars

Conversation

@stevenayers
Copy link

@stevenayers stevenayers commented Mar 6, 2025

Add CLI interface for managing Dagster Cloud variables

General

% dagster-cloud variable -h       
                                                                                                                                        
 Usage: dagster-cloud variable [OPTIONS] COMMAND [ARGS]...                                                                              
                                                                                                                                        
 Commands for managing Dagster Cloud variables.                                                                                         
                                                                                                                                        
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --help  -h        Show this message and exit.                                                                                        │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ create      Create a new variable.                                                                                                   │
│ delete      Delete a variable.                                                                                                       │
│ describe    Get details about a specific variable.                                                                                   │
│ get-id      Get only the ID of a specific variable, useful for update and delete operations.                                         │
│ get-value   Get only the value of a specific variable, useful for scripting.                                                         │
│ list        List all variables in your deployment.                                                                                   │
│ update      Update an existing variable.                                                                                             │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Usage Notes

As you can have variables with the same name but different locations, scopes and deployments:

  • GET operations (list, get-id) support filtering
  • CRUD operations on specific resources (describe, get-value, update, delete) use the unique ID

This avoids having to use complex filtering for CRUD operations which are prone to human error.

Example:

# List all variables and their scopes, locations and IDs. Does not display variable value.
dagster-cloud variable list 

ID                               │ NAME              │ LOCATIONS          │ SCOPES                  │ UPDATED            
─────────────────────────────────┼───────────────────┼────────────────────┼─────────────────────────┼────────────────────
524b4f2cc23249cda80e5293b75221e1 │ FOO_ENV_ID        │ cookie_monster     │ full                    │ 2025-02-11 09:59:53
8cc70159777c42b796909217af757c21 │ FOO_ENV_ID        │ cookie_monster     │ branch-all, local       │ 2025-02-11 11:50:15
3585634018bd46fe84cf9518a6aeae76 │ FOO_CLIENT_SECRET │ cookie_monster     │ full                    │ 2025-02-11 11:44:26
81b96e6df3184573b28fc7015b7161c8 │ FOO_CLIENT_ID     │ cookie_monster     │ full                    │ 2025-02-11 11:49:34
e93b8b9162ac483486cdf9a8cde1160a │ DB_PASSWORD       │ cookie_monster     │ branch-all, local       │ 2025-02-11 11:49:43
01b4bf89d7eb447fae8c2aebef5473ed │ DB_PASSWORD       │ cookie_monster     │ full                    │ 2025-02-11 11:49:55
a33330cfca2645bd9cc2cf854a58f969 │ BAR_ENV_ID        │ cookie_monster     │ branch-all              │ 2025-02-11 11:50:03
bb935239147a4f10ac5325615b9d5cc7 │ FOO_CLIENT_SECRET │ cookie_monster     │ branch-all, local       │ 2025-02-11 11:50:26
3fb9f3961f964783b6bdb47d88780536 │ FOO_CLIENT_ID     │ cookie_monster     │ branch-all, local       │ 2025-02-11 11:50:35
1cdb9a41297e4e6b8fa9bc0b19fa132d │ DB_PASSWORD       │ code_loc_jaffle    │ full, branch-all        │ 2025-02-11 11:51:58
4ba1ab3c86614cc1b4ed8eece83b2700 │ BAR_ENV_ID        │ cookie_monster     │ full                    │ 2025-02-11 09:59:44
f1871fa06cef443384621b31aec88443 │ DB_SCHEMA         │ cookie_monster     │ local                   │ 2025-02-16 14:58:38

# List variables, filtering by scope, location, etc
dagster-cloud  variable list --all-branch-deployments

# Supports table output by default, you can set json output with --output json
dagster-cloud variable list --output json 

# Find a specific variable's ID
ID=$(dagster-cloud variable get-id FOO_ENV_ID --full-deployment --location cookie_monster)

# Then work with the variable by ID
dagster-cloud variable describe $ID --show-value
dagster-cloud variable get-value $ID
dagster-cloud variable update $ID --value "new_value"
dagster-cloud variable delete $ID

@danielgafni
Copy link

Amazing! May I ask if there is going to be an idempotent "create-or-update" operation?

@stevenayers
Copy link
Author

stevenayers commented Mar 12, 2025

Amazing! May I ask if there is going to be an idempotent "create-or-update" operation?

Hi @danielgafni! Because you can have multiple variables with the same name, and there can be many depending on the scopes, locations and deployments set on each one, this means that there is no user-friendly unique identifier for a variable.

This means that creating a create-or-update operation out of the box would be tricky and potentially quite inflexible. You would need to specify the exact combination of name and deployment,scopes and locations your variable is associated with, while also specifying the new ones. That's a lot of parameters!

My advice would be use the CLI wrapped in a bit of shell script conditional logic if you want to perform a create or update. These CLI commands should provide you with all the building blocks you need to do so :)

ID=$(dagster-cloud variable get-id FOO_ENV_ID --full-deployment --location cookie_monster)
if [ $? -ne 0 ]; then
    # if we failed to get the ID, create a new variable
    dagster-cloud variable create FOO_ENV_ID --full-deployment --location cookie_monster --value BAR
else
    # otherwise update the existing one
    dagster-cloud variable update $ID --value "new_value"
fi

@danielgafni
Copy link

I'm mostly thinking about CI/CD pipelines. In this context the deployment name and the code location name is already known and used as arguments (or environment variables) for other dagster-cloud commands, so I would argue there is no additional complexity in this case.

It would be convenient if there was a straightforward way to just run one dagster-cloud command in this pipeline instead of making a wrapper. This workflow is very common so I think we would save quite some keystrokes for our users (combined) this way.

@stevenayers
Copy link
Author

I'm mostly thinking about CI/CD pipelines. In this context the deployment name and the code location name is already known and used as arguments (or environment variables) for other dagster-cloud commands, so I would argue there is no additional complexity in this case.

It would be convenient if there was a straightforward way to just run one dagster-cloud command in this pipeline instead of making a wrapper. This workflow is very common so I think we would save quite some keystrokes for our users (combined) this way.

That does make sense, I'm just not sure how best to implement it considering how variables are implemented in the graphql API.

Would you be willing to take a look at the code in the PR so far and share a suggestion on the logic?

@JuanEcheagaray75
Copy link

Was this feature ever added?

I have the same use case, tried using the new dg cli as documented here, but it requires browser authentication, not suited for CI/CD environments

@danielgafni
Copy link

Looks like there is a user_token option in the user config file. Not sure if it accepts it via a env variable tho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants