Skip to content

Commit 7edf2e2

Browse files
authored
feat: Add official GitHub Action for CI/CD integration (#2683)
* feat: Add official GitHub Action for datamodel-code-generator with usage examples and inputs documentation * feat: Refactor GitHub Action script to streamline argument handling and improve readability * feat: Add 'extras' input to GitHub Action for optional dependency installation with usage examples * feat: Simplify argument handling in GitHub Action by using an array for datamodel-codegen command
1 parent e70273a commit 7edf2e2

3 files changed

Lines changed: 252 additions & 3 deletions

File tree

action.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: 'datamodel-code-generator'
2+
description: 'Generate Python data models from schema definitions (OpenAPI, JSON Schema, GraphQL, and more)'
3+
branding:
4+
icon: 'code'
5+
color: 'blue'
6+
7+
inputs:
8+
input:
9+
description: 'Input schema file or directory'
10+
required: true
11+
output:
12+
description: 'Output file or directory'
13+
required: true
14+
input-file-type:
15+
description: 'Input file type (openapi, jsonschema, json, yaml, csv, graphql)'
16+
required: true
17+
output-model-type:
18+
description: 'Output model type (pydantic_v2.BaseModel, pydantic.BaseModel, dataclasses.dataclass, typing.TypedDict, msgspec.Struct)'
19+
required: true
20+
check:
21+
description: 'Validate that existing output is up to date (no generation)'
22+
required: false
23+
default: 'true'
24+
working-directory:
25+
description: 'Working directory (where pyproject.toml is located)'
26+
required: false
27+
default: '.'
28+
profile:
29+
description: 'Named profile from pyproject.toml [tool.datamodel-codegen.profiles.<name>]'
30+
required: false
31+
default: ''
32+
extra-args:
33+
description: 'Additional CLI arguments'
34+
required: false
35+
default: ''
36+
version:
37+
description: 'Specific version to install (defaults to action tag version if tag looks like a version, otherwise latest)'
38+
required: false
39+
default: ''
40+
extras:
41+
description: 'Optional extras to install (comma-separated: graphql, http, validation, ruff, all)'
42+
required: false
43+
default: ''
44+
45+
runs:
46+
using: 'composite'
47+
steps:
48+
- uses: actions/setup-python@v6
49+
with:
50+
python-version: '3.14'
51+
52+
- uses: actions/cache@v5
53+
with:
54+
path: ~/.cache/pip
55+
key: datamodel-codegen-${{ runner.os }}
56+
57+
- run: |
58+
EXTRAS=""
59+
if [ -n "${{ inputs.extras }}" ]; then
60+
EXTRAS="[${{ inputs.extras }}]"
61+
fi
62+
63+
if [ -n "${{ inputs.version }}" ]; then
64+
pip install "datamodel-code-generator${EXTRAS}==${{ inputs.version }}"
65+
elif [[ "${{ github.action_ref }}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
66+
pip install "datamodel-code-generator${EXTRAS}==${{ github.action_ref }}"
67+
else
68+
pip install "datamodel-code-generator${EXTRAS}"
69+
fi
70+
shell: bash
71+
72+
- run: |
73+
ARGS=(
74+
--input "${{ inputs.input }}"
75+
--output "${{ inputs.output }}"
76+
--input-file-type "${{ inputs.input-file-type }}"
77+
--output-model-type "${{ inputs.output-model-type }}"
78+
)
79+
80+
if [ "${{ inputs.check }}" = "true" ]; then
81+
ARGS+=(--check)
82+
fi
83+
84+
if [ -n "${{ inputs.profile }}" ]; then
85+
ARGS+=(--profile "${{ inputs.profile }}")
86+
fi
87+
88+
datamodel-codegen "${ARGS[@]}" ${{ inputs.extra-args }}
89+
shell: bash
90+
working-directory: ${{ inputs.working-directory }}

docs/ci-cd.md

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,165 @@ This guide covers how to use datamodel-code-generator in CI/CD pipelines and dev
99

1010
---
1111

12+
## Official GitHub Action
13+
14+
The official GitHub Action provides a simple way to validate generated models in your CI pipeline.
15+
16+
### Basic Usage
17+
18+
```yaml
19+
- uses: koxudaxi/datamodel-code-generator@0.44.0
20+
with:
21+
input: schema.yaml
22+
output: src/models.py
23+
input-file-type: openapi
24+
output-model-type: pydantic_v2.BaseModel
25+
```
26+
27+
By default, the action runs in **check mode** (`--check`), which validates that the existing output file matches what would be generated. If they differ, the action fails.
28+
29+
### Inputs
30+
31+
| Input | Required | Default | Description |
32+
|-------|----------|---------|-------------|
33+
| `input` | Yes | - | Input schema file or directory |
34+
| `output` | Yes | - | Output file or directory |
35+
| `input-file-type` | Yes | - | Input file type (`openapi`, `jsonschema`, `json`, `yaml`, `csv`, `graphql`) |
36+
| `output-model-type` | Yes | - | Output model type (`pydantic_v2.BaseModel`, `pydantic.BaseModel`, `dataclasses.dataclass`, `typing.TypedDict`, `msgspec.Struct`) |
37+
| `check` | No | `true` | Validate that existing output is up to date (no generation) |
38+
| `working-directory` | No | `.` | Working directory (where `pyproject.toml` is located) |
39+
| `profile` | No | - | Named profile from `pyproject.toml` |
40+
| `extra-args` | No | - | Additional CLI arguments |
41+
| `version` | No | - | Specific version to install (defaults to action's tag version) |
42+
| `extras` | No | - | Optional extras to install (comma-separated: `graphql`, `http`, `validation`, `ruff`, `all`) |
43+
44+
### Example: Validate on Pull Request
45+
46+
```yaml title=".github/workflows/validate-models.yml"
47+
name: Validate Generated Models
48+
49+
on:
50+
pull_request:
51+
paths:
52+
- 'schemas/**'
53+
- 'src/models/**'
54+
55+
jobs:
56+
validate:
57+
runs-on: ubuntu-latest
58+
steps:
59+
- uses: actions/checkout@v4
60+
61+
- uses: koxudaxi/datamodel-code-generator@0.44.0
62+
with:
63+
input: schemas/api.yaml
64+
output: src/models/api.py
65+
input-file-type: openapi
66+
output-model-type: pydantic_v2.BaseModel
67+
```
68+
69+
### Example: Monorepo with Multiple Schemas
70+
71+
```yaml title=".github/workflows/validate-models.yml"
72+
jobs:
73+
validate:
74+
runs-on: ubuntu-latest
75+
strategy:
76+
matrix:
77+
include:
78+
- working-directory: packages/api
79+
input: schemas/openapi.yaml
80+
output: src/models.py
81+
input-file-type: openapi
82+
- working-directory: packages/admin
83+
input: schemas/schema.json
84+
output: src/models.py
85+
input-file-type: jsonschema
86+
steps:
87+
- uses: actions/checkout@v4
88+
89+
- uses: koxudaxi/datamodel-code-generator@0.44.0
90+
with:
91+
input: ${{ matrix.input }}
92+
output: ${{ matrix.output }}
93+
input-file-type: ${{ matrix.input-file-type }}
94+
output-model-type: pydantic_v2.BaseModel
95+
working-directory: ${{ matrix.working-directory }}
96+
```
97+
98+
### Example: Using Profiles
99+
100+
```yaml
101+
- uses: koxudaxi/datamodel-code-generator@0.44.0
102+
with:
103+
input: schemas/api.yaml
104+
output: src/models.py
105+
input-file-type: openapi
106+
output-model-type: pydantic_v2.BaseModel
107+
profile: api
108+
```
109+
110+
### Example: Generate Models (Instead of Validation)
111+
112+
Set `check: 'false'` to actually generate the models:
113+
114+
```yaml
115+
- uses: koxudaxi/datamodel-code-generator@0.44.0
116+
with:
117+
input: schema.yaml
118+
output: src/models.py
119+
input-file-type: openapi
120+
output-model-type: pydantic_v2.BaseModel
121+
check: 'false'
122+
```
123+
124+
### Example: GraphQL Schema
125+
126+
For GraphQL schemas, use the `extras` input to install the required dependency:
127+
128+
```yaml
129+
- uses: koxudaxi/datamodel-code-generator@0.44.0
130+
with:
131+
input: schema.graphql
132+
output: src/models.py
133+
input-file-type: graphql
134+
output-model-type: pydantic_v2.BaseModel
135+
extras: 'graphql'
136+
```
137+
138+
### Example: Multiple Extras
139+
140+
You can install multiple extras with comma-separated values:
141+
142+
```yaml
143+
- uses: koxudaxi/datamodel-code-generator@0.44.0
144+
with:
145+
input: schema.yaml
146+
output: src/models.py
147+
input-file-type: openapi
148+
output-model-type: pydantic_v2.BaseModel
149+
extras: 'http,validation,ruff'
150+
```
151+
152+
### Example: Additional CLI Options
153+
154+
Use `extra-args` for CLI options not covered by the inputs:
155+
156+
```yaml
157+
- uses: koxudaxi/datamodel-code-generator@0.44.0
158+
with:
159+
input: schema.yaml
160+
output: src/models.py
161+
input-file-type: openapi
162+
output-model-type: pydantic_v2.BaseModel
163+
extra-args: '--snake-case-field --field-constraints'
164+
```
165+
166+
!!! tip "Version Pinning"
167+
Always pin the action to a specific version tag (e.g., `@0.44.0`) to ensure reproducible builds. The action installs the same version of `datamodel-code-generator` as the tag.
168+
169+
---
170+
12171
## The `--check` Flag
13172

14173
The `--check` flag verifies that generated code matches existing files without modifying them. If the output would differ, it exits with a non-zero status code.
@@ -86,7 +245,7 @@ jobs:
86245
- uses: actions/checkout@v4
87246
88247
- name: Set up Python
89-
uses: actions/setup-python@v5
248+
uses: actions/setup-python@v6
90249
with:
91250
python-version: "3.14"
92251

docs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ Verify generated code stays in sync with schemas using `--check`:
150150
datamodel-codegen --input schema.yaml --output models.py --disable-timestamp --check
151151
```
152152

153-
See [CI/CD Integration](ci-cd.md) for GitHub Actions, pre-commit hooks, and more.
153+
See [CI/CD Integration](ci-cd.md) for GitHub Actions and more.
154154

155155
---
156156

@@ -159,7 +159,7 @@ See [CI/CD Integration](ci-cd.md) for GitHub Actions, pre-commit hooks, and more
159159
- 🖥️ **[CLI Reference](cli-reference/index.md)** - All command-line options with examples
160160
- ⚙️ **[pyproject.toml Configuration](pyproject_toml.md)** - Configure via pyproject.toml
161161
- 🚀 **[One-liner Usage](oneliner.md)** - uvx, pipx, clipboard integration
162-
- 🔄 **[CI/CD Integration](ci-cd.md)** - GitHub Actions, pre-commit hooks, and CI validation
162+
- 🔄 **[CI/CD Integration](ci-cd.md)** - GitHub Actions and CI validation
163163
- 🎨 **[Custom Templates](custom_template.md)** - Customize generated code with Jinja2
164164
- 🖌️ **[Code Formatting](formatting.md)** - Configure black, isort, and ruff
165165
-**[FAQ](faq.md)** - Common questions and troubleshooting

0 commit comments

Comments
 (0)