Skip to content

Commit c7f71d1

Browse files
Copilotchefgs
andcommitted
fix(gitlab-ci): prevent empty stages and missing deploy job when kubernetes disabled (BUG-1)
Co-authored-by: chefgs <7605658+chefgs@users.noreply.github.com>
1 parent 8954189 commit c7f71d1

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

cli/scaffold_gitlab.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def _global_section(args):
9393
stages.append("build")
9494
if args.type in ("test", "complete"):
9595
stages.append("test")
96-
if args.type in ("deploy", "complete") and args.kubernetes:
96+
if args.type == "deploy" or (args.type == "complete" and args.kubernetes):
9797
stages.append("deploy")
9898

9999
return {
@@ -225,9 +225,22 @@ def _test_job(lang_config):
225225

226226

227227
def _deploy_job(args):
228-
"""Kubernetes deploy job."""
228+
"""Kubernetes deploy job, or a generic deploy stub when kubernetes is disabled."""
229229
if not args.kubernetes:
230-
return {}
230+
protected_branches = [b.strip() for b in args.branches.split(",")]
231+
rules = [
232+
{"if": f"$CI_COMMIT_BRANCH == \"{b}\"", "when": "manual" if b != protected_branches[0] else "on_success"}
233+
for b in protected_branches
234+
]
235+
return {
236+
"deploy": {
237+
"stage": "deploy",
238+
"script": [
239+
"echo \"Deploy stage — configure your deployment scripts here\"",
240+
],
241+
"rules": rules,
242+
}
243+
}
231244

232245
protected_branches = [b.strip() for b in args.branches.split(",")]
233246

@@ -298,7 +311,7 @@ def generate_pipeline(args, custom_values):
298311
if args.type in ("test", "complete"):
299312
pipeline.update(_test_job(lang_config))
300313

301-
if args.type in ("deploy", "complete") and args.kubernetes:
314+
if args.type == "deploy" or (args.type == "complete" and args.kubernetes):
302315
pipeline.update(_deploy_job(args))
303316

304317
pipeline.update(custom_values)

tests/test_comprehensive.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -614,22 +614,13 @@ def test_build_job_docker_services(self):
614614
# Build job uses docker-in-docker services
615615
assert "services" in data["build"]
616616

617-
# BUG-1: deploy pipeline with no kubernetes produces empty stages list
618-
@pytest.mark.xfail(
619-
strict=True,
620-
reason=(
621-
"BUG-1: When type='deploy' and kubernetes=False, _global_section() "
622-
"produces an empty stages list because the 'deploy' stage is only "
623-
"appended when args.kubernetes is True. A deploy-only pipeline "
624-
"without Kubernetes has no valid stages, making it an invalid "
625-
"GitLab CI pipeline."
626-
),
627-
)
617+
# BUG-1 regression: deploy pipeline with no kubernetes must still have stages
628618
def test_deploy_pipeline_no_kubernetes_empty_stages(self):
629619
"""
630620
BUG-1: When type='deploy' and kubernetes=False, the generated
631621
pipeline has an empty stages list, which is invalid for GitLab CI.
632-
Expected: at least one stage should be present even for non-k8s deploy.
622+
Expected: at least one stage should be present even for non-k8s deploy,
623+
and a deploy job stub must be present in the pipeline.
633624
"""
634625
with tempfile.TemporaryDirectory() as tmp:
635626
out = os.path.join(tmp, ".gitlab-ci.yml")
@@ -646,6 +637,10 @@ def test_deploy_pipeline_no_kubernetes_empty_stages(self):
646637
assert len(stages) > 0, (
647638
"Expected at least one stage in a deploy pipeline, got: {!r}".format(stages)
648639
)
640+
# A deploy job stub must be present so the pipeline is valid
641+
assert "deploy" in data, (
642+
"Expected a deploy job in the pipeline, got keys: {!r}".format(list(data.keys()))
643+
)
649644

650645

651646
# ===========================================================================

0 commit comments

Comments
 (0)