<@ULVA73B9P> how to load gitlab ci token to clone ...
# ask-marvin
h
@Marvin how to load gitlab ci token to clone codes using prefect block?
m
thought for 103.2 seconds
In Prefect 3.x, you generally don’t load a GitLab CI token into a “git storage block” like in older patterns. The recommended approach is: * use
flow.from_source(...)
/
.deploy(...)
for Python deployments, or * use a
git_clone
pull step in
prefect.yaml
* store the token in either: * a
GitLabCredentials
block, or * a
Secret
block Best option for private GitLab repos: use `prefect-gitlab`’s
GitLabCredentials
block. Docs / references: * GitRepository reference * Secret block reference * prefect-gitlab credentials docs 1) Using a
GitLabCredentials
block
First install the integration and register blocks if needed:
Copy code
pip install prefect-gitlab
prefect block register -m prefect_gitlab
Create/save the block:
Copy code
from prefect_gitlab import GitLabCredentials

creds = GitLabCredentials(token="your-gitlab-token")
creds.save("gitlab-creds")
Then use it with a private repo:
Copy code
from prefect import flow
from prefect.runner.storage import GitRepository
from prefect_gitlab import GitLabCredentials

@flow
def my_flow():
    print("hello")

if __name__ == "__main__":
    source = GitRepository(
        url="<https://gitlab.com/my-org/my-repo.git>",
        credentials=GitLabCredentials.load("gitlab-creds"),
        branch="main",
    )

    my_flow.from_source(
        source=source,
        entrypoint="flows.py:my_flow",
    ).deploy(
        name="my-deployment",
        work_pool_name="my-pool",
    )
2) Using a
Secret
block for the token
If you prefer storing only the token as a secret:
Copy code
from prefect.blocks.system import Secret

Secret(value="your-gitlab-token").save("gitlab-token")
Then:
Copy code
from prefect import flow
from prefect.runner.storage import GitRepository
from prefect.blocks.system import Secret

@flow
def my_flow():
    print("hello")

if __name__ == "__main__":
    source = GitRepository(
        url="<https://gitlab.com/my-org/my-repo.git>",
        credentials={
            "access_token": Secret.load("gitlab-token")
        },
        branch="main",
    )

    my_flow.from_source(
        source=source,
        entrypoint="flows.py:my_flow",
    ).deploy(
        name="my-deployment",
        work_pool_name="my-pool",
    )
3) If you are using
prefect.yaml
with a pull step
You can clone from GitLab in the deployment pull step:
Copy code
pull:
  - prefect.deployments.steps.git_clone:
      repository: <https://gitlab.com/my-org/my-repo.git>
      credentials: "{{ prefect.blocks.gitlab-credentials.gitlab-creds }}"
Or with a
Secret
block:
Copy code
pull:
  - prefect.deployments.steps.git_clone:
      repository: <https://gitlab.com/my-org/my-repo.git>
      access_token: "{{ prefect.blocks.secret.gitlab-token }}"
Important note about GitLab CI tokens If by “GitLab CI token” you mean
CI_JOB_TOKEN
, that token usually works only within GitLab CI job permissions and repo/project scope. Whether it can clone your target repo depends on your GitLab project settings and token permissions. If you want to use it dynamically in CI, the simplest pattern is often to expose it as an environment variable and create the Prefect block at deploy time:
Copy code
from prefect_gitlab import GitLabCredentials
import os

GitLabCredentials(token=os.environ["CI_JOB_TOKEN"]).save(
    "gitlab-creds",
    overwrite=True
)
Then use
GitLabCredentials.load("gitlab-creds")
. Important Prefect 3.x note Old 2.x git storage/block patterns are removed. Don’t use: *
Deployment.build_from_flow()
*
prefect deployment build
* old GitHub/Git storage block patterns Use
flow.from_source(...).deploy(...)
instead. If you want, I can give you the exact working example for GitLab CI in one of these cases: 1. deploying from a GitLab CI pipeline
2. worker cloning code at runtime from a private GitLab repo 3. using
prefect.yaml
only