<@ULVA73B9P> can you get info about the deployment...
# ask-marvin
a
@Marvin can you get info about the deployment inside the flow?
m
thought for 134.0 seconds
Yes — in Prefect 3.x you can get deployment-related info from inside a running flow. The simplest approach is
prefect.runtime
.
Copy code
python
from prefect import flow
from prefect.runtime import deployment, flow_run

@flow
def my_flow():
    print("deployment id:", deployment.id)
    print("deployment name:", deployment.name)
    print("deployment version:", deployment.version)
    print("deployment parameters:", deployment.parameters)

    print("flow run id:", flow_run.id)
    print("flow run name:", flow_run.name)
    print("flow run tags:", flow_run.tags)
    print("flow parameters:", flow_run.parameters)
What you can access easily -
deployment.id
-
deployment.name
-
deployment.version
-
deployment.parameters
-
deployment.flow_run_id
And from `flow_run`: -
flow_run.id
-
flow_run.name
-
flow_run.flow_name
-
flow_run.flow_version
-
flow_run.tags
-
flow_run.parameters
-
flow_run.job_variables
If you need lower-level metadata like work pool / work queue, use the flow run context:
Copy code
python
from prefect import flow
from prefect.context import FlowRunContext

@flow
def my_flow():
    ctx = FlowRunContext.get()
    if not ctx:
        return

    fr = ctx.flow_run
    print("deployment_id:", fr.deployment_id)
    print("work_pool_name:", fr.work_pool_name)
    print("work_queue_name:", fr.work_queue_name)
    print("job_variables:", fr.job_variables)
A few caveats - If the flow is run ad hoc (for example
python my_flow.py
) instead of from a deployment, deployment fields are usually
None
or
{}
. -
deployment.tags
are not exposed directly via
prefect.runtime.deployment
. If you need deployment fields not exposed at runtime, query the API:
Copy code
python
from prefect import flow
from prefect.context import FlowRunContext
from prefect.client.orchestration import get_client

@flow
async def my_flow():
    ctx = FlowRunContext.get()
    if not ctx or not ctx.flow_run.deployment_id:
        print("Not running from a deployment")
        return

    async with get_client() as client:
        dep = await client.read_deployment(ctx.flow_run.deployment_id)

    print("deployment tags:", dep.tags)
    print("description:", dep.description)
Rule of thumb - Use
prefect.runtime
for common metadata - Use
FlowRunContext.get()
for work pool / queue details - Use the client if you need full deployment object data If you want, I can give you a copy-paste helper function like
get_current_deployment_info()
that returns all of this in one dict.
a
@Marvin is it possible to log the branch a deployment pulls from durng a flow run
m
thought for 11.7 seconds
Yes — if you make that branch available to the flow at runtime. Prefect does not generally expose a universal
deployment.branch
field inside a flow run. If you're deploying from Git with
flow.from_source(...)
, the branch/ref used for the pull is part of the source configuration, but it is not a standard runtime field you can always read from
prefect.runtime.deployment
. So in practice, the reliable options are: Best options 1. Pass the branch as a deployment parameter 2. Set it in a variable/env var/job variable 3. Query the deployment definition and inspect its source/pull configuration if you need to infer it Recommended pattern: parameter
Copy code
python
from prefect import flow, get_run_logger

@flow
def my_flow(git_branch: str = "main"):
    logger = get_run_logger()
    <http://logger.info|logger.info>(f"Deployment branch: {git_branch}")
Then set
git_branch
in the deployment so every run logs it. Alternative: job variable / env var If your worker job config sets something like
GIT_BRANCH
, you can log it:
Copy code
python
import os
from prefect import flow, get_run_logger

@flow
def my_flow():
    logger = get_run_logger()
    <http://logger.info|logger.info>(f"Deployment branch: {os.getenv('GIT_BRANCH', 'unknown')}")
Can you inspect the deployment from inside the flow? Yes, sometimes. You can query the deployment object from the API, but whether the branch is easy to extract depends on how the deployment source was configured.
Copy code
python
from prefect import flow, get_run_logger
from prefect.context import FlowRunContext
from prefect.client.orchestration import get_client

@flow
async def my_flow():
    logger = get_run_logger()
    ctx = FlowRunContext.get()

    if not ctx or not ctx.flow_run.deployment_id:
        <http://logger.info|logger.info>("No deployment id found; probably an ad-hoc run")
        return

    async with get_client() as client:
        deployment = await client.read_deployment(ctx.flow_run.deployment_id)

    <http://logger.info|logger.info>(f"Deployment name: {deployment.name}")
    <http://logger.info|logger.info>(f"Deployment: {deployment}")
But I wouldn't recommend depending on parsing that object for branch info unless you've confirmed your deployment shape and tested it. Bottom line - Yes, you can log the branch during a flow run - No, there is not a guaranteed built-in runtime field like
deployment.branch
- The most reliable solution is to explicitly pass/store the branch in the deployment configuration If you want, I can show you the cleanest way to do this for: -
flow.from_source(...).deploy(...)
-
prefect deploy
- GitHub Actions-based deployments
a
@Marvin is it possible to see the branch used by a flow run in the prefect cloud ui if it has been changed for the deployment after the run
m
thought for 10.1 seconds
Not by default, no. A flow run in Prefect Cloud is tied to the deployment id/versioning context at launch time, but the UI does not generally preserve and display a historical “Git branch used for this run” field if the deployment’s branch/source is later changed. So if you: - run deployment
A
when it points at branch
feature-x
- later update deployment
A
to point at
main
…the run UI will usually show the current deployment metadata, not a guaranteed immutable historical branch field for that old run. What is preserved for the run You can reliably keep branch history only if you store it as run-time data, for example: - a flow parameter like
git_branch
- a tag - a logged message/artifact - a custom variable in job config copied into the run Then the run’s logs / parameters / artifacts remain associated with that specific run even if the deployment changes later. Best practice If branch provenance matters, explicitly record it at run time. For example:
Copy code
python
from prefect import flow, get_run_logger

@flow
def my_flow(git_branch: str = "unknown"):
    logger = get_run_logger()
    <http://logger.info|logger.info>(f"Git branch for this run: {git_branch}")
Then every run keeps its own branch value in: - flow run parameters - logs You could also create an artifact if you want it more visible in the UI. Short answer - Current deployment branch after edits? Yes, you can inspect the deployment as it exists now. - Historical branch actually used by an older run after the deployment was changed? Not reliably in the UI unless you recorded it on the run itself. If you want, I can show you the best way to persist branch info per run so it’s visible in Prefect Cloud even after deployments are updated.
a
@Marvin when you use a variable to set the branch for a deployment like this branch: "{{ prefect.variables.deployment_branch }}" the template is still shown in the prefect cloud ui. is this right?
m
thought for 177.2 seconds
Yes — that is expected. If you set:
Copy code
yaml
branch: "{{ prefect.variables.deployment_branch }}"
then Prefect Cloud will typically show the template string itself in the deployment config/UI, not the resolved branch value. Why? Because for pull/source config like
branch
, Prefect keeps the unresolved template in the deployment definition and resolves it at runtime when the worker executes the pull step. So the behavior is: - Deployment UI/config: shows
{{ prefect.variables.deployment_branch }}
- Actual run: worker resolves
prefect.variables.deployment_branch
at run time and uses that value for the clone/pull That design is what allows you to change the variable later without redeploying. Important implication Because the branch is resolved at run time: - the deployment UI may not tell you which concrete branch a past run used - if the variable changes later, older runs won't automatically show the old resolved branch unless you log or persist it yourself Best practice if you care about auditability Log the resolved branch during the run, or persist it as an artifact/parameter. Otherwise the UI will just keep showing the template. So short answer: yes, this is right. If you want, I can show you a pattern to record the resolved branch on every flow run so it's visible later in Cloud.