From 18ec0ba6af989a0bf06f8a200a347b64c6909b8a Mon Sep 17 00:00:00 2001 From: Johannes Carlsson Date: Tue, 1 Apr 2025 09:16:22 +0200 Subject: [PATCH] Add support for "prune-tags" in git scm "prune-tags" removes tags from the local workspace before fetch if they no longer exist on the remote. Support for this was added to the git plugin in JENKINS-40529. Change-Id: I204fd0a508944fecba8a3a26527793cdf37234f8 --- jenkins_jobs/modules/scm.py | 10 ++++++++++ tests/multibranch/fixtures/scm_git_full.xml | 3 +++ tests/multibranch/fixtures/scm_git_full.yaml | 1 + 3 files changed, 14 insertions(+) diff --git a/jenkins_jobs/modules/scm.py b/jenkins_jobs/modules/scm.py index 4d56964e5..140884f20 100644 --- a/jenkins_jobs/modules/scm.py +++ b/jenkins_jobs/modules/scm.py @@ -307,6 +307,7 @@ def git(registry, xml_parent, data): * **per-build-tag** (`bool`) - Create a tag in the workspace for every build. (default is inverse of skip-tag if set, otherwise false) * **prune** (`bool`) - Prune remote branches (default false) + * **prune-tags** (`bool`) - Prune tags if they no longer exist on the remote (default false) * **scm-name** (`string`) - The unique scm name for this Git SCM (optional) * **shallow-clone** (`bool`) - Perform shallow clone (default false) @@ -763,6 +764,15 @@ def git_extensions(xml_parent, data): ext = XML.SubElement(tr, "extension", {"class": ext_name}) else: ext = XML.SubElement(xml_parent, ext_name) + prune = str(data.get("prune-tags", False)).lower() + if prune == "true": + ext_name = impl_prefix + "PruneStaleTag" + if trait: + trait_name = "PruneStaleTagTrait" + tr = XML.SubElement(xml_parent, trait_prefix + trait_name) + ext = XML.SubElement(tr, "extension", {"class": ext_name}) + else: + ext = XML.SubElement(xml_parent, ext_name) ignore_notify_commits = str(data.get("ignore-notify", False)).lower() if not trait and ignore_notify_commits == "true": XML.SubElement(xml_parent, impl_prefix + "IgnoreNotifyCommit") diff --git a/tests/multibranch/fixtures/scm_git_full.xml b/tests/multibranch/fixtures/scm_git_full.xml index f3b7fa68d..e6f0f194c 100644 --- a/tests/multibranch/fixtures/scm_git_full.xml +++ b/tests/multibranch/fixtures/scm_git_full.xml @@ -110,6 +110,9 @@ + + + diff --git a/tests/multibranch/fixtures/scm_git_full.yaml b/tests/multibranch/fixtures/scm_git_full.yaml index 65e51b761..eef8c4d03 100644 --- a/tests/multibranch/fixtures/scm_git_full.yaml +++ b/tests/multibranch/fixtures/scm_git_full.yaml @@ -71,6 +71,7 @@ scm: after: true before: true prune: true + prune-tags: true local-branch: true sparse-checkout: paths: