Deploying a Python Package with GitLab CI using Podman and Alpine
Here's a comprehensive guide to set up GitLab CI/CD for your Python package using Podman runners and Alpine-based containers with uv for Python package management.
1. Set up GitLab Runner with Podman
Install Podman on your server
# For Debian/Ubuntu
sudo apt-get update
sudo apt-get install -y podman
# For RHEL/CentOS
sudo yum install -y podman
Register a GitLab Runner
-
Get your registration token from GitLab:
- Go to your project → Settings → CI/CD → Runners
- Copy the registration token
-
Register the runner:
sudo gitlab-runner register \
--non-interactive \
--url "https://gitlab.com/" \
--registration-token "YOUR_REGISTRATION_TOKEN" \
--executor "docker" \
--docker-image "alpine:latest" \
--description "podman-runner" \
--tag-list "python,alpine,uv" \
--run-untagged="true" \
--locked="false" \
--docker-volumes "/var/run/podman/podman.sock:/var/run/docker.sock" \
--docker-privileged
Configure the runner to use Podman
Edit /etc/gitlab-runner/config.toml:
[[runners]]
name = "podman-runner"
url = "https://gitlab.com/"
token = "YOUR_RUNNER_TOKEN"
executor = "docker"
[runners.docker]
tls_verify = false
image = "alpine:latest"
privileged = true
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/var/run/podman/podman.sock:/var/run/docker.sock", "/cache"]
shm_size = 0
Restart the runner:
sudo systemctl restart gitlab-runner
2. Create your .gitlab-ci.yml file
Here's a complete example for your Python package:
stages:
- test
- build
- deploy
variables:
# Use alpine-based Python image
PYTHON_IMAGE: "python:alpine"
# Use uv for package management
UV_VERSION: "0.1.44"
before_script:
- echo "Installing uv..."
- pip install --no-cache-dir uv==${UV_VERSION}
- uv venv
- source .venv/bin/activate
test:
stage: test
image: $PYTHON_IMAGE
script:
- uv pip install -e .[test]
- pytest tests/
tags:
- python
- alpine
- uv
build:
stage: build
image: $PYTHON_IMAGE
script:
- uv pip install build
- python -m build
artifacts:
paths:
- dist/
tags:
- python
- alpine
- uv
deploy:
stage: deploy
image: $PYTHON_IMAGE
script:
- uv pip install twine
- twine upload --username __token__ --password ${PYPI_TOKEN} dist/*
only:
- tags
tags:
- python
- alpine
- uv
3. Set up your Python project
Project structure
your_package/
├── src/
│ └── your_package/
│ ├── __init__.py
│ └── ... (your code)
├── tests/
│ └── ... (your tests)
├── pyproject.toml
├── .gitlab-ci.yml
└── README.md
Example pyproject.toml
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "your-package"
version = "0.1.0"
description = "Your package description"
readme = "README.md"
requires-python = ">=3.8"
license = {text = "MIT"}
authors = [
{name = "Your Name", email = "your.email@example.com"},
]
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
]
[project.urls]
Homepage = "https://gitlab.com/your-username/your-package"
[project.optional-dependencies]
test = ["pytest>=7.0", "pytest-cov>=3.0"]
4. Set up secrets
- Add your PyPI token as a CI/CD variable:
- Go to your project → Settings → CI/CD → Variables
- Add a new variable:
- Key:
PYPI_TOKEN
- Value: Your PyPI API token
- Check "Mask variable"
- Check "Protect variable" if you only want it available for protected branches/tags
5. Additional optimizations
Create a custom Alpine-based Python image with uv pre-installed
- Create a
Dockerfile:
FROM python:alpine
# Install build dependencies
RUN apk add --no-cache gcc musl-dev libffi-dev openssl-dev
# Install uv
ARG UV_VERSION=0.1.44
RUN pip install --no-cache-dir uv==${UV_VERSION}
# Set up default entrypoint
ENTRYPOINT ["uv"]
- Build and push to your container registry:
podman build -t your-registry/python-uv-alpine .
podman push your-registry/python-uv-alpine
- Update your
.gitlab-ci.yml to use this custom image:
variables:
PYTHON_IMAGE: "your-registry/python-uv-alpine"
6. Run your pipeline
Commit and push your changes to trigger the pipeline:
git add .gitlab-ci.yml pyproject.toml src/ tests/
git commit -m "Set up GitLab CI/CD with Podman and uv"
git push origin main
Your pipeline should now run, executing the test, build, and deploy stages as configured.