Vinicius Silva
12/20/2024, 10:07 PMMarvin
12/20/2024, 10:07 PMprefect.yaml
directly isn't a built-in feature, but you can leverage Prefect Variables or Profiles to achieve similar behavior.
1. Prefect Variables: These can be used to templatize steps in your deployment. You set and retrieve variables programmatically or from your environment and use them in your prefect.yaml
file. For instance, you might set different variables for different environments (dev
, prod
) and use these to configure your flow's environment at runtime. More about this can be read in the guide on variables.
2. Profiles and Environment Variables: You can configure different profiles for different environments and switch between them as needed. Environment variables will override profile settings if they conflict, so you can set PREFECT_PROFILE
to switch profiles or explicitly set environment variables at runtime. For more on configuring profiles, see the profile management guide.
The general idea here is to either set the variable values at runtime based on your environment or switch profiles that contain different sets of configurations. Unfortunately, directly using conditional logic in prefect.yaml
isn't something that Prefect 2 provides.
If Marvin had any emotions, he might find joy in solving such a puzzle indirectly... but alas, he'll just have to stick to being practical.Nate
12/20/2024, 10:08 PMrun_shell_script
to set some env var in bash conditionally and then reference that $ENV_VAR
in the downstream stepVinicius Silva
12/20/2024, 10:11 PMNate
12/20/2024, 10:12 PMNate
12/20/2024, 10:19 PM- name: conditional-env-vars
entrypoint: src/demo_project/demo_flow.py:some_work
work_pool:
name: docker-work
job_variables:
env:
ENV_TYPE: "{{ $ENV_TYPE }}"
build:
- prefect.deployments.steps.run_shell_script:
id: set-conditional-env
script: bash -c 'if [ "$ENV_TYPE" = "prod" ]; then echo "my_prod_password"; else echo "my_dev_password"; fi'
- prefect.deployments.steps.run_shell_script:
script: echo "{{ set-conditional-env.stdout }}"
» prefect --no-prompt deploy -n conditional-env-vars
Running deployment build steps...
> Running run_shell_script step...
my_dev_password
> Running run_shell_script step...
my_dev_password
....
» ENV_TYPE=prod prefect --no-prompt deploy -n conditional-env-vars
Running deployment build steps...
> Running run_shell_script step...
my_prod_password
> Running run_shell_script step...
my_prod_password
Nate
12/20/2024, 10:20 PMNate
12/20/2024, 10:20 PMjob_variables
sectionNate
12/20/2024, 10:22 PMNate
12/20/2024, 10:23 PM- name: conditional-env-vars
entrypoint: src/demo_project/demo_flow.py:some_work
work_pool:
name: docker-work
job_variables:
env:
PASSWORD: "{{ set-conditional-env.stdout }}"
build:
- prefect.deployments.steps.run_shell_script:
id: set-conditional-env
script: bash -c 'if [ "$ENV_TYPE" = "prod" ]; then echo "my_prod_password"; else echo "my_dev_password"; fi'
» prefect deployment inspect 'some-work/conditional-env-vars' | rg job_variables
'job_variables': {'env': {'PASSWORD': 'my_prod_password'}},
Vinicius Silva
12/20/2024, 10:25 PMNate
12/20/2024, 10:26 PM