workflowforge 1.0b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of workflowforge might be problematic. Click here for more details.
- workflowforge/__init__.py +195 -0
- workflowforge/ai_documentation.py +137 -0
- workflowforge/codebuild.py +337 -0
- workflowforge/environment.py +25 -0
- workflowforge/jenkins.py +321 -0
- workflowforge/jenkins_plugins.py +171 -0
- workflowforge/job.py +102 -0
- workflowforge/schema_validation.py +137 -0
- workflowforge/secrets.py +153 -0
- workflowforge/step.py +106 -0
- workflowforge/strategy.py +59 -0
- workflowforge/templates.py +177 -0
- workflowforge/triggers.py +142 -0
- workflowforge/validation.py +73 -0
- workflowforge/visualization.py +200 -0
- workflowforge/workflow.py +152 -0
- workflowforge-1.0b1.dist-info/METADATA +336 -0
- workflowforge-1.0b1.dist-info/RECORD +22 -0
- workflowforge-1.0b1.dist-info/WHEEL +5 -0
- workflowforge-1.0b1.dist-info/licenses/AUTHORS.md +18 -0
- workflowforge-1.0b1.dist-info/licenses/LICENSE +21 -0
- workflowforge-1.0b1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""GitHub Actions schema validation."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from typing import Any, Dict, List
|
|
5
|
+
|
|
6
|
+
import yaml
|
|
7
|
+
|
|
8
|
+
from .validation import ValidationError
|
|
9
|
+
|
|
10
|
+
# Simplified GitHub Actions schema validation
|
|
11
|
+
GITHUB_ACTIONS_SCHEMA = {
|
|
12
|
+
"required_fields": ["name", "on", "jobs"],
|
|
13
|
+
"valid_triggers": [
|
|
14
|
+
"push",
|
|
15
|
+
"pull_request",
|
|
16
|
+
"release",
|
|
17
|
+
"schedule",
|
|
18
|
+
"workflow_dispatch",
|
|
19
|
+
"workflow_call",
|
|
20
|
+
"issues",
|
|
21
|
+
"issue_comment",
|
|
22
|
+
"create",
|
|
23
|
+
"delete",
|
|
24
|
+
],
|
|
25
|
+
"valid_job_fields": [
|
|
26
|
+
"runs-on",
|
|
27
|
+
"steps",
|
|
28
|
+
"needs",
|
|
29
|
+
"if",
|
|
30
|
+
"strategy",
|
|
31
|
+
"environment",
|
|
32
|
+
"permissions",
|
|
33
|
+
"outputs",
|
|
34
|
+
"env",
|
|
35
|
+
"defaults",
|
|
36
|
+
"timeout-minutes",
|
|
37
|
+
],
|
|
38
|
+
"valid_step_fields": [
|
|
39
|
+
"name",
|
|
40
|
+
"uses",
|
|
41
|
+
"run",
|
|
42
|
+
"with",
|
|
43
|
+
"env",
|
|
44
|
+
"if",
|
|
45
|
+
"id",
|
|
46
|
+
"continue-on-error",
|
|
47
|
+
],
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def validate_github_actions_schema(workflow_dict: dict[str, Any]) -> list[str]:
|
|
52
|
+
"""Validate workflow against GitHub Actions schema."""
|
|
53
|
+
errors = []
|
|
54
|
+
|
|
55
|
+
# Check required fields
|
|
56
|
+
for field in GITHUB_ACTIONS_SCHEMA["required_fields"]:
|
|
57
|
+
if field not in workflow_dict:
|
|
58
|
+
errors.append(f"Missing required field: {field}")
|
|
59
|
+
|
|
60
|
+
# Validate triggers
|
|
61
|
+
if "on" in workflow_dict:
|
|
62
|
+
on_value = workflow_dict["on"]
|
|
63
|
+
if isinstance(on_value, str):
|
|
64
|
+
if on_value not in GITHUB_ACTIONS_SCHEMA["valid_triggers"]:
|
|
65
|
+
errors.append(f"Invalid trigger: {on_value}")
|
|
66
|
+
elif isinstance(on_value, dict):
|
|
67
|
+
for trigger in on_value.keys():
|
|
68
|
+
if trigger not in GITHUB_ACTIONS_SCHEMA["valid_triggers"]:
|
|
69
|
+
errors.append(f"Invalid trigger: {trigger}")
|
|
70
|
+
|
|
71
|
+
# Validate jobs
|
|
72
|
+
if "jobs" in workflow_dict:
|
|
73
|
+
for job_name, job_config in workflow_dict["jobs"].items():
|
|
74
|
+
if not isinstance(job_config, dict):
|
|
75
|
+
errors.append(f"Job '{job_name}' must be an object")
|
|
76
|
+
continue
|
|
77
|
+
|
|
78
|
+
# Check required job fields
|
|
79
|
+
if "runs-on" not in job_config:
|
|
80
|
+
errors.append(f"Job '{job_name}' missing required 'runs-on' field")
|
|
81
|
+
|
|
82
|
+
# Validate job fields
|
|
83
|
+
for field in job_config.keys():
|
|
84
|
+
if field not in GITHUB_ACTIONS_SCHEMA["valid_job_fields"]:
|
|
85
|
+
errors.append(f"Invalid job field in '{job_name}': {field}")
|
|
86
|
+
|
|
87
|
+
# Validate steps
|
|
88
|
+
if "steps" in job_config:
|
|
89
|
+
for i, step in enumerate(job_config["steps"]):
|
|
90
|
+
if not isinstance(step, dict):
|
|
91
|
+
errors.append(f"Step {i} in job '{job_name}' must be an object")
|
|
92
|
+
continue
|
|
93
|
+
|
|
94
|
+
# Must have either 'uses' or 'run'
|
|
95
|
+
if "uses" not in step and "run" not in step:
|
|
96
|
+
errors.append(
|
|
97
|
+
f"Step {i} in job '{job_name}' must have either 'uses' or 'run'"
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
# Validate step fields
|
|
101
|
+
for field in step.keys():
|
|
102
|
+
if field not in GITHUB_ACTIONS_SCHEMA["valid_step_fields"]:
|
|
103
|
+
errors.append(
|
|
104
|
+
f"Invalid step field in job '{job_name}', step {i}: {field}"
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
return errors
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def validate_yaml_syntax(yaml_content: str) -> list[str]:
|
|
111
|
+
"""Validate YAML syntax."""
|
|
112
|
+
errors = []
|
|
113
|
+
try:
|
|
114
|
+
yaml.safe_load(yaml_content)
|
|
115
|
+
except yaml.YAMLError as e:
|
|
116
|
+
errors.append(f"YAML syntax error: {str(e)}")
|
|
117
|
+
return errors
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def validate_workflow_yaml(yaml_content: str) -> list[str]:
|
|
121
|
+
"""Complete workflow validation."""
|
|
122
|
+
errors = []
|
|
123
|
+
|
|
124
|
+
# First validate YAML syntax
|
|
125
|
+
yaml_errors = validate_yaml_syntax(yaml_content)
|
|
126
|
+
if yaml_errors:
|
|
127
|
+
return yaml_errors
|
|
128
|
+
|
|
129
|
+
# Parse and validate structure
|
|
130
|
+
try:
|
|
131
|
+
workflow_dict = yaml.safe_load(yaml_content)
|
|
132
|
+
schema_errors = validate_github_actions_schema(workflow_dict)
|
|
133
|
+
errors.extend(schema_errors)
|
|
134
|
+
except Exception as e:
|
|
135
|
+
errors.append(f"Validation error: {str(e)}")
|
|
136
|
+
|
|
137
|
+
return errors
|
workflowforge/secrets.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""Secrets and variables support for all platforms."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# GitHub Actions
|
|
9
|
+
class Secret(BaseModel):
|
|
10
|
+
"""Represents a GitHub Actions secret reference."""
|
|
11
|
+
|
|
12
|
+
name: str = Field(..., description="Secret name")
|
|
13
|
+
|
|
14
|
+
def __str__(self) -> str:
|
|
15
|
+
return f"${{{{ secrets.{self.name} }}}}"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Variable(BaseModel):
|
|
19
|
+
"""Represents a GitHub Actions variable reference."""
|
|
20
|
+
|
|
21
|
+
name: str = Field(..., description="Variable name")
|
|
22
|
+
|
|
23
|
+
def __str__(self) -> str:
|
|
24
|
+
return f"${{{{ vars.{self.name} }}}}"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class GitHubContext(BaseModel):
|
|
28
|
+
"""Represents GitHub context variables."""
|
|
29
|
+
|
|
30
|
+
@staticmethod
|
|
31
|
+
def event_name() -> str:
|
|
32
|
+
return "${{ github.event_name }}"
|
|
33
|
+
|
|
34
|
+
@staticmethod
|
|
35
|
+
def ref() -> str:
|
|
36
|
+
return "${{ github.ref }}"
|
|
37
|
+
|
|
38
|
+
@staticmethod
|
|
39
|
+
def sha() -> str:
|
|
40
|
+
return "${{ github.sha }}"
|
|
41
|
+
|
|
42
|
+
@staticmethod
|
|
43
|
+
def actor() -> str:
|
|
44
|
+
return "${{ github.actor }}"
|
|
45
|
+
|
|
46
|
+
@staticmethod
|
|
47
|
+
def repository() -> str:
|
|
48
|
+
return "${{ github.repository }}"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
# Jenkins
|
|
52
|
+
class JenkinsCredential(BaseModel):
|
|
53
|
+
"""Represents a Jenkins credential reference."""
|
|
54
|
+
|
|
55
|
+
id: str = Field(..., description="Credential ID")
|
|
56
|
+
|
|
57
|
+
def __str__(self) -> str:
|
|
58
|
+
return f"credentials('{self.id}')"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class JenkinsEnvVar(BaseModel):
|
|
62
|
+
"""Represents a Jenkins environment variable."""
|
|
63
|
+
|
|
64
|
+
name: str = Field(..., description="Environment variable name")
|
|
65
|
+
|
|
66
|
+
def __str__(self) -> str:
|
|
67
|
+
return f"env.{self.name}"
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class JenkinsParam(BaseModel):
|
|
71
|
+
"""Represents a Jenkins parameter."""
|
|
72
|
+
|
|
73
|
+
name: str = Field(..., description="Parameter name")
|
|
74
|
+
|
|
75
|
+
def __str__(self) -> str:
|
|
76
|
+
return f"params.{self.name}"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# AWS CodeBuild
|
|
80
|
+
class CodeBuildSecret(BaseModel):
|
|
81
|
+
"""Represents an AWS CodeBuild secret from Secrets Manager."""
|
|
82
|
+
|
|
83
|
+
name: str = Field(..., description="Secret name")
|
|
84
|
+
|
|
85
|
+
def __str__(self) -> str:
|
|
86
|
+
return f"${self.name}"
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class CodeBuildParameter(BaseModel):
|
|
90
|
+
"""Represents an AWS CodeBuild parameter from Parameter Store."""
|
|
91
|
+
|
|
92
|
+
name: str = Field(..., description="Parameter name")
|
|
93
|
+
|
|
94
|
+
def __str__(self) -> str:
|
|
95
|
+
return f"${self.name}"
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class CodeBuildEnvVar(BaseModel):
|
|
99
|
+
"""Represents an AWS CodeBuild environment variable."""
|
|
100
|
+
|
|
101
|
+
name: str = Field(..., description="Environment variable name")
|
|
102
|
+
|
|
103
|
+
def __str__(self) -> str:
|
|
104
|
+
return f"${self.name}"
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
# Factory functions
|
|
108
|
+
# GitHub Actions
|
|
109
|
+
def secret(name: str) -> str:
|
|
110
|
+
"""Create a GitHub Actions secret reference."""
|
|
111
|
+
return str(Secret(name=name))
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def variable(name: str) -> str:
|
|
115
|
+
"""Create a GitHub Actions variable reference."""
|
|
116
|
+
return str(Variable(name=name))
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def github_context() -> GitHubContext:
|
|
120
|
+
"""Get GitHub context helper."""
|
|
121
|
+
return GitHubContext()
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
# Jenkins
|
|
125
|
+
def jenkins_credential(id: str) -> str:
|
|
126
|
+
"""Create a Jenkins credential reference."""
|
|
127
|
+
return str(JenkinsCredential(id=id))
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def jenkins_env(name: str) -> str:
|
|
131
|
+
"""Create a Jenkins environment variable reference."""
|
|
132
|
+
return str(JenkinsEnvVar(name=name))
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def jenkins_param(name: str) -> str:
|
|
136
|
+
"""Create a Jenkins parameter reference."""
|
|
137
|
+
return str(JenkinsParam(name=name))
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
# AWS CodeBuild
|
|
141
|
+
def codebuild_secret(name: str) -> str:
|
|
142
|
+
"""Create a CodeBuild Secrets Manager reference."""
|
|
143
|
+
return str(CodeBuildSecret(name=name))
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def codebuild_parameter(name: str) -> str:
|
|
147
|
+
"""Create a CodeBuild Parameter Store reference."""
|
|
148
|
+
return str(CodeBuildParameter(name=name))
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def codebuild_env(name: str) -> str:
|
|
152
|
+
"""Create a CodeBuild environment variable reference."""
|
|
153
|
+
return str(CodeBuildEnvVar(name=name))
|
workflowforge/step.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Definición de Steps para jobs de GitHub Actions."""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from typing import Any, Dict, Optional, Union
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel, Field
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Step(BaseModel, ABC):
|
|
10
|
+
"""Clase base abstracta para steps."""
|
|
11
|
+
|
|
12
|
+
name: str | None = Field(None, description="Nombre del step")
|
|
13
|
+
id: str | None = Field(None, description="ID único del step")
|
|
14
|
+
if_condition: str | None = Field(
|
|
15
|
+
None, alias="if", description="Condición para ejecutar el step"
|
|
16
|
+
)
|
|
17
|
+
continue_on_error: bool | None = Field(None, description="Continuar si hay error")
|
|
18
|
+
timeout_minutes: int | None = Field(None, description="Timeout en minutos")
|
|
19
|
+
env: dict[str, str] | None = Field(
|
|
20
|
+
None, description="Variables de entorno del step"
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def to_dict(self) -> dict[str, Any]:
|
|
25
|
+
"""Convierte el step a diccionario para YAML."""
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ActionStep(Step):
|
|
30
|
+
"""Step que ejecuta una action."""
|
|
31
|
+
|
|
32
|
+
uses: str = Field(..., description="Action a usar")
|
|
33
|
+
with_params: dict[str, Any] | None = Field(
|
|
34
|
+
None, description="Parámetros de la action"
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
def to_dict(self) -> dict[str, Any]:
|
|
38
|
+
"""Convierte el ActionStep a diccionario."""
|
|
39
|
+
result = {"uses": self.uses}
|
|
40
|
+
|
|
41
|
+
if self.name:
|
|
42
|
+
result["name"] = self.name
|
|
43
|
+
if self.id:
|
|
44
|
+
result["id"] = self.id
|
|
45
|
+
if self.if_condition:
|
|
46
|
+
result["if"] = self.if_condition
|
|
47
|
+
if self.continue_on_error is not None:
|
|
48
|
+
result["continue-on-error"] = self.continue_on_error
|
|
49
|
+
if self.timeout_minutes:
|
|
50
|
+
result["timeout-minutes"] = self.timeout_minutes
|
|
51
|
+
if self.env:
|
|
52
|
+
result["env"] = self.env
|
|
53
|
+
if self.with_params:
|
|
54
|
+
result["with"] = self.with_params
|
|
55
|
+
|
|
56
|
+
return result
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class RunStep(Step):
|
|
60
|
+
"""Step que ejecuta comandos shell."""
|
|
61
|
+
|
|
62
|
+
run: str = Field(..., description="Comando a ejecutar")
|
|
63
|
+
shell: str | None = Field(None, description="Shell a usar")
|
|
64
|
+
working_directory: str | None = Field(
|
|
65
|
+
None, alias="working-directory", description="Directorio de trabajo"
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
def to_dict(self) -> dict[str, Any]:
|
|
69
|
+
"""Convierte el RunStep a diccionario."""
|
|
70
|
+
result = {"run": self.run}
|
|
71
|
+
|
|
72
|
+
if self.name:
|
|
73
|
+
result["name"] = self.name
|
|
74
|
+
if self.id:
|
|
75
|
+
result["id"] = self.id
|
|
76
|
+
if self.if_condition:
|
|
77
|
+
result["if"] = self.if_condition
|
|
78
|
+
if self.continue_on_error is not None:
|
|
79
|
+
result["continue-on-error"] = self.continue_on_error
|
|
80
|
+
if self.timeout_minutes:
|
|
81
|
+
result["timeout-minutes"] = self.timeout_minutes
|
|
82
|
+
if self.env:
|
|
83
|
+
result["env"] = self.env
|
|
84
|
+
if self.shell:
|
|
85
|
+
result["shell"] = self.shell
|
|
86
|
+
if self.working_directory:
|
|
87
|
+
result["working-directory"] = self.working_directory
|
|
88
|
+
|
|
89
|
+
return result
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
# Factory functions para crear steps fácilmente
|
|
93
|
+
def action(
|
|
94
|
+
uses: str,
|
|
95
|
+
name: str | None = None,
|
|
96
|
+
with_: dict[str, Any] | None = None,
|
|
97
|
+
**kwargs,
|
|
98
|
+
) -> ActionStep:
|
|
99
|
+
"""Crea un ActionStep de manera conveniente."""
|
|
100
|
+
with_params = with_ if with_ is not None else (kwargs if kwargs else None)
|
|
101
|
+
return ActionStep(uses=uses, name=name, with_params=with_params)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def run(command: str, name: str | None = None, shell: str | None = None) -> RunStep:
|
|
105
|
+
"""Crea un RunStep de manera conveniente."""
|
|
106
|
+
return RunStep(run=command, name=name, shell=shell) # nosec B604
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Definición de estrategias y matrices para jobs."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, List, Optional, Union
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Matrix(BaseModel):
|
|
9
|
+
"""Representa una matriz de build."""
|
|
10
|
+
|
|
11
|
+
model_config = {"extra": "allow"}
|
|
12
|
+
|
|
13
|
+
include: list[dict[str, Any]] | None = Field(
|
|
14
|
+
None, description="Configuraciones adicionales"
|
|
15
|
+
)
|
|
16
|
+
exclude: list[dict[str, Any]] | None = Field(
|
|
17
|
+
None, description="Configuraciones a excluir"
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
def model_dump(self, **kwargs) -> dict[str, Any]:
|
|
21
|
+
"""Serializa la matriz incluyendo variables dinámicas."""
|
|
22
|
+
result = super().model_dump(**kwargs)
|
|
23
|
+
return {k: v for k, v in result.items() if v is not None}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Strategy(BaseModel):
|
|
27
|
+
"""Representa una estrategia de ejecución para jobs."""
|
|
28
|
+
|
|
29
|
+
matrix: Matrix | None = Field(None, description="Matriz de configuraciones")
|
|
30
|
+
fail_fast: bool | None = Field(None, description="Fallar rápido si hay error")
|
|
31
|
+
max_parallel: int | None = Field(None, description="Máximo de jobs paralelos")
|
|
32
|
+
|
|
33
|
+
def model_dump(self, **kwargs) -> dict[str, Any]:
|
|
34
|
+
"""Serializa la estrategia."""
|
|
35
|
+
result = {}
|
|
36
|
+
|
|
37
|
+
if self.matrix:
|
|
38
|
+
result["matrix"] = self.matrix.model_dump(**kwargs)
|
|
39
|
+
if self.fail_fast is not None:
|
|
40
|
+
result["fail-fast"] = self.fail_fast
|
|
41
|
+
if self.max_parallel is not None:
|
|
42
|
+
result["max-parallel"] = self.max_parallel
|
|
43
|
+
|
|
44
|
+
return result
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# Factory functions para crear estrategias fácilmente
|
|
48
|
+
def matrix(**variables) -> Matrix:
|
|
49
|
+
"""Crea una Matrix de manera conveniente."""
|
|
50
|
+
return Matrix(**variables)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def strategy(
|
|
54
|
+
matrix: Matrix | None = None,
|
|
55
|
+
fail_fast: bool | None = None,
|
|
56
|
+
max_parallel: int | None = None,
|
|
57
|
+
) -> Strategy:
|
|
58
|
+
"""Crea una Strategy de manera conveniente."""
|
|
59
|
+
return Strategy(matrix=matrix, fail_fast=fail_fast, max_parallel=max_parallel)
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"""Predefined templates for common workflows."""
|
|
2
|
+
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
|
|
5
|
+
from .job import Job
|
|
6
|
+
from .secrets import secret
|
|
7
|
+
from .step import action, run
|
|
8
|
+
from .strategy import matrix, strategy
|
|
9
|
+
from .triggers import on_pull_request, on_push, on_release
|
|
10
|
+
from .workflow import Workflow
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def python_ci_template(
|
|
14
|
+
name: str = "Python CI",
|
|
15
|
+
python_versions: list[str] = ["3.11", "3.12", "3.13"],
|
|
16
|
+
branches: list[str] = ["main"],
|
|
17
|
+
) -> Workflow:
|
|
18
|
+
"""Template for Python CI workflow."""
|
|
19
|
+
workflow = Workflow(
|
|
20
|
+
name=name, on=[on_push(branches=branches), on_pull_request(branches=branches)]
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
# Test job with matrix
|
|
24
|
+
test_job = Job(
|
|
25
|
+
runs_on="ubuntu-latest",
|
|
26
|
+
strategy=strategy(matrix=matrix(python_version=python_versions)),
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
test_job.add_step(action("actions/checkout@v4", name="Checkout code"))
|
|
30
|
+
test_job.add_step(
|
|
31
|
+
action(
|
|
32
|
+
"actions/setup-python@v5",
|
|
33
|
+
name="Set up Python ${{ matrix.python-version }}",
|
|
34
|
+
with_={"python-version": "${{ matrix.python-version }}"},
|
|
35
|
+
)
|
|
36
|
+
)
|
|
37
|
+
test_job.add_step(run("pip install -e .[dev]", name="Install dependencies"))
|
|
38
|
+
test_job.add_step(run("pytest", name="Run tests"))
|
|
39
|
+
|
|
40
|
+
workflow.add_job("test", test_job)
|
|
41
|
+
return workflow
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def docker_build_template(
|
|
45
|
+
name: str = "Docker Build",
|
|
46
|
+
image_name: str = "my-app",
|
|
47
|
+
dockerfile_path: str = "Dockerfile",
|
|
48
|
+
) -> Workflow:
|
|
49
|
+
"""Template for Docker build workflow."""
|
|
50
|
+
workflow = Workflow(name=name, on=[on_push(), on_pull_request()])
|
|
51
|
+
|
|
52
|
+
build_job = Job(runs_on="ubuntu-latest")
|
|
53
|
+
build_job.add_step(action("actions/checkout@v4", name="Checkout code"))
|
|
54
|
+
build_job.add_step(
|
|
55
|
+
action("docker/setup-buildx-action@v3", name="Set up Docker Buildx")
|
|
56
|
+
)
|
|
57
|
+
build_job.add_step(
|
|
58
|
+
action(
|
|
59
|
+
"docker/login-action@v3",
|
|
60
|
+
name="Login to Docker Hub",
|
|
61
|
+
with_={
|
|
62
|
+
"username": "${{ secrets.DOCKER_USERNAME }}",
|
|
63
|
+
"password": secret("DOCKER_PASSWORD"),
|
|
64
|
+
},
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
build_job.add_step(
|
|
68
|
+
action(
|
|
69
|
+
"docker/build-push-action@v5",
|
|
70
|
+
name="Build and push",
|
|
71
|
+
with_={
|
|
72
|
+
"context": ".",
|
|
73
|
+
"file": dockerfile_path,
|
|
74
|
+
"push": "true",
|
|
75
|
+
"tags": f"{image_name}:latest",
|
|
76
|
+
},
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
workflow.add_job("build", build_job)
|
|
81
|
+
return workflow
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def node_ci_template(
|
|
85
|
+
name: str = "Node.js CI",
|
|
86
|
+
node_versions: list[str] = ["18", "20"],
|
|
87
|
+
package_manager: str = "npm",
|
|
88
|
+
) -> Workflow:
|
|
89
|
+
"""Template for Node.js CI workflow."""
|
|
90
|
+
workflow = Workflow(name=name, on=[on_push(), on_pull_request()])
|
|
91
|
+
|
|
92
|
+
test_job = Job(
|
|
93
|
+
runs_on="ubuntu-latest",
|
|
94
|
+
strategy=strategy(matrix=matrix(node_version=node_versions)),
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
test_job.add_step(action("actions/checkout@v4", name="Checkout code"))
|
|
98
|
+
test_job.add_step(
|
|
99
|
+
action(
|
|
100
|
+
"actions/setup-node@v4",
|
|
101
|
+
name="Setup Node.js ${{ matrix.node-version }}",
|
|
102
|
+
with_={
|
|
103
|
+
"node-version": "${{ matrix.node-version }}",
|
|
104
|
+
"cache": package_manager,
|
|
105
|
+
},
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
if package_manager == "npm":
|
|
110
|
+
test_job.add_step(run("npm ci", name="Install dependencies"))
|
|
111
|
+
test_job.add_step(run("npm test", name="Run tests"))
|
|
112
|
+
elif package_manager == "yarn":
|
|
113
|
+
test_job.add_step(
|
|
114
|
+
run("yarn install --frozen-lockfile", name="Install dependencies")
|
|
115
|
+
)
|
|
116
|
+
test_job.add_step(run("yarn test", name="Run tests"))
|
|
117
|
+
|
|
118
|
+
workflow.add_job("test", test_job)
|
|
119
|
+
return workflow
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def release_template(
|
|
123
|
+
name: str = "Release",
|
|
124
|
+
build_command: str = "python -m build",
|
|
125
|
+
test_command: str | None = "pytest",
|
|
126
|
+
) -> Workflow:
|
|
127
|
+
"""Template for release workflow with PyPI publishing."""
|
|
128
|
+
workflow = Workflow(name=name, on=[on_release(types=["published"])])
|
|
129
|
+
|
|
130
|
+
# Test job
|
|
131
|
+
if test_command:
|
|
132
|
+
test_job = Job(runs_on="ubuntu-latest")
|
|
133
|
+
test_job.add_step(action("actions/checkout@v4", name="Checkout code"))
|
|
134
|
+
test_job.add_step(action("actions/setup-python@v5", name="Setup Python"))
|
|
135
|
+
test_job.add_step(run("pip install -e .[dev]", name="Install dependencies"))
|
|
136
|
+
test_job.add_step(run(test_command, name="Run tests"))
|
|
137
|
+
workflow.add_job("test", test_job)
|
|
138
|
+
|
|
139
|
+
# Build job
|
|
140
|
+
build_job = Job(runs_on="ubuntu-latest")
|
|
141
|
+
if test_command:
|
|
142
|
+
build_job.set_needs("test")
|
|
143
|
+
|
|
144
|
+
build_job.add_step(action("actions/checkout@v4", name="Checkout code"))
|
|
145
|
+
build_job.add_step(action("actions/setup-python@v5", name="Setup Python"))
|
|
146
|
+
build_job.add_step(run("pip install build", name="Install build tools"))
|
|
147
|
+
build_job.add_step(run(build_command, name="Build package"))
|
|
148
|
+
build_job.add_step(
|
|
149
|
+
action(
|
|
150
|
+
"actions/upload-artifact@v4",
|
|
151
|
+
name="Upload artifacts",
|
|
152
|
+
with_={"name": "dist", "path": "dist/"},
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
# Publish job
|
|
157
|
+
publish_job = Job(
|
|
158
|
+
runs_on="ubuntu-latest",
|
|
159
|
+
environment={"name": "pypi", "url": "https://pypi.org/p/<package-name>"},
|
|
160
|
+
permissions={"id-token": "write"},
|
|
161
|
+
)
|
|
162
|
+
publish_job.set_needs("build")
|
|
163
|
+
|
|
164
|
+
publish_job.add_step(
|
|
165
|
+
action(
|
|
166
|
+
"actions/download-artifact@v4",
|
|
167
|
+
name="Download artifacts",
|
|
168
|
+
with_={"name": "dist", "path": "dist/"},
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
publish_job.add_step(
|
|
172
|
+
action("pypa/gh-action-pypi-publish@release/v1", name="Publish to PyPI")
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
workflow.add_job("build", build_job)
|
|
176
|
+
workflow.add_job("publish", publish_job)
|
|
177
|
+
return workflow
|