<@ULVA73B9P> how can i put conditional branch here...
# ask-marvin
s
@Marvin how can i put conditional branch here from another varibale for example if prefect.variables.environment = PROD, branch:main else branch: dev pull: - prefect.deployments.steps.git_clone: repository: https://github.com/PrefectHQ/hello-projects.git branch: "{{ prefect.variables.deployment_branch }}"
m
thought for 6.7 seconds
Let me help you with setting up conditional branching using Prefect variables in your deployment YAML. In Prefect 3.x, you can use Jinja templating to achieve this conditional logic. Here's how you can modify your pull configuration to implement the branch condition you described:
Copy code
yaml
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:
Copy code
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:
Copy 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.
s
can i log this somewhere to be sure which branch it pulled from @Marvin
m
thought for 9.5 seconds
Yes, absolutely! You can add logging to track which branch was selected using Prefect's built-in logging capabilities. Here's how you can modify your configuration to include logging:
Copy code
yaml
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:
Copy code
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.