Loading...
Loading...
Set up uv (Rust-based Python package manager) in CI/CD pipelines. Use when configuring GitHub Actions workflows, GitLab CI/CD, Docker builds, or matrix testing across Python versions. Includes patterns for cache optimization, frozen lockfiles, multi-stage builds, and PyPI publishing with trusted publishing. Covers GitHub Actions setup-uv action, Docker multi-stage production/development builds, and deployment patterns.
npx skill4agent add dawiddutoit/custom-claude uv-ci-cd-integration# Create .github/workflows/ci.yml
curl -s https://docs.astral.sh/uv/guides/integration/github/ | grep -A 30 "name: CI" > temp.yamlFROM python:3.12-slim AS builder
COPY /uv /usr/local/bin/uv
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev --no-install-project
FROM python:3.12-slim
COPY /app/.venv /app/.venv
COPY . .
ENV PATH="/app/.venv/bin:$PATH"
CMD ["python", "-m", "myapp"]# Install uv in before_script, sync dependencies, run tests
curl -LsSf https://astral.sh/uv/install.sh | sh
uv sync --all-extras --dev
uv run pytestsetup-uv- name: Install uv
uses: astral-sh/setup-uv@v6
with:
version: "0.9.8" # Optional: pin specific version
enable-cache: true # Enable dependency caching
cache-dependency-glob: "uv.lock" # Track changes to this filevariables:
UV_CACHE_DIR: .uv-cache
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- .uv-cache# Layer caching: Only rebuild if pyproject.toml or uv.lock changes
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev --no-install-projectstrategy:
matrix:
python-version: ["3.11", "3.12", "3.13"]
steps:
- uses: astral-sh/setup-uv@v6
- run: uv python install ${{ matrix.python-version }}
env:
UV_PYTHON: ${{ matrix.python-version }}
- run: uv sync --all-extras --dev
- run: uv run pytesttest:3.11:
image: python:3.11
script:
- curl -LsSf https://astral.sh/uv/install.sh | sh
- uv sync --all-extras --dev
- uv run pytest
test:3.12:
image: python:3.12
script:
- curl -LsSf https://astral.sh/uv/install.sh | sh
- uv sync --all-extras --dev
- uv run pytest# Fails if lockfile is out of sync with pyproject.toml
uv sync --frozen --no-dev
# For development environments (interactive)
uv sync --all-extras --dev--frozenRUN uv sync --frozen --no-dev --no-install-project- name: Sync with frozen lockfile
run: uv sync --frozen --all-extras --devuv.lockuv lock --upgrade# Stage 1: Builder - compile dependencies
FROM python:3.12-slim AS builder
COPY /uv /usr/local/bin/uv
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev --no-install-project
# Stage 2: Runtime - minimal image with only .venv
FROM python:3.12-slim
WORKDIR /app
COPY /app/.venv /app/.venv
# Copy application code
COPY . .
# Ensure virtual environment is in PATH
ENV PATH="/app/.venv/bin:$PATH"
# Run application
CMD ["python", "-m", "myapp"]name: Publish
on:
push:
tags:
- "v*"
jobs:
publish:
runs-on: ubuntu-latest
permissions:
id-token: write # Required for OIDC/trusted publishing
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v6
- name: Build distributions
run: uv build
- name: Publish to PyPI
run: uv publish
# No credentials needed - uses OIDC tokens- name: Publish to custom index
run: uv publish --index-url https://example.org/pypi
env:
UV_PUBLISH_TOKEN: ${{ secrets.CUSTOM_PYPI_TOKEN }}examples/github-actions-complete.ymlexamples/dockerfile-developmentexamples/gitlab-ci-complete.ymlexamples/pypi-publishing-workflow.ymluv.lock.python-versionuv python pin 3.12