shlok
07/11/2025, 10:13 AMMarvin
07/11/2025, 10:13 AMMarvin
07/11/2025, 10:13 AMyaml
pull:
- prefect.deployments.steps.git_clone:
repository: <https://github.com/PrefectHQ/hello-projects.git>
branch: "{% if prefect.variables.environment == 'PROD' %}main{% else %}dev{% endif %}"
This configuration uses Jinja's if-else syntax to:
1. Check if prefect.variables.environment
equals 'PROD'
2. Use 'main' branch if true
3. Use 'dev' branch if false
To make this work, you'll need to:
1. Set your environment variable using either:
bash
prefect variable set environment PROD # For production
# or
prefect variable set environment DEV # For development
You can also set variables programmatically in your code:
python
from prefect import variables
variables.set("environment", "PROD") # or "DEV"
The deployment will evaluate this condition at runtime and choose the appropriate branch based on the value of prefect.variables.environment
.
Note: Make sure both 'main' and 'dev' branches exist in your repository, or you might encounter errors when the deployment tries to clone a non-existent branch.shlok
07/11/2025, 10:35 AMMarvin
07/11/2025, 10:35 AMMarvin
07/11/2025, 10:35 AMyaml
pull:
- prefect.deployments.steps.run_shell_script:
script: echo "Preparing to clone from branch: {% if prefect.variables.environment == 'PROD' %}main{% else %}dev{% endif %}"
stream_output: true
- prefect.deployments.steps.git_clone:
repository: <https://github.com/PrefectHQ/hello-projects.git>
branch: "{% if prefect.variables.environment == 'PROD' %}main{% else %}dev{% endif %}"
Or, for a more robust solution, you can create a small Python script as a deployment step:
yaml
pull:
- prefect.deployments.steps.run_python_script:
script: |
from prefect import get_client, variables
from prefect.logging import get_run_logger
logger = get_run_logger()
env = variables.get("environment", default="DEV")
branch = "main" if env == "PROD" else "dev"
<http://logger.info|logger.info>(f"Environment is set to: {env}")
<http://logger.info|logger.info>(f"Selected branch for deployment: {branch}")
- prefect.deployments.steps.git_clone:
repository: <https://github.com/PrefectHQ/hello-projects.git>
branch: "{% if prefect.variables.environment == 'PROD' %}main{% else %}dev{% endif %}"
These logs will appear in:
1. The deployment logs in the Prefect UI
2. Your server logs
3. The flow run logs associated with this deployment
You can view these logs in several ways:
- Through the Prefect UI in the deployment's run history
- Using the CLI: prefect deployment logs <deployment-name>
- In your server logs if you're running Prefect Server
The logging will help you confirm which branch was selected based on your environment variable setting.