-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathscaffold_cicd.py
More file actions
executable file
·252 lines (212 loc) · 8.03 KB
/
scaffold_cicd.py
File metadata and controls
executable file
·252 lines (212 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python3
"""
DevOps-OS CI/CD Generator Helper
Generate both GitHub Actions workflows and Jenkins pipelines in one step.
Uses the scaffold_gha and scaffold_jenkins modules directly — no subprocess
calls, no external script files required.
Usage:
python -m cli.scaffold_cicd [options]
python -m cli.devopsos scaffold cicd [options]
"""
TEMPLATE_INFO = {
"name": "cicd",
"category": "CI/CD",
"description": "Combined CI/CD (GHA + Jenkins)",
}
import os
import sys
import argparse
def _run_module_main(module_main, flags: list) -> bool:
"""Call *module_main()* with the given CLI flag list via sys.argv swap.
Returns True on success, False if the module exits with a non-zero code.
"""
saved = sys.argv[:]
sys.argv = sys.argv[:1] + flags
try:
module_main()
return True
except SystemExit as exc:
if exc.code not in (None, 0):
return False
return True
except Exception as exc: # noqa: BLE001
print(f" Error ({type(exc).__name__}): {exc}")
return False
finally:
sys.argv = saved
def parse_arguments():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="Generate CI/CD configurations (GitHub Actions + Jenkins) for DevOps-OS"
)
parser.add_argument("--name", help="CI/CD pipeline name", default="DevOps-OS")
parser.add_argument(
"--type",
choices=["build", "test", "deploy", "complete"],
help="Type of CI/CD pipeline to generate",
default="complete",
)
parser.add_argument(
"--languages",
help="Comma-separated list of languages",
default="python,javascript",
)
parser.add_argument(
"--kubernetes",
action="store_true",
help="Include Kubernetes deployment steps",
)
parser.add_argument(
"--k8s-method",
choices=["kubectl", "kustomize", "argocd", "flux"],
help="Kubernetes deployment method",
default="kubectl",
)
parser.add_argument("--output-dir", help="Root output directory", default=os.getcwd())
parser.add_argument("--registry", help="Container registry URL", default="docker.io")
parser.add_argument(
"--image",
help="DevOps-OS container image",
default="docker.io/yourorg/devops-os:latest",
)
parser.add_argument("--custom-values", help="Path to custom values JSON file")
parser.add_argument(
"--matrix",
action="store_true",
help="Enable matrix builds for GitHub Actions",
)
parser.add_argument(
"--parameters",
action="store_true",
help="Enable parameterized builds for Jenkins",
)
parser.add_argument(
"--github",
action="store_true",
help="Generate GitHub Actions workflow only",
)
parser.add_argument(
"--jenkins",
action="store_true",
help="Generate Jenkins pipeline only",
)
parser.add_argument(
"--all",
action="store_true",
help="Generate both GitHub Actions and Jenkins (default when neither --github nor --jenkins is given)",
)
args = parser.parse_args()
# Default to --all when neither --github nor --jenkins is specified
if not (args.github or args.jenkins or args.all):
args.all = True
if args.all:
args.github = True
args.jenkins = True
return args
def run_github_generator(args) -> bool:
"""Generate GitHub Actions workflow using the scaffold_gha module."""
import cli.scaffold_gha as scaffold_gha # noqa: PLC0415 (local import to avoid circular)
print("Generating GitHub Actions workflow...")
github_output = os.path.join(args.output_dir, ".github", "workflows")
os.makedirs(github_output, exist_ok=True)
flags = [
"--name", args.name,
"--type", args.type,
"--languages", args.languages,
"--registry", args.registry,
"--image", args.image,
"--output", github_output,
]
if args.kubernetes:
flags += ["--kubernetes", "--k8s-method", args.k8s_method]
if args.matrix:
flags.append("--matrix")
if args.custom_values:
flags += ["--custom-values", args.custom_values]
ok = _run_module_main(scaffold_gha.main, flags)
if ok:
print("GitHub Actions workflow generated successfully!")
else:
print("Error generating GitHub Actions workflow. Check output above.")
return ok
def run_jenkins_generator(args) -> bool:
"""Generate Jenkins pipeline using the scaffold_jenkins module."""
import cli.scaffold_jenkins as scaffold_jenkins # noqa: PLC0415
print("Generating Jenkins pipeline...")
jenkins_output = os.path.join(args.output_dir, "Jenkinsfile")
flags = [
"--name", args.name,
"--type", args.type,
"--languages", args.languages,
"--registry", args.registry,
"--image", args.image,
"--output", jenkins_output,
]
if args.kubernetes:
flags += ["--kubernetes", "--k8s-method", args.k8s_method]
if args.parameters:
flags.append("--parameters")
if args.custom_values:
flags += ["--custom-values", args.custom_values]
ok = _run_module_main(scaffold_jenkins.main, flags)
if ok:
print("Jenkins pipeline generated successfully!")
else:
print("Error generating Jenkins pipeline. Check output above.")
return ok
def create_readme(args):
"""Create a README.md file explaining the generated CI/CD files."""
readme_path = os.path.join(args.output_dir, "CICD-README.md")
with open(readme_path, "w") as f:
f.write(f"# {args.name} CI/CD Configuration\n\n")
f.write("This directory contains CI/CD configuration files generated by DevOps-OS.\n\n")
if args.github:
f.write("## GitHub Actions Workflow\n\n")
f.write(f"- Type: {args.type}\n")
f.write(f"- Languages: {args.languages}\n")
if args.kubernetes:
f.write(f"- Kubernetes Deployment Method: {args.k8s_method}\n")
if args.matrix:
f.write("- Matrix Build: Enabled\n")
f.write("\nWorkflow location: `.github/workflows/`\n\n")
if args.jenkins:
f.write("## Jenkins Pipeline\n\n")
f.write(f"- Type: {args.type}\n")
f.write(f"- Languages: {args.languages}\n")
if args.kubernetes:
f.write(f"- Kubernetes Deployment Method: {args.k8s_method}\n")
if args.parameters:
f.write("- Parameterized: Enabled\n")
f.write("\nPipeline location: `Jenkinsfile`\n\n")
f.write("## Usage\n\n")
if args.github:
f.write("### GitHub Actions\n\n")
f.write("The GitHub Actions workflow will automatically run when you push to your repository.\n\n")
if args.jenkins:
f.write("### Jenkins\n\n")
f.write("To use the Jenkins pipeline:\n\n")
f.write("1. Create a new Jenkins Pipeline job\n")
f.write("2. Configure it to use the Jenkinsfile in your repository\n")
if args.parameters:
f.write("3. The pipeline includes parameters you can configure for each build\n\n")
f.write("\n## Generated with DevOps-OS\n\n")
f.write("These CI/CD configurations were generated using DevOps-OS CI/CD generators.\n")
f.write("For more information, see the DevOps-OS documentation.\n")
print(f"Created CI/CD README: {readme_path}")
def main():
"""Main function."""
args = parse_arguments()
success = True
if args.github:
if not run_github_generator(args):
success = False
if args.jenkins:
if not run_jenkins_generator(args):
success = False
if success:
create_readme(args)
print("\nCI/CD generation completed successfully!")
else:
print("\nCI/CD generation completed with errors. Please check the output above.")
if __name__ == "__main__":
main()