<@ULVA73B9P>: How can I specify a Git commit SHA t...
# marvin-ai
j
@Marvin: How can I specify a Git commit SHA to use when deploying a flow from GitHub?
m
thought for 371.5 seconds
In Prefect 3.x, pin the deployment to a specific Git commit by setting the commit_sha on the git clone pull step. The parameter name is commit_sha (not ref/revision/commit). Add this to your prefect.yaml and deploy with prefect deploy:
Copy code
deployments:
  - name: prod
    entrypoint: flows/etl.py:etl
    pull:
      - prefect.deployments.steps.git_clone:
          repository: "<https://github.com/your-org/your-repo.git>"
          # pin to an exact commit:
          commit_sha: "abc1234deadbeefcafebabefeedface01234567"
          # optional helpers:
          # branch: "main"
          # access_token: "{{ prefect.variables.GITHUB_TOKEN }}"
      - prefect.deployments.steps.pip_install_requirements:
          requirements_file: "requirements.txt"
Tip: You can template the SHA from CI or a Prefect variable:
Copy code
commit_sha: "{{ env.CI_COMMIT_SHA }}"
# or
commit_sha: "{{ prefect.variables.GIT_COMMIT_SHA }}"
Notes: - commit_sha takes precedence if both branch and commit_sha are provided. - Flow.from_source(...) itself does not accept a commit parameter. The pinning happens via the pull step configuration used by prefect deploy. Docs: - prefect.yaml guide - Deployment versioning
j
@Marvin: How could I do it with the Python SDK?
m
thought for 187.4 seconds
Great question. In Prefect 3.x you can pin to a specific Git commit entirely from the Python SDK by using a GitRepository as the source for your deployment. Two common patterns: 1) Flow.from_source(...).deploy(...) - Load the flow directly from your repo (at the pinned commit) and deploy it.
Copy code
from prefect.flows import Flow
from prefect.runner.storage import GitRepository

repo = GitRepository(
    url="<https://github.com/your-org/your-repo.git>",
    commit_sha="abc1234deadbeefcafebabefeedface01234567",  # pin here
    # optional:
    # branch="main",
    # include_submodules=True,
    # directories=["flows"],  # clone only these directories
)

flow = Flow.from_source(
    source=repo,
    entrypoint="flows/etl.py:etl",  # path in the repo
)

flow.deploy(
    name="prod",
    work_pool_name="my-work-pool",
    # other options like schedule, parameters, tags, etc.
)
2) Construct a RunnerDeployment and call prefect.deployments.deploy(...) - Useful if you prefer to reference an entrypoint without importing the flow in this script.
Copy code
from prefect.deployments.runner import RunnerDeployment
from prefect.deployments import deploy
from prefect.runner.storage import GitRepository

deployment = RunnerDeployment(
    name="prod",
    entrypoint="flows/etl.py:etl",
    storage=GitRepository(
        url="<https://github.com/your-org/your-repo.git>",
        commit_sha="abc1234deadbeefcafebabefeedface01234567",
        # branch="main",
    ),
    work_pool_name="my-work-pool",
    # parameters={}, schedules=[...], tags=[...], etc.
)

deploy(deployment)
Notes and tips: - The parameter is commit_sha. If both branch and commit_sha are set, the commit_sha wins. - For private repos, authenticate using your preferred method. Common options: - Embed a token in the URL (e.g., GitHub’s x-access-token format), or - Provide credentials via a GitCredentials block (if you’ve created one) passed to GitRepository via the credentials argument. - include_submodules=True if you rely on submodules. Docs: - Deployment versioning - Store flow code (Git)