Pydantic models wrapping GitLab CI/CD YAML with 1:1 mapping
Project description
Gitlab CICD Python Wrapper
Pydantic models wrapping every GitLab CI/CD YAML keyword with 1:1 mapping to GitLab CI/CD YAML reference.
Features
- Programmatic pipeline generation using Python-native objects
- Validation framework for existing pipelines
- Comment-preserving round-trip serialization (load -> validate -> write = identical YAML)
- GitLab CI/CD Component parsing with input validation
- Both sync and async APIs
- CLI validator and pre-commit hook
Installation
pip install gitlab-cicd-python-wrapper
Or with Poetry:
poetry add gitlab-cicd-python-wrapper
Compatibility Matrix
| GitLab Version | Pipeline Coverage | Component Coverage | Notes |
|---|---|---|---|
| 17.x | Full | Full | All keywords as of 17.9 |
| 16.x | Full | Partial | Components GA in 17.0 |
| 15.x | Partial | N/A | Deprecated keywords still accepted |
Quick Start
Generate a Pipeline
from gitlab_cicd_python_wrapper import Pipeline, Job, Image, Artifacts
pipeline = Pipeline(
stages=["build", "test", "deploy"],
jobs={
"build": Job(
stage="build",
image=Image(name="python:3.11"),
script=["pip install -r requirements.txt", "python setup.py build"],
artifacts=Artifacts(paths=["dist/"]),
),
"test": Job(
stage="test",
script=["pytest"],
needs=["build"],
),
},
)
print(pipeline.to_yaml())
Validate an Existing Pipeline
from gitlab_cicd_python_wrapper import Pipeline
# Load and validate
pipeline = Pipeline.from_yaml(".gitlab-ci.yml")
print(f"Stages: {pipeline.stages}")
print(f"Jobs: {list(pipeline.jobs.keys())}")
# Validate without loading
errors = Pipeline.validate_file(".gitlab-ci.yml")
if errors:
for err in errors:
print(f"Error: {err}")
Round-Trip Editing (Comments Preserved)
from gitlab_cicd_python_wrapper import Pipeline
# Load -> modify -> save preserves comments and formatting
pipeline = Pipeline.from_yaml(".gitlab-ci.yml")
pipeline.to_yaml("validated-output.yml")
# Output is byte-identical to input for unmodified pipelines
CLI Validator
# Validate pipeline files
gitlab-cicd-validate .gitlab-ci.yml
# Validate multiple files
gitlab-cicd-validate .gitlab-ci.yml .gitlab/ci/*.yml
# Validate component templates
gitlab-cicd-validate --component templates/build.yml
# JSON output
gitlab-cicd-validate --format json .gitlab-ci.yml
# Strict mode (fail on warnings)
gitlab-cicd-validate --strict .gitlab-ci.yml
Pre-commit Hook
Add to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/szymonrychu/gitlab-cicd-python-wrapper
rev: v0.1.0
hooks:
- id: gitlab-cicd-validate
args: ['--strict']
- id: gitlab-cicd-validate-components
This validates .gitlab-ci.yml and any YAML files under .gitlab/ci/ automatically,
plus component templates under templates/.
Component Parsing
Parse and validate GitLab CI/CD components with input validation:
from gitlab_cicd_python_wrapper import Component
# Load a component template
component = Component.from_yaml("templates/deploy.yml")
# Inspect the spec
for name, input_def in component.spec.inputs.items():
print(f"{name}: type={input_def.type}, default={input_def.default}")
# Validate inputs (raises ValueError on failure)
resolved = component.validate_inputs({
"stage": "deploy",
"environment": "production",
"timeout": 3600,
})
# Render with inputs interpolated (returns a Pipeline)
pipeline = component.render({
"stage": "deploy",
"environment": "production",
"timeout": 3600,
})
print(pipeline.to_yaml())
Dynamic Child Pipelines
Generate pipelines dynamically for use with trigger:include:
from gitlab_cicd_python_wrapper import Pipeline, Job
services = ["api", "web", "worker"]
jobs = {}
for svc in services:
jobs[f"build-{svc}"] = Job(
stage="build",
image="docker:latest",
script=[f"docker build -t {svc} services/{svc}/"],
tags=["docker"],
)
child = Pipeline(stages=["build"], jobs=jobs)
child.to_yaml("generated-pipeline.yml")
# Parent pipeline triggers it:
# trigger:
# include:
# - artifact: generated-pipeline.yml
# job: generate-pipeline
Async API
import asyncio
from gitlab_cicd_python_wrapper import AsyncPipeline, AsyncComponent
async def main():
# Pipeline
pipeline = await AsyncPipeline.from_yaml(".gitlab-ci.yml")
print(f"Stages: {pipeline.stages}")
print(f"Jobs: {list(pipeline.jobs.keys())}")
await AsyncPipeline.to_yaml(pipeline, "validated-output.yml")
# Component
component = await AsyncComponent.from_yaml("templates/build.yml")
resolved = component.validate_inputs({"image": "python:3.12"})
asyncio.run(main())
Docker
Run the validator as a container:
docker run --rm -v $(pwd):/data szymonrychu/gitlab-cicd-python-wrapper /data/.gitlab-ci.yml
PyPI Configuration
To publish this library to PyPI using GitHub Actions trusted publishing:
- Create an account at pypi.org
- Go to "Your projects" -> "Publishing" -> "Add a new pending publisher"
- Set: GitHub owner=
szymonrychu, repo=gitlab-cicd-python-wrapper, workflow=pypi.yaml, environment=pypi - Create a GitHub environment named
pypiin your repo settings - Push a semantic version tag (e.g.,
v0.1.0) to trigger the publish workflow
Development
# Install dependencies
mise install
poetry install
# Run tests
poetry run pytest
# Run linters
pre-commit run --all-files
# Run CLI
poetry run gitlab-cicd-validate .gitlab-ci.yml
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gitlab_cicd_python_wrapper-0.0.8.tar.gz.
File metadata
- Download URL: gitlab_cicd_python_wrapper-0.0.8.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31a46b2e01a7bade829654b2ea9ef6f2a9144d77515ba3f43dccc5ad52fe3587
|
|
| MD5 |
b7c23f5bd47651971027cec4e64d673d
|
|
| BLAKE2b-256 |
65814fcd13e0fa292d5699c5de8cbd5f97bf97799ef292c86b3b25aaf03bfeb4
|
Provenance
The following attestation bundles were made for gitlab_cicd_python_wrapper-0.0.8.tar.gz:
Publisher:
pypi.yaml on szymonrychu/gitlab-cicd-python-wrapper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gitlab_cicd_python_wrapper-0.0.8.tar.gz -
Subject digest:
31a46b2e01a7bade829654b2ea9ef6f2a9144d77515ba3f43dccc5ad52fe3587 - Sigstore transparency entry: 1325465948
- Sigstore integration time:
-
Permalink:
szymonrychu/gitlab-cicd-python-wrapper@8236389b71d61ee0404b58f03d1ebdc6c1f7c0aa -
Branch / Tag:
refs/tags/0.0.8 - Owner: https://github.com/szymonrychu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yaml@8236389b71d61ee0404b58f03d1ebdc6c1f7c0aa -
Trigger Event:
push
-
Statement type:
File details
Details for the file gitlab_cicd_python_wrapper-0.0.8-py3-none-any.whl.
File metadata
- Download URL: gitlab_cicd_python_wrapper-0.0.8-py3-none-any.whl
- Upload date:
- Size: 19.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55ce5b1094405b1bb605053960a61dddc565c9077fab7e2f942d5da3d11c899a
|
|
| MD5 |
0f85d13b10b1497c926041ee24b826f0
|
|
| BLAKE2b-256 |
f79dafa8b4ad4fc5b0044f6000a272c4c2f0caad4906e4f609f9f8132324046f
|
Provenance
The following attestation bundles were made for gitlab_cicd_python_wrapper-0.0.8-py3-none-any.whl:
Publisher:
pypi.yaml on szymonrychu/gitlab-cicd-python-wrapper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gitlab_cicd_python_wrapper-0.0.8-py3-none-any.whl -
Subject digest:
55ce5b1094405b1bb605053960a61dddc565c9077fab7e2f942d5da3d11c899a - Sigstore transparency entry: 1325466099
- Sigstore integration time:
-
Permalink:
szymonrychu/gitlab-cicd-python-wrapper@8236389b71d61ee0404b58f03d1ebdc6c1f7c0aa -
Branch / Tag:
refs/tags/0.0.8 - Owner: https://github.com/szymonrychu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yaml@8236389b71d61ee0404b58f03d1ebdc6c1f7c0aa -
Trigger Event:
push
-
Statement type: