Victor Rosario Núñez
10/15/2024, 3:01 PMMarvin
10/15/2024, 3:01 PMVictor Rosario Núñez
10/15/2024, 3:01 PMMarvin
10/15/2024, 3:02 PMbaseJobTemplate
by using the job_variables
section in your deployment's configuration file, such as prefect.yaml
. Here's how you can set it up:
1. Define the Environment Variables: Use the job_variables
section to specify the environment variables you want to set. This is done in the prefect.yaml
file.
2. Syntax Example:
yaml
deployments:
- name: demo-deployment
entrypoint: demo_project/demo_flow.py:some_work
work_pool:
name: local
job_variables:
env:
EXECUTION_ENVIRONMENT: staging
MY_NOT_SO_SECRET_CONFIG: plumbus
schedule: null
In the example above:
- EXECUTION_ENVIRONMENT
and MY_NOT_SO_SECRET_CONFIG
are the environment variables you're setting.
- These variables will be available at runtime for your flow runs under the specified work pool.
Additionally, if you want to utilize existing environment variables from your local environment, you can use templating with the {{ $ENV_VAR_NAME }}
syntax in the `prefect.yaml`:
yaml
deployments:
- name: demo-deployment
entrypoint: demo_project/demo_flow.py:some_work
work_pool:
name: local
job_variables:
env:
MY_ENV_VAR: "{{ $MY_ENV_VAR }}"
schedule: null
For more details, you can refer to the Prefect 3.x documentation on deploying and customizing work pools.Victor Rosario Núñez
10/15/2024, 3:02 PMMarvin
10/15/2024, 3:03 PMbaseJobTemplate.json
for Prefect 3.x, you can set environment variables in the job_configuration
section using placeholders for the variables you define in the variables
section. Here’s how you can set it up:
1. Define the Variables Section: This section defines the environment variables you want to set and makes them available for use within the job_configuration
section. The variables can include default values or constraints.
2. Job Configuration Section: In this section, you can reference the environment variables by using placeholders, which will be replaced with the corresponding variable values you provided.
Here's a simplified example of how you might structure the baseJobTemplate.json
to include environment variables:
json
{
"variables": {
"env": {
"type": "object",
"properties": {
"EXECUTION_ENVIRONMENT": {
"type": "string"
},
"MY_NOT_SO_SECRET_CONFIG": {
"type": "string"
}
},
"required": ["EXECUTION_ENVIRONMENT", "MY_NOT_SO_SECRET_CONFIG"]
}
},
"job_configuration": {
"command": "echo Running with environment: {{env.EXECUTION_ENVIRONMENT}}, config: {{env.MY_NOT_SO_SECRET_CONFIG}}",
"environment": {
"EXECUTION_ENVIRONMENT": "{{env.EXECUTION_ENVIRONMENT}}",
"MY_NOT_SO_SECRET_CONFIG": "{{env.MY_NOT_SO_SECRET_CONFIG}}"
}
}
}
In this example:
- The variables
section specifies an env
object with properties for EXECUTION_ENVIRONMENT
and MY_NOT_SO_SECRET_CONFIG
.
- The job_configuration
section then uses these variables as placeholders to set the environment for the job execution.
This setup allows you to pass custom environment variables to your jobs, ensuring the worker environment is configured correctly when running flows. For more specific details, you may want to check the Prefect 3.x documentation on work pools and base job templates.Nate
10/15/2024, 3:17 PMVictor Rosario Núñez
10/16/2024, 7:15 AM