Hi <@ULVA73B9P> apologies if this has been answere...
# marvin-ai
p
Hi @Marvin apologies if this has been answered already. I would like to dynamically set the
schedule
parameter of deployments depending on what environment I'm in -- ideally it would not be set for dev, stage, etc - and would be the actual
cron
schedule in prod. Here's a snippet of a deployment I have:
Copy code
schedule:
      cron: "0 7 * * *"
      timezone: "America/Denver"
What would be the best way to set the schedule dynamically based on environment?
m
thought for 460.5 seconds
Great question — totally common need. Assuming Prefect 3.x, here are a few reliable ways to make schedules environment-specific. Pick the one that best matches how you deploy today. Option A: Programmatic deploy with an env-driven schedule (simple and explicit) - Drive the schedule from an environment variable and pass it to
deploy()
.
Copy code
import os
from prefect import schedules

ENV = os.getenv("ENVIRONMENT", "dev")

schedule = None
if ENV == "prod":
    schedule = schedules.Cron("0 7 * * *", timezone="America/Denver")

(
    # If you’re deploying from source (recommended)
    flow.from_source(
        source="<https://github.com/your-org/your-repo>",
        entrypoint="path/to/flow.py:my_flow",
    )
    .deploy(
        name=f"my-flow-{ENV}",
        work_pool_name="my-pool",
        schedule=schedule,  # None for dev/stage, cron for prod
    )
)
Option B: Keep YAML schedule-free and add the schedule only in prod via CLI - Define the deployment without a
schedule:
in your
prefect.yaml
. - In prod CI, add the schedule after deployment:
Copy code
# Deploy (no schedule defined in YAML)
prefect deploy

# In prod only, attach a cron schedule
prefect deployment schedule create "my-flow/my-deployment" \
  --cron "0 7 * * *" \
  --timezone "America/Denver"
You can also pause/resume later:
Copy code
prefect deployment schedule pause "my-flow/my-deployment"
prefect deployment schedule resume "my-flow/my-deployment"
Option C: Separate deployment definitions per environment - Maintain separate deployment entries (e.g.,
my-deployment-dev
,
my-deployment-prod
) and only put the
schedule:
on the prod one. - In CI, select which to deploy by name pattern:
Copy code
prefect deploy --name "*prod*"
# or
prefect deploy --name "*dev*"
Notes - “No schedule” just means omit the
schedule:
block (or set it to null). You can still trigger ad-hoc runs via the UI/CLI. - If you already have a YAML snippet like:
Copy code
schedule:
    cron: "0 7 * * *"
    timezone: "America/Denver"
simply remove that block for non-prod, and use Option B to attach the schedule only in prod. Quick question to tailor the guidance: are you deploying with
prefect deploy
from a
prefect.yaml
project, or programmatically (Python calling
.deploy()
)? If you share that, I can provide a drop-in example for your exact setup.
p
Leaving this here in case someone stumbles into this thread. I went with option c: and my
command
argument in my compose file looks like...
Copy code
command: >
      bash -c '
        if [ "$environment" = "PROD" ]; then
          echo "🚀 Deploying with prod deployments";
          prefect --no-prompt deploy --all --prefect-file prefect_prod.yaml
        else
          echo "🧪 Deploying with dev deployments";
          prefect --no-prompt deploy --all --prefect-file prefect_dev.yaml
        fi
      '
And I split my
prefect.yaml
file into two different files
prefect_dev.yaml
and
prefect_prod.yaml