<@ULVA73B9P> Give me example how to use Gitlab Credentials block during flow pull using gitlab token...
r
@Marvin Give me example how to use Gitlab Credentials block during flow pull using gitlab token. Please show prefect.yaml and exact steps to setup block in UI. I am using local gitlab instance (gitlab.local.com). Token has name my-token.
m
thought for 1076.4 seconds
Great question — here’s a working example for Prefect 3.x that uses a GitLab Credentials block to authenticate when pulling your flow code from a self-hosted GitLab during deployment. Assumptions - You’re on Prefect 3.x - Your GitLab is at http://gitlab.local.com - You’ve created a personal access token in GitLab (the token’s label is “my-token”) - You want to reference a Prefect Block named “my-token” that holds this token Step 1 — Install integration and register blocks Make sure the GitLab integration is installed everywhere your deployments will run (local dev + worker images/environments):
Copy code
pip install "prefect[gitlab]"
prefect block register -m prefect_gitlab.credentials
- Block registration makes the GitLab block type available in the UI. - Help link: prefect-gitlab integration docs Step 2 — Create the GitLab Credentials block in the UI 1) In the Prefect UI, go to Blocks 2) Click “+ Create block” 3) Search for “GitLab Credentials” 4) Fill the fields: - Name: my-token - Base URL: http://gitlab.local.com - Access token: paste your GitLab token value (ensure it has at least read_repository scope) 5) Save Step 3 — Use the block in prefect.yaml to pull from GitLab This prefect.yaml uses deployment “pull” steps to: - Load your GitLab Credentials block - Clone your repository with those credentials - Set the working directory to the checked-out code - Register a deployment for a flow found in flows/my_flow.py:my_flow Replace owner/repo with your real GitLab path.
Copy code
name: gitlab-example
prefect-version: ">=3.0.0,<4.0.0"

pull:
  # Load the GitLab Credentials block
  - prefect.deployments.steps.pull.pull_with_block:
      id: gl-creds
      block_type_slug: gitlab-credentials
      block_document_name: my-token

  # Clone your repo using the credentials block
  - prefect.deployments.steps.pull.git_clone:
      id: clone
      repository: "<http://gitlab.local.com/owner/repo.git>"
      branch: main
      credentials: "{{ gl-creds.block }}"
      # If you prefer, you can pass the token directly instead:
      # access_token: "{{ gl-creds.block.access_token }}"

  # Set the working directory to the cloned repo so entrypoint paths resolve
  - prefect.deployments.steps.pull.set_working_directory:
      directory: "{{ clone.directory }}"

deployments:
  - name: my-flow/my-deployment
    entrypoint: "flows/my_flow.py:my_flow"
    work_pool:
      name: default
    # optional schedules, parameters, etc.
Step 4 — Deploy From the directory with your prefect.yaml:
Copy code
prefect deploy
Notes and tips - The git_clone step supports either credentials: <Block> or access_token: <string>. Using the block is preferred for secret management. - Use the full Git URL including .git, e.g. http://gitlab.local.com/group/project.git - Ensure the same packages are available in your execution environment (workers) — i.e., install prefect[gitlab] there too - Docs for YAML and pull steps: - prefect.yaml reference - Store flow code for deployments If you share your exact repo path and flow entrypoint, I can tailor the YAML to your setup precisely.