workflowforge 1.0.0__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.
- workflowforge/__init__.py +18 -0
- workflowforge/ai_documentation.py +138 -0
- workflowforge/codebuild.py +348 -0
- workflowforge/environment.py +25 -0
- workflowforge/github_actions_module.py +71 -0
- workflowforge/jenkins.py +323 -0
- workflowforge/jenkins_plugins.py +172 -0
- workflowforge/job.py +102 -0
- workflowforge/schema_validation.py +138 -0
- workflowforge/secrets.py +151 -0
- workflowforge/step.py +101 -0
- workflowforge/strategy.py +59 -0
- workflowforge/templates.py +177 -0
- workflowforge/triggers.py +139 -0
- workflowforge/validation.py +75 -0
- workflowforge/visualization.py +204 -0
- workflowforge/workflow.py +161 -0
- workflowforge-1.0.0.dist-info/METADATA +430 -0
- workflowforge-1.0.0.dist-info/RECORD +23 -0
- workflowforge-1.0.0.dist-info/WHEEL +5 -0
- workflowforge-1.0.0.dist-info/licenses/AUTHORS.md +18 -0
- workflowforge-1.0.0.dist-info/licenses/LICENSE +21 -0
- workflowforge-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"""GitHub Actions schema validation."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from ruamel.yaml import YAML
|
|
6
|
+
|
|
7
|
+
# Simplified GitHub Actions schema validation
|
|
8
|
+
GITHUB_ACTIONS_SCHEMA = {
|
|
9
|
+
"required_fields": ["name", "on", "jobs"],
|
|
10
|
+
"valid_triggers": [
|
|
11
|
+
"push",
|
|
12
|
+
"pull_request",
|
|
13
|
+
"release",
|
|
14
|
+
"schedule",
|
|
15
|
+
"workflow_dispatch",
|
|
16
|
+
"workflow_call",
|
|
17
|
+
"issues",
|
|
18
|
+
"issue_comment",
|
|
19
|
+
"create",
|
|
20
|
+
"delete",
|
|
21
|
+
],
|
|
22
|
+
"valid_job_fields": [
|
|
23
|
+
"runs-on",
|
|
24
|
+
"steps",
|
|
25
|
+
"needs",
|
|
26
|
+
"if",
|
|
27
|
+
"strategy",
|
|
28
|
+
"environment",
|
|
29
|
+
"permissions",
|
|
30
|
+
"outputs",
|
|
31
|
+
"env",
|
|
32
|
+
"defaults",
|
|
33
|
+
"timeout-minutes",
|
|
34
|
+
],
|
|
35
|
+
"valid_step_fields": [
|
|
36
|
+
"name",
|
|
37
|
+
"uses",
|
|
38
|
+
"run",
|
|
39
|
+
"with",
|
|
40
|
+
"env",
|
|
41
|
+
"if",
|
|
42
|
+
"id",
|
|
43
|
+
"continue-on-error",
|
|
44
|
+
],
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def validate_github_actions_schema(workflow_dict: dict[str, Any]) -> list[str]:
|
|
49
|
+
"""Validate workflow against GitHub Actions schema."""
|
|
50
|
+
errors = []
|
|
51
|
+
|
|
52
|
+
# Check required fields
|
|
53
|
+
for field in GITHUB_ACTIONS_SCHEMA["required_fields"]:
|
|
54
|
+
if field not in workflow_dict:
|
|
55
|
+
errors.append(f"Missing required field: {field}")
|
|
56
|
+
|
|
57
|
+
# Validate triggers
|
|
58
|
+
if "on" in workflow_dict:
|
|
59
|
+
on_value = workflow_dict["on"]
|
|
60
|
+
if isinstance(on_value, str):
|
|
61
|
+
if on_value not in GITHUB_ACTIONS_SCHEMA["valid_triggers"]:
|
|
62
|
+
errors.append(f"Invalid trigger: {on_value}")
|
|
63
|
+
elif isinstance(on_value, dict):
|
|
64
|
+
for trigger in on_value.keys():
|
|
65
|
+
if trigger not in GITHUB_ACTIONS_SCHEMA["valid_triggers"]:
|
|
66
|
+
errors.append(f"Invalid trigger: {trigger}")
|
|
67
|
+
|
|
68
|
+
# Validate jobs
|
|
69
|
+
if "jobs" in workflow_dict:
|
|
70
|
+
for job_name, job_config in workflow_dict["jobs"].items():
|
|
71
|
+
if not isinstance(job_config, dict):
|
|
72
|
+
errors.append(f"Job '{job_name}' must be an object")
|
|
73
|
+
continue
|
|
74
|
+
|
|
75
|
+
# Check required job fields
|
|
76
|
+
if "runs-on" not in job_config:
|
|
77
|
+
errors.append(f"Job '{job_name}' missing required 'runs-on' field")
|
|
78
|
+
|
|
79
|
+
# Validate job fields
|
|
80
|
+
for field in job_config.keys():
|
|
81
|
+
if field not in GITHUB_ACTIONS_SCHEMA["valid_job_fields"]:
|
|
82
|
+
errors.append(f"Invalid job field in '{job_name}': {field}")
|
|
83
|
+
|
|
84
|
+
# Validate steps
|
|
85
|
+
if "steps" in job_config:
|
|
86
|
+
for i, step in enumerate(job_config["steps"]):
|
|
87
|
+
if not isinstance(step, dict):
|
|
88
|
+
errors.append(f"Step {i} in job '{job_name}' must be an object")
|
|
89
|
+
continue
|
|
90
|
+
|
|
91
|
+
# Must have either 'uses' or 'run'
|
|
92
|
+
if "uses" not in step and "run" not in step:
|
|
93
|
+
errors.append(
|
|
94
|
+
f"Step {i} in job '{job_name}' must have "
|
|
95
|
+
"either 'uses' or 'run'"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
# Validate step fields
|
|
99
|
+
for field in step.keys():
|
|
100
|
+
if field not in GITHUB_ACTIONS_SCHEMA["valid_step_fields"]:
|
|
101
|
+
errors.append(
|
|
102
|
+
f"Invalid step field in job '{job_name}', "
|
|
103
|
+
f"step {i}: {field}"
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
return errors
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def validate_yaml_syntax(yaml_content: str) -> list[str]:
|
|
110
|
+
"""Validate YAML syntax."""
|
|
111
|
+
errors = []
|
|
112
|
+
try:
|
|
113
|
+
yaml = YAML(typ="safe")
|
|
114
|
+
yaml.load(yaml_content)
|
|
115
|
+
except Exception 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
|
+
yaml = YAML(typ="safe")
|
|
132
|
+
workflow_dict = yaml.load(yaml_content)
|
|
133
|
+
schema_errors = validate_github_actions_schema(workflow_dict)
|
|
134
|
+
errors.extend(schema_errors)
|
|
135
|
+
except Exception as e:
|
|
136
|
+
errors.append(f"Validation error: {str(e)}")
|
|
137
|
+
|
|
138
|
+
return errors
|
workflowforge/secrets.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""Secrets and variables support for all platforms."""
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel, Field
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# GitHub Actions
|
|
7
|
+
class Secret(BaseModel):
|
|
8
|
+
"""Represents a GitHub Actions secret reference."""
|
|
9
|
+
|
|
10
|
+
name: str = Field(..., description="Secret name")
|
|
11
|
+
|
|
12
|
+
def __str__(self) -> str:
|
|
13
|
+
return f"${{{{ secrets.{self.name} }}}}"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Variable(BaseModel):
|
|
17
|
+
"""Represents a GitHub Actions variable reference."""
|
|
18
|
+
|
|
19
|
+
name: str = Field(..., description="Variable name")
|
|
20
|
+
|
|
21
|
+
def __str__(self) -> str:
|
|
22
|
+
return f"${{{{ vars.{self.name} }}}}"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class GitHubContext(BaseModel):
|
|
26
|
+
"""Represents GitHub context variables."""
|
|
27
|
+
|
|
28
|
+
@staticmethod
|
|
29
|
+
def event_name() -> str:
|
|
30
|
+
return "${{ github.event_name }}"
|
|
31
|
+
|
|
32
|
+
@staticmethod
|
|
33
|
+
def ref() -> str:
|
|
34
|
+
return "${{ github.ref }}"
|
|
35
|
+
|
|
36
|
+
@staticmethod
|
|
37
|
+
def sha() -> str:
|
|
38
|
+
return "${{ github.sha }}"
|
|
39
|
+
|
|
40
|
+
@staticmethod
|
|
41
|
+
def actor() -> str:
|
|
42
|
+
return "${{ github.actor }}"
|
|
43
|
+
|
|
44
|
+
@staticmethod
|
|
45
|
+
def repository() -> str:
|
|
46
|
+
return "${{ github.repository }}"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# Jenkins
|
|
50
|
+
class JenkinsCredential(BaseModel):
|
|
51
|
+
"""Represents a Jenkins credential reference."""
|
|
52
|
+
|
|
53
|
+
id: str = Field(..., description="Credential ID")
|
|
54
|
+
|
|
55
|
+
def __str__(self) -> str:
|
|
56
|
+
return f"credentials('{self.id}')"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class JenkinsEnvVar(BaseModel):
|
|
60
|
+
"""Represents a Jenkins environment variable."""
|
|
61
|
+
|
|
62
|
+
name: str = Field(..., description="Environment variable name")
|
|
63
|
+
|
|
64
|
+
def __str__(self) -> str:
|
|
65
|
+
return f"env.{self.name}"
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class JenkinsParam(BaseModel):
|
|
69
|
+
"""Represents a Jenkins parameter."""
|
|
70
|
+
|
|
71
|
+
name: str = Field(..., description="Parameter name")
|
|
72
|
+
|
|
73
|
+
def __str__(self) -> str:
|
|
74
|
+
return f"params.{self.name}"
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# AWS CodeBuild
|
|
78
|
+
class CodeBuildSecret(BaseModel):
|
|
79
|
+
"""Represents an AWS CodeBuild secret from Secrets Manager."""
|
|
80
|
+
|
|
81
|
+
name: str = Field(..., description="Secret name")
|
|
82
|
+
|
|
83
|
+
def __str__(self) -> str:
|
|
84
|
+
return f"${self.name}"
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class CodeBuildParameter(BaseModel):
|
|
88
|
+
"""Represents an AWS CodeBuild parameter from Parameter Store."""
|
|
89
|
+
|
|
90
|
+
name: str = Field(..., description="Parameter name")
|
|
91
|
+
|
|
92
|
+
def __str__(self) -> str:
|
|
93
|
+
return f"${self.name}"
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class CodeBuildEnvVar(BaseModel):
|
|
97
|
+
"""Represents an AWS CodeBuild environment variable."""
|
|
98
|
+
|
|
99
|
+
name: str = Field(..., description="Environment variable name")
|
|
100
|
+
|
|
101
|
+
def __str__(self) -> str:
|
|
102
|
+
return f"${self.name}"
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# Factory functions
|
|
106
|
+
# GitHub Actions
|
|
107
|
+
def secret(name: str) -> str:
|
|
108
|
+
"""Create a GitHub Actions secret reference."""
|
|
109
|
+
return str(Secret(name=name))
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def variable(name: str) -> str:
|
|
113
|
+
"""Create a GitHub Actions variable reference."""
|
|
114
|
+
return str(Variable(name=name))
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def github_context() -> GitHubContext:
|
|
118
|
+
"""Get GitHub context helper."""
|
|
119
|
+
return GitHubContext()
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
# Jenkins
|
|
123
|
+
def jenkins_credential(id: str) -> str:
|
|
124
|
+
"""Create a Jenkins credential reference."""
|
|
125
|
+
return str(JenkinsCredential(id=id))
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def jenkins_env(name: str) -> str:
|
|
129
|
+
"""Create a Jenkins environment variable reference."""
|
|
130
|
+
return str(JenkinsEnvVar(name=name))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def jenkins_param(name: str) -> str:
|
|
134
|
+
"""Create a Jenkins parameter reference."""
|
|
135
|
+
return str(JenkinsParam(name=name))
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
# AWS CodeBuild
|
|
139
|
+
def codebuild_secret(name: str) -> str:
|
|
140
|
+
"""Create a CodeBuild Secrets Manager reference."""
|
|
141
|
+
return str(CodeBuildSecret(name=name))
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def codebuild_parameter(name: str) -> str:
|
|
145
|
+
"""Create a CodeBuild Parameter Store reference."""
|
|
146
|
+
return str(CodeBuildParameter(name=name))
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def codebuild_env(name: str) -> str:
|
|
150
|
+
"""Create a CodeBuild environment variable reference."""
|
|
151
|
+
return str(CodeBuildEnvVar(name=name))
|
workflowforge/step.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""Step definitions for GitHub Actions jobs."""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel, Field
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Step(BaseModel, ABC):
|
|
10
|
+
"""Abstract base class for steps."""
|
|
11
|
+
|
|
12
|
+
name: str | None = Field(None, description="Step name")
|
|
13
|
+
id: str | None = Field(None, description="Unique step ID")
|
|
14
|
+
if_condition: str | None = Field(
|
|
15
|
+
None, alias="if", description="Condition to execute step"
|
|
16
|
+
)
|
|
17
|
+
continue_on_error: bool | None = Field(None, description="Continue on error")
|
|
18
|
+
timeout_minutes: int | None = Field(None, description="Timeout in minutes")
|
|
19
|
+
env: dict[str, Any] | None = Field(None, description="Step environment variables")
|
|
20
|
+
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def to_dict(self) -> dict[str, Any]:
|
|
23
|
+
"""Convert step to dictionary for YAML."""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ActionStep(Step):
|
|
27
|
+
"""Step that executes an action."""
|
|
28
|
+
|
|
29
|
+
uses: str = Field(..., description="Action to use")
|
|
30
|
+
with_params: dict[str, Any] | None = Field(None, description="Action parameters")
|
|
31
|
+
|
|
32
|
+
def to_dict(self) -> dict[str, Any]:
|
|
33
|
+
"""Convert ActionStep to dictionary."""
|
|
34
|
+
result: dict[str, Any] = {"uses": self.uses}
|
|
35
|
+
|
|
36
|
+
if self.name:
|
|
37
|
+
result["name"] = self.name
|
|
38
|
+
if self.id:
|
|
39
|
+
result["id"] = self.id
|
|
40
|
+
if self.if_condition:
|
|
41
|
+
result["if"] = self.if_condition
|
|
42
|
+
if self.continue_on_error is not None:
|
|
43
|
+
result["continue-on-error"] = self.continue_on_error
|
|
44
|
+
if self.timeout_minutes:
|
|
45
|
+
result["timeout-minutes"] = self.timeout_minutes
|
|
46
|
+
if self.env:
|
|
47
|
+
result["env"] = self.env
|
|
48
|
+
if self.with_params:
|
|
49
|
+
result["with"] = self.with_params
|
|
50
|
+
|
|
51
|
+
return result
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class RunStep(Step):
|
|
55
|
+
"""Step that executes shell commands."""
|
|
56
|
+
|
|
57
|
+
run: str = Field(..., description="Command to execute")
|
|
58
|
+
shell: str | None = Field(None, description="Shell to use")
|
|
59
|
+
working_directory: str | None = Field(
|
|
60
|
+
None, alias="working-directory", description="Working directory"
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
def to_dict(self) -> dict[str, Any]:
|
|
64
|
+
"""Convert RunStep to dictionary."""
|
|
65
|
+
result: dict[str, Any] = {"run": self.run}
|
|
66
|
+
|
|
67
|
+
if self.name:
|
|
68
|
+
result["name"] = self.name
|
|
69
|
+
if self.id:
|
|
70
|
+
result["id"] = self.id
|
|
71
|
+
if self.if_condition:
|
|
72
|
+
result["if"] = self.if_condition
|
|
73
|
+
if self.continue_on_error is not None:
|
|
74
|
+
result["continue-on-error"] = self.continue_on_error
|
|
75
|
+
if self.timeout_minutes:
|
|
76
|
+
result["timeout-minutes"] = self.timeout_minutes
|
|
77
|
+
if self.env:
|
|
78
|
+
result["env"] = self.env
|
|
79
|
+
if self.shell:
|
|
80
|
+
result["shell"] = self.shell
|
|
81
|
+
if self.working_directory:
|
|
82
|
+
result["working-directory"] = self.working_directory
|
|
83
|
+
|
|
84
|
+
return result
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# Factory functions for convenient step creation
|
|
88
|
+
def action(
|
|
89
|
+
uses: str,
|
|
90
|
+
name: str | None = None,
|
|
91
|
+
with_: dict[str, Any] | None = None,
|
|
92
|
+
**kwargs: Any,
|
|
93
|
+
) -> ActionStep:
|
|
94
|
+
"""Create an ActionStep conveniently."""
|
|
95
|
+
with_params = with_ if with_ is not None else (kwargs if kwargs else None)
|
|
96
|
+
return ActionStep(uses=uses, name=name, with_params=with_params)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def run(command: str, name: str | None = None, shell: str | None = None) -> RunStep:
|
|
100
|
+
"""Create a RunStep conveniently."""
|
|
101
|
+
return RunStep(run=command, name=name, shell=shell) # nosec B604
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Strategy and matrix definitions for jobs."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Matrix(BaseModel):
|
|
9
|
+
"""Represents a build matrix."""
|
|
10
|
+
|
|
11
|
+
model_config = {"extra": "allow"}
|
|
12
|
+
|
|
13
|
+
include: list[dict[str, Any]] | None = Field(
|
|
14
|
+
None, description="Additional configurations"
|
|
15
|
+
)
|
|
16
|
+
exclude: list[dict[str, Any]] | None = Field(
|
|
17
|
+
None, description="Configurations to exclude"
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
def model_dump(self, **kwargs: Any) -> dict[str, Any]:
|
|
21
|
+
"""Serialize matrix including dynamic variables."""
|
|
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
|
+
"""Represents execution strategy for jobs."""
|
|
28
|
+
|
|
29
|
+
matrix: Matrix | None = Field(None, description="Configuration matrix")
|
|
30
|
+
fail_fast: bool | None = Field(None, description="Fail fast on error")
|
|
31
|
+
max_parallel: int | None = Field(None, description="Maximum parallel jobs")
|
|
32
|
+
|
|
33
|
+
def model_dump(self, **kwargs: Any) -> dict[str, Any]:
|
|
34
|
+
"""Serialize strategy."""
|
|
35
|
+
result: dict[str, Any] = {}
|
|
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 for convenient strategy creation
|
|
48
|
+
def matrix(**variables: Any) -> Matrix:
|
|
49
|
+
"""Create a Matrix conveniently."""
|
|
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
|
+
"""Create a Strategy conveniently."""
|
|
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 .job import Job
|
|
4
|
+
from .secrets import secret
|
|
5
|
+
from .step import action, run
|
|
6
|
+
from .strategy import matrix, strategy
|
|
7
|
+
from .triggers import on_pull_request, on_push, on_release
|
|
8
|
+
from .workflow import Workflow
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def python_ci_template(
|
|
12
|
+
name: str = "Python CI",
|
|
13
|
+
python_versions: list[str] = ["3.11", "3.12", "3.13"],
|
|
14
|
+
branches: list[str] = ["main"],
|
|
15
|
+
) -> Workflow:
|
|
16
|
+
"""Template for Python CI workflow."""
|
|
17
|
+
workflow = Workflow(
|
|
18
|
+
name=name, on=[on_push(branches=branches), on_pull_request(branches=branches)]
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
# Test job with matrix
|
|
22
|
+
test_job = Job(
|
|
23
|
+
runs_on="ubuntu-latest",
|
|
24
|
+
strategy=strategy(matrix=matrix(python_version=python_versions)),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
test_job.add_step(action("actions/checkout@v4", name="Checkout code"))
|
|
28
|
+
test_job.add_step(
|
|
29
|
+
action(
|
|
30
|
+
"actions/setup-python@v5",
|
|
31
|
+
name="Set up Python ${{ matrix.python_version }}",
|
|
32
|
+
with_={"python-version": "${{ matrix.python_version }}"},
|
|
33
|
+
)
|
|
34
|
+
)
|
|
35
|
+
test_job.add_step(run("pip install -e .[dev]", name="Install dependencies"))
|
|
36
|
+
test_job.add_step(run("pytest", name="Run tests"))
|
|
37
|
+
|
|
38
|
+
workflow.add_job("test", test_job)
|
|
39
|
+
return workflow
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def docker_build_template(
|
|
43
|
+
name: str = "Docker Build",
|
|
44
|
+
image_name: str = "my-app",
|
|
45
|
+
dockerfile_path: str = "Dockerfile",
|
|
46
|
+
) -> Workflow:
|
|
47
|
+
"""Template for Docker build workflow."""
|
|
48
|
+
workflow = Workflow(name=name, on=[on_push(), on_pull_request()])
|
|
49
|
+
|
|
50
|
+
build_job = Job(runs_on="ubuntu-latest")
|
|
51
|
+
build_job.add_step(action("actions/checkout@v4", name="Checkout code"))
|
|
52
|
+
build_job.add_step(
|
|
53
|
+
action("docker/setup-buildx-action@v3", name="Set up Docker Buildx")
|
|
54
|
+
)
|
|
55
|
+
build_job.add_step(
|
|
56
|
+
action(
|
|
57
|
+
"docker/login-action@v3",
|
|
58
|
+
name="Login to Docker Hub",
|
|
59
|
+
with_={
|
|
60
|
+
"username": "${{ secrets.DOCKER_USERNAME }}",
|
|
61
|
+
"password": secret("DOCKER_PASSWORD"),
|
|
62
|
+
},
|
|
63
|
+
)
|
|
64
|
+
)
|
|
65
|
+
build_job.add_step(
|
|
66
|
+
action(
|
|
67
|
+
"docker/build-push-action@v5",
|
|
68
|
+
name="Build and push",
|
|
69
|
+
with_={
|
|
70
|
+
"context": ".",
|
|
71
|
+
"file": dockerfile_path,
|
|
72
|
+
"push": "true",
|
|
73
|
+
"tags": f"{image_name}:latest",
|
|
74
|
+
},
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
workflow.add_job("build", build_job)
|
|
79
|
+
return workflow
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def node_ci_template(
|
|
83
|
+
name: str = "Node.js CI",
|
|
84
|
+
node_versions: list[str] = ["18", "20"],
|
|
85
|
+
package_manager: str = "npm",
|
|
86
|
+
) -> Workflow:
|
|
87
|
+
"""Template for Node.js CI workflow."""
|
|
88
|
+
workflow = Workflow(name=name, on=[on_push(), on_pull_request()])
|
|
89
|
+
|
|
90
|
+
test_job = Job(
|
|
91
|
+
runs_on="ubuntu-latest",
|
|
92
|
+
strategy=strategy(matrix=matrix(node_version=node_versions)),
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
test_job.add_step(action("actions/checkout@v4", name="Checkout code"))
|
|
96
|
+
test_job.add_step(
|
|
97
|
+
action(
|
|
98
|
+
"actions/setup-node@v4",
|
|
99
|
+
name="Setup Node.js ${{ matrix.node_version }}",
|
|
100
|
+
with_={
|
|
101
|
+
"node-version": "${{ matrix.node_version }}",
|
|
102
|
+
"cache": package_manager,
|
|
103
|
+
},
|
|
104
|
+
)
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
if package_manager == "npm":
|
|
108
|
+
test_job.add_step(run("npm ci", name="Install dependencies"))
|
|
109
|
+
test_job.add_step(run("npm test", name="Run tests"))
|
|
110
|
+
elif package_manager == "yarn":
|
|
111
|
+
test_job.add_step(
|
|
112
|
+
run("yarn install --frozen-lockfile", name="Install dependencies")
|
|
113
|
+
)
|
|
114
|
+
test_job.add_step(run("yarn test", name="Run tests"))
|
|
115
|
+
|
|
116
|
+
workflow.add_job("test", test_job)
|
|
117
|
+
return workflow
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def release_template(
|
|
121
|
+
name: str = "Release",
|
|
122
|
+
build_command: str = "python -m build",
|
|
123
|
+
test_command: str | None = "pytest",
|
|
124
|
+
) -> Workflow:
|
|
125
|
+
"""Template for release workflow with PyPI publishing."""
|
|
126
|
+
workflow = Workflow(name=name, on=[on_release(types=["published"])])
|
|
127
|
+
|
|
128
|
+
# Test job
|
|
129
|
+
if test_command:
|
|
130
|
+
test_job = Job(runs_on="ubuntu-latest")
|
|
131
|
+
test_job.add_step(action("actions/checkout@v4", name="Checkout code"))
|
|
132
|
+
test_job.add_step(action("actions/setup-python@v5", name="Setup Python"))
|
|
133
|
+
test_job.add_step(run("pip install -e .[dev]", name="Install dependencies"))
|
|
134
|
+
test_job.add_step(run(test_command, name="Run tests"))
|
|
135
|
+
workflow.add_job("test", test_job)
|
|
136
|
+
|
|
137
|
+
# Build job
|
|
138
|
+
build_job = Job(runs_on="ubuntu-latest")
|
|
139
|
+
if test_command:
|
|
140
|
+
build_job.set_needs("test")
|
|
141
|
+
|
|
142
|
+
build_job.add_step(action("actions/checkout@v4", name="Checkout code"))
|
|
143
|
+
build_job.add_step(action("actions/setup-python@v5", name="Setup Python"))
|
|
144
|
+
build_job.add_step(run("pip install build", name="Install build tools"))
|
|
145
|
+
build_job.add_step(run(build_command, name="Build package"))
|
|
146
|
+
build_job.add_step(
|
|
147
|
+
action(
|
|
148
|
+
"actions/upload-artifact@v4",
|
|
149
|
+
name="Upload artifacts",
|
|
150
|
+
with_={"name": "dist", "path": "dist/"},
|
|
151
|
+
)
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
# Publish job
|
|
155
|
+
from .environment import environment
|
|
156
|
+
|
|
157
|
+
publish_job = Job(
|
|
158
|
+
runs_on="ubuntu-latest",
|
|
159
|
+
environment=environment("pypi", "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
|