<@ULVA73B9P> what's the best way to deploy workflo...
# ask-marvin
n
@Marvin what's the best way to deploy workflows in CI w prefect?
m
🔄 Researching your answer... this may take a while 🔧 Using tool:
research_prefect_topic
📊 Tools used: •
explore_module_offerings
(1x) •
research_prefect_topic
(1x)
n
@Marvin ayo?
m
âś… thought for 16.0 seconds
Hey! How can I help today? If you want, we can dig into: - Reducing ECS/EC2 flow-run container startup latency - Parallel task execution and task runners - Container-based deployments best practices If you share a bit more (Prefect version, work pool type, image size/base, how you deploy, and what startup times you’re seeing), I can give targeted steps.
n
@Marvin what's the best way to deploy workflows in CI w prefect?
m
âś… thought for 1626.1 seconds
Great question. In Prefect 3.x there are two solid patterns for deploying flows from CI. Pick the one that fits your repo/org setup: 1) Project-driven deployments with the CLI (prefect deploy) - Best when each repo is self-contained and owns its deployments - Steps: - In your repo, scaffold once with
prefect init
(choose an ECS/Docker/Kubernetes recipe if you want a container build baked in) - Commit the generated config - In CI: - Authenticate to Prefect Cloud:
prefect cloud login --key $PREFECT_API_KEY --workspace $PREFECT_WORKSPACE
- Optionally build and push your Docker image (tag with the git SHA) - Call
prefect deploy
from the repo root. It reads your
prefect.yaml
and creates/updates deployments. 2) Code-driven deployments from CI (Flow.from_source(...).deploy(...)) - Best when you want a central “deployment job” that can deploy any repo/flow without cloning or when you prefer explicit control in code - Steps: - In CI, run a tiny Python script that: - points to your Git repo (or package storage) - sets the entrypoint - targets the work pool (ECS/Kubernetes/Docker) - sets the image, parameters, schedules, etc. Below are minimal GitHub Actions examples for both approaches. They assume: - Prefect Cloud with secrets PREFECT_API_KEY and PREFECT_WORKSPACE - ECS work pool already created and a worker running - You build/push an image per commit and want that image in the deployment A) GitHub Actions using prefect deploy
Copy code
name: Deploy Prefect flows

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Install Prefect
        run: pip install "prefect[aws]"

      - name: Authenticate to Prefect Cloud
        run: prefect cloud login --key "$PREFECT_API_KEY" --workspace "$PREFECT_WORKSPACE"
        env:
          PREFECT_API_KEY: ${{ secrets.PREFECT_API_KEY }}
          PREFECT_WORKSPACE: ${{ secrets.PREFECT_WORKSPACE }}

      # Optional: build and push your image (ECR example)
      - name: Configure AWS
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1

      - name: Login to ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v2

      - name: Build and push image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPOSITORY: my-prefect-flows
          IMAGE_TAG: ${{ github.sha }}
        run: |
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

      # Deploy using repo config
      - name: Deploy
        run: |
          # `prefect.yaml` should define deployments and target your ECS work pool.
          # If your recipe supports it, you can set image via env or directly in the config.
          prefect deploy
B) GitHub Actions using Flow.from_source(...).deploy(...) ``` name: Deploy Prefect flows (code-driven) on: push: branches: [ main ] jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/setup-python@v5 with: python-version: "3.11" - name: Install Prefect run: pip install "prefect[aws]" - name: Authenticate to Prefect Cloud run: prefect cloud login --key "$PREFECT_API_KEY" --workspace "$PREFECT_WORKSPACE" env: PREFECT_API_KEY: ${{ secrets.PREFECT_API_KEY }} PREFECT_WORKSPACE: ${{ secrets.PREFECT_WORKSPACE }} # Optional: build/push image (same as above) - name: Configure AWS uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: us-east-1 - name: Login to ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v2 - name: Build and push image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} ECR_REPOSITORY: my-prefect-flows IMAGE_TAG: ${{ github.sha }} run: | docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG - name: Deploy via Python env: GIT_SHA: ${{ github.sha }} IMAGE: ${{ steps.login-ecr.outputs.registry }}/my-prefect-flows:${{ github.sha }} run: | python - << 'PY' import os from prefect.flows import Flow # Point to your repo and flow entrypoint flow = Flow.from_source( source="https://github.com/&lt;org&gt;/&lt;repo&gt;.git", entrypoint="flows/my_flow.py:my_flow", ) deployment_id = flow.deploy( name=f"my-flow-{os.environ['GIT_SHA'][:7]}", work_pool_name="ecs-pool", image=os.environ["IMAGE"], # Optionally set schedules and parameters # cron="0 * * * *", # parameters={"env": "prod"}, # ECS-specific overrides if needed; these keys must match your work pool template # job_variables={ # "cluster": "my-ecs-cluster", # "subnets": ["subnet-abc", "subnet-def"], # "securityGroups": ["sg-123"], # } ) print("Created/updated deployment:", deployment_id) PY ``` Notes and tips - Work pools and workers - Ensure you have a work pool for your target runtime (ECS, Kubernetes, Docker) and at least one worker running:
prefect worker start -p <pool-name>
- You can create or update a pool in CI with
prefect work-pool create "<pool-name>" --type <ecs|kubernetes|docker> --overwrite
if you want to manage it as code. - Image build strategy - Tag images with the git SHA; pass that tag to your deployment to guarantee reproducibility. - Cache layers in CI to speed up builds. - For ECS, push to ECR and reference that image in the deployment. - Secrets and auth - Prefect Cloud: use
prefect cloud login --key ... --workspace ...
- Prefect Server: set
PREFECT_API_URL
in CI instead of logging in. - When to use which approach - Use
prefect deploy
if your repo already contains deployment config (e.g., from
prefect init
) and you want “convention over configuration”. - Use
Flow.from_source(...).deploy(...)
if you prefer a single CI job that deploys multiple flows or repos, or you want full control in code without relying on on-disk project files. If you share: - Prefect Cloud vs Server - Your work pool type (sounds like ECS) - Which registry you use for images - Whether you want the deployment name to track branches/SHAs …I can tailor the exact CI YAML and, for ECS, suggest the right
job_variables
that match your task definition and networking.