Skip to content

Using uv in GitLab CI/CDGitLab CI/CDでのuvの使用

Using the uv imageuvイメージの使用

Astral provides Docker images with uv preinstalled. Select a variant that is suitable for your workflow.AstralはDockerイメージを提供しており、uvがプリインストールされています。 あなたのワークフローに適したバリアントを選択してください。

gitlab-ci.yml
variables:
  UV_VERSION: "0.5"
  PYTHON_VERSION: "3.12"
  BASE_LAYER: bookworm-slim
  # GitLab CI creates a separate mountpoint for the build directory,
  # so we need to copy instead of using hard links.
  UV_LINK_MODE: copy

uv:
  image: ghcr.io/astral-sh/uv:$UV_VERSION-python$PYTHON_VERSION-$BASE_LAYER
  script:
    # your `uv` commands

Note注意

If you are using a distroless image, you have to specify the entrypoint: distrolessイメージを使用している場合、エントリーポイントを指定する必要があります:

uv:
  image:
    name: ghcr.io/astral-sh/uv:$UV_VERSION
    entrypoint: [""]
  # ...

Cachingキャッシング

Persisting the uv cache between workflow runs can improve performance.ワークフローの実行間でuvキャッシュを保持することで、パフォーマンスが向上する可能性があります。

uv-install:
  variables:
    UV_CACHE_DIR: .uv-cache
  cache:
    - key:
        files:
          - uv.lock
      paths:
        - $UV_CACHE_DIR
  script:
    # Your `uv` commands
    - uv cache prune --ci

See the GitLab caching documentation for more details on configuring caching.キャッシュの設定に関する詳細は、GitLabキャッシングドキュメントを参照してください。

Using uv cache prune --ci at the end of the job is recommended to reduce cache size. See the uv cache documentation for more details.ジョブの最後にuv cache prune --ciを使用することをお勧めします。これによりキャッシュサイズが削減されます。詳細についてはuvキャッシュドキュメントを参照してください。

Using uv pipUsing uv pip

If using the uv pip interface instead of the uv project interface, uv requires a virtual environment by default. To allow installing packages into the system environment, use the --system flag on all uv invocations or set the UV_SYSTEM_PYTHON variable.uvプロジェクトインターフェースの代わりにuv pipインターフェースを使用する場合、uvはデフォルトで仮想環境を必要とします。システム環境にパッケージをインストールできるようにするには、すべてのuv呼び出しに--systemフラグを使用するか、UV_SYSTEM_PYTHON変数を設定してください。

The UV_SYSTEM_PYTHON variable can be defined in at different scopes. You can read more about how variables and their precedence works in GitLab here変数 UV_SYSTEM_PYTHON は異なるスコープで定義できます。GitLabにおける変数とその優先順位についてはこちらで詳しく読むことができます

Opt-in for the entire workflow by defining it at the top level:最上位で定義することで、ワークフロー全体にオプトインします:

gitlab-ci.yml
variables:
  UV_SYSTEM_PYTHON: 1

# [...]

To opt-out again, the --no-system flag can be used in any uv invocation.再度オプトアウトするには、任意の uv 呼び出しで --no-system フラグを使用できます。

When persisting the cache, you may want to use requirements.txt or pyproject.toml as your cache key files instead of uv.lock.キャッシュを永続化する際には、キャッシュキーとして uv.lock の代わりに requirements.txt または pyproject.toml を使用することを検討してください。